Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 85 additions & 40 deletions packages/typescript/schemas/contentmapper.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -410,51 +410,96 @@
"required": ["unusedExpectDirectiveDiagnostics", "directives"]
},
"MappedDiagnosticDirective": {
"type": "array",
"description": "[originalStart, originalLength, virtualStart, virtualEnd, policy, unusedExpectDirectiveIndex?]. Expect directives require a valid original range. An omitted index selects the only shared unused-expect diagnostic and requires exactly one such diagnostic.",
"title": "Diagnostic directive",
"items": [
{
"type": "integer",
"title": "originalStart",
"description": "Original start offset. It is ignored for ignore directives if the original range is invalid."
},
{
"type": "integer",
"title": "originalLength",
"description": "Original span length. It is ignored for ignore directives if the original range is invalid."
},
{
"type": "integer",
"minimum": 0,
"title": "virtualStart",
"description": "Inclusive virtual start offset."
},
{
"type": "integer",
"minimum": 0,
"title": "virtualEnd",
"description": "Exclusive virtual end offset, which must not precede the start."
},
"description": "[originalStart, originalLength, virtualStart, virtualEnd, 0, ...diagnosticCodes] or [originalStart, originalLength, virtualStart, virtualEnd, 1, unusedExpectDirectiveIndex?].",
"oneOf": [
{
"$ref": "#/definitions/DiagnosticDirectivePolicy",
"title": "policy"
"type": "array",
"description": "Ignore directive suppressing TypeScript diagnostics in the virtual range. An invalid original range is ignored. When diagnostic codes are given, only matching diagnostics are suppressed.",
"items": [
{
"type": "integer",
"title": "originalStart",
"description": "Original start offset."
},
{
"type": "integer",
"title": "originalLength",
"description": "Original span length."
},
{
"type": "integer",
"minimum": 0,
"title": "virtualStart",
"description": "Inclusive virtual start offset."
},
{
"type": "integer",
"minimum": 0,
"title": "virtualEnd",
"description": "Exclusive virtual end offset, which must not precede the start."
},
{
"type": "integer",
"const": 0,
"title": "policy",
"description": "Ignore directive policy."
}
],
"additionalItems": {
"type": "integer",
"minimum": 1,
"maximum": 2147483647,
"title": "diagnosticCode",
"description": "TypeScript diagnostic code to ignore."
},
"minItems": 5
},
{
"type": "integer",
"minimum": 0,
"title": "unusedExpectDirectiveIndex",
"description": "Index into unusedExpectDirectiveDiagnostics, used by expect directives."
"type": "array",
"description": "Expect directive suppressing TypeScript diagnostics in the virtual range. It requires a valid original range and reports an unused-expect diagnostic there when no diagnostic is suppressed.",
"items": [
{
"type": "integer",
"title": "originalStart",
"minimum": 0,
"description": "Original start offset."
},
{
"type": "integer",
"title": "originalLength",
"minimum": 0,
"description": "Original span length."
},
{
"type": "integer",
"minimum": 0,
"title": "virtualStart",
"description": "Inclusive virtual start offset."
},
{
"type": "integer",
"minimum": 0,
"title": "virtualEnd",
"description": "Exclusive virtual end offset, which must not precede the start."
},
{
"type": "integer",
"const": 1,
"title": "policy",
"description": "Expect directive policy."
},
{
"type": "integer",
"minimum": 0,
"title": "unusedExpectDirectiveIndex",
"description": "Index into unusedExpectDirectiveDiagnostics. An omitted index selects the only shared unused-expect diagnostic and requires exactly one such diagnostic."
}
],
"minItems": 5,
"maxItems": 6
}
],
"minItems": 5,
"maxItems": 6,
"if": {
"items": [{}, {}, {}, {}, { "const": 1 }]
},
"then": {
"items": [{ "minimum": 0 }, { "minimum": 0 }]
}
]
},
"SupplementalOutput": {
"$ref": "#/definitions/MappedOutput"
Expand Down
100 changes: 100 additions & 0 deletions packages/typescript/src/api/node/msgpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,106 @@ export class MsgpackReader {
throw new Error(`Expected array header, got 0x${byte.toString(16)}`);
}

skipValue(): void {
let remaining = 1;
while (remaining > 0) {
this.skipBytes(1);
const byte = this.buf[this.pos - 1];
remaining--;
if (byte <= 0x7f || byte >= 0xe0 || byte === 0xc0 || byte === 0xc2 || byte === 0xc3) continue;
if ((byte & 0xe0) === 0xa0) {
this.skipBytes(byte & 0x1f);
}
else if ((byte & 0xf0) === 0x90) {
remaining += byte & 0x0f;
}
else if ((byte & 0xf0) === 0x80) {
remaining += (byte & 0x0f) * 2;
}
else {
switch (byte) {
case 0xcc:
case 0xd0:
this.skipBytes(1);
break;
case 0xcd:
case 0xd1:
case 0xd4:
this.skipBytes(2);
break;
case 0xce:
case 0xd2:
case 0xca:
this.skipBytes(4);
break;
case 0xcf:
case 0xd3:
case 0xcb:
this.skipBytes(8);
break;
case 0xd5:
this.skipBytes(3);
break;
case 0xd6:
this.skipBytes(5);
break;
case 0xd7:
this.skipBytes(9);
break;
case 0xd8:
this.skipBytes(17);
break;
case 0xc4:
case 0xd9:
this.skipBytes(this.readLength(1));
break;
case 0xc5:
case 0xda:
this.skipBytes(this.readLength(2));
break;
case 0xc6:
case 0xdb:
this.skipBytes(this.readLength(4));
break;
case 0xc7:
this.skipBytes(this.readLength(1) + 1);
break;
case 0xc8:
this.skipBytes(this.readLength(2) + 1);
break;
case 0xc9:
this.skipBytes(this.readLength(4) + 1);
break;
case 0xdc:
remaining += this.readLength(2);
break;
case 0xdd:
remaining += this.readLength(4);
break;
case 0xde:
remaining += this.readLength(2) * 2;
break;
case 0xdf:
remaining += this.readLength(4) * 2;
break;
default:
throw new Error(`Invalid MessagePack prefix 0x${byte.toString(16)}`);
}
}
}
}

private skipBytes(length: number): void {
if (length > this.buf.length - this.pos) throw new Error("Unexpected end of MessagePack data");
this.pos += length;
}

private readLength(bytes: 1 | 2 | 4): number {
const offset = this.pos;
this.skipBytes(bytes);
return bytes === 1 ? this.buf[offset] : bytes === 2 ? this.view.getUint16(offset, false) : this.view.getUint32(offset, false);
}

readUint(): number {
const byte = this.buf[this.pos++];
if (byte <= 0x7f) return byte;
Expand Down
14 changes: 11 additions & 3 deletions packages/typescript/src/api/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,16 +397,24 @@ export class RemoteSourceFile extends RemoteNode implements SourceFileInfo {
const count = reader.readArrayHeader();
const directives = Array<MappedDiagnosticDirective>(count);
for (let i = 0; i < count; i++) {
if (reader.readArrayHeader() !== 6) throw new Error("Invalid diagnostic directive");
const tupleLength = reader.readArrayHeader();
if (tupleLength < 7) throw new Error("Invalid diagnostic directive");
const originalStart = reader.readUint();
const originalLength = reader.readUint();
const virtualStart = reader.readUint();
const virtualLength = reader.readUint();
const policy = reader.readUint();
const unusedCode = reader.readUint();
const codeCount = reader.readArrayHeader();
const diagnosticCodes = Array<number>(codeCount);
for (let j = 0; j < codeCount; j++) diagnosticCodes[j] = reader.readUint();
for (let j = 7; j < tupleLength; j++) reader.skipValue();
directives[i] = {
originalRange: { pos: originalStart, end: originalStart + originalLength },
virtualRange: { pos: virtualStart, end: virtualStart + virtualLength },
policy: reader.readUint(),
unusedCode: reader.readUint(),
policy,
unusedCode,
diagnosticCodes,
};
}
return this._cachedDiagnosticDirectives = directives;
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript/src/api/node/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const PROTOCOL_VERSION = 9;
export const PROTOCOL_VERSION = 10;

export const HEADER_OFFSET_METADATA = 0;
export const HEADER_OFFSET_HASH_LO0 = 4;
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript/src/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export interface MappedDiagnosticDirective {
readonly virtualRange: ReadonlyTextRange;
readonly policy: DiagnosticDirectivePolicy;
readonly unusedCode: number;
/** For ignore directives, an empty list selects all diagnostic codes. */
readonly diagnosticCodes: readonly number[];
}

export interface SourceFile extends Node {
Expand Down
16 changes: 15 additions & 1 deletion packages/typescript/test/async/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3111,7 +3111,7 @@ export const other: { value: string } = box;`,
}
});

test("getSourceFile includes supplemental files without physical files", {
test("getSourceFile exposes mapped directives and supplemental files", {
skip: process.env.TS_API_TEST_WASM ? "WASM cannot spawn external content mapper processes" : false,
}, async t => {
const directory = mkdtempSync(join(tmpdir(), "ts-project-membership-"));
Expand All @@ -3127,6 +3127,10 @@ export const other: { value: string } = box;`,
connection.onRequest("transform", ({ content }) => ({
text: "export {};",
extension: ".ts",
diagnosticDirectives: {
unusedExpectDirectiveDiagnostics: [],
directives: [[0, 0, 0, 10, 0, 2353, 2322]],
},
supplemental: [{ text: content, extension: ".ts" }],
}));
connection.listen();
Expand All @@ -3153,6 +3157,16 @@ export const other: { value: string } = box;`,
await using api = new API({ cwd: directory, runExternalCode: true });
const snapshot = await api.createSnapshot({ openProjects: [configFileName] });
const program = snapshot.getConfiguredProject(configFileName)!.program;
const mappedFileName = toRootedFilePath(join(directory, "app.box"), undefined);
const mappedFile = await program.getSourceFile(mappedFileName);
assert.ok(mappedFile);
assert.deepEqual(mappedFile.diagnosticDirectives, [{
originalRange: { pos: 0, end: 0 },
virtualRange: { pos: 0, end: 10 },
policy: 0,
unusedCode: 0,
diagnosticCodes: [2353, 2322],
}]);
const supplementalFileName = toRootedFilePath(join(directory, "app.box.0.ts"), undefined);
assert.equal(existsSync(supplementalFileName), false);
assert.ok(await program.getSourceFile(supplementalFileName));
Expand Down
35 changes: 35 additions & 0 deletions packages/typescript/test/encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ import {
encodeSourceFile,
uint8ArrayToBase64,
} from "../src/api/node/encoder.ts";
import { MsgpackWriter } from "../src/api/node/msgpack.ts";
import {
RemoteNode,
RemoteNodeList,
RemoteSourceFile,
} from "../src/api/node/node.ts";
import {
HEADER_OFFSET_EXTENDED_DATA,
HEADER_OFFSET_NODES,
HEADER_OFFSET_STRUCTURED_DATA,
HEADER_SIZE,
NODE_LEN,
NODE_OFFSET_DATA,
Expand Down Expand Up @@ -116,6 +119,38 @@ describe("Encoder", { concurrency }, () => {
assert.strictEqual(decoded.canonicalSourceFileName, undefined);
});

test("skips unknown trailing diagnostic directive fields", () => {
const writer = new MsgpackWriter();
writer.writeArrayHeader(1); // one directive
writer.writeArrayHeader(8); // seven known fields plus one from a future protocol
for (const value of [0, 1, 2, 3, 0, 0]) writer.writeUint(value); // ranges [0, 1) and [2, 5), ignore policy, no unused code
writer.writeArrayHeader(1); // diagnostic codes
writer.writeUint(2322);
writer.writeBool(true); // unknown field
const directives = writer.finish();

// Splice the directives in as structured data, just before the node table.
const encoded = encodeSourceFile(makeSF("text", "/component.vue.ts", []));
const nodesOffset = new DataView(encoded.buffer, encoded.byteOffset).getUint32(HEADER_OFFSET_NODES, true);
const withDirectives = new Uint8Array(encoded.length + directives.length);
withDirectives.set(encoded.subarray(0, nodesOffset));
withDirectives.set(directives, nodesOffset);
withDirectives.set(encoded.subarray(nodesOffset), nodesOffset + directives.length);
const view = new DataView(withDirectives.buffer);
view.setUint32(HEADER_OFFSET_STRUCTURED_DATA, nodesOffset, true);
view.setUint32(HEADER_OFFSET_NODES, nodesOffset + directives.length, true);
// The source file's extended data points at the directives (offset 0 within structured data).
view.setUint32(view.getUint32(HEADER_OFFSET_EXTENDED_DATA, true) + 72, 0, true);

assert.deepStrictEqual(decode(withDirectives).diagnosticDirectives, [{
originalRange: { pos: 0, end: 1 },
virtualRange: { pos: 2, end: 5 },
policy: 0,
unusedCode: 0,
diagnosticCodes: [2322],
}]);
});

test("keeps adjacent surrogate string-table entries separate", () => {
const high = String.fromCharCode(0xD800);
const low = String.fromCharCode(0xDC00);
Expand Down
Loading
Loading