From b3aaf561ee6fdfa5c1ad9b8c181c5e71acc468d2 Mon Sep 17 00:00:00 2001 From: ubugeeei Date: Mon, 16 Nov 2026 01:31:47 +0900 Subject: [PATCH 1/7] feat(content-mapper): select ignored diagnostic codes Co-authored-by: miyaoka <1443118+miyaoka@users.noreply.github.com> --- packages/typescript/src/ast/ast.ts | 1 + tsc/internal/ast/ast.go | 1 + tsc/internal/compiler/contentmapper_test.go | 33 +++++++++++ tsc/internal/compiler/program.go | 3 +- tsc/internal/contentmapper/host.go | 1 + tsc/internal/contentmapper/host_test.go | 61 +++++++++++++++++---- tsc/internal/contentmapper/hostimpl.go | 46 ++++++++++++++-- 7 files changed, 127 insertions(+), 19 deletions(-) diff --git a/packages/typescript/src/ast/ast.ts b/packages/typescript/src/ast/ast.ts index cb3c6dcd2d731..c9b47594372c3 100644 --- a/packages/typescript/src/ast/ast.ts +++ b/packages/typescript/src/ast/ast.ts @@ -174,6 +174,7 @@ export interface MappedDiagnosticDirective { readonly originalRange: ReadonlyTextRange; readonly virtualRange: ReadonlyTextRange; readonly policy: DiagnosticDirectivePolicy; + readonly diagnosticCode: number; readonly unusedCode: number; } diff --git a/tsc/internal/ast/ast.go b/tsc/internal/ast/ast.go index 2b1cff046e6d9..4ace2ed4d511b 100644 --- a/tsc/internal/ast/ast.go +++ b/tsc/internal/ast/ast.go @@ -2546,6 +2546,7 @@ type MappedDiagnosticDirective struct { OriginalRange core.TextRange VirtualRange core.TextRange Policy MappedDiagnosticDirectivePolicy + DiagnosticCode int32 UnusedCode int32 UnusedMessageText string Source string diff --git a/tsc/internal/compiler/contentmapper_test.go b/tsc/internal/compiler/contentmapper_test.go index 08b3f9959679d..ce0e8a444037e 100644 --- a/tsc/internal/compiler/contentmapper_test.go +++ b/tsc/internal/compiler/contentmapper_test.go @@ -866,6 +866,39 @@ func TestContentMapperDirectivesPreserveIncrementalGlobals(t *testing.T) { } } +func TestContentMapperDiagnosticDirectiveCodeSelector(t *testing.T) { + t.Parallel() + const text = ` +const object: { known: string } = { extra: 1 }; +const value: string = 1; +` + program := newContentMapperProgram(t, fakeContentMapperHost{ + transform: func(fileName string, content string) (contentmapper.Result, error) { + return contentmapper.Result{ + Text: text, + VirtualExtension: ".ts", + Mappings: spanmap.New([]spanmap.Segment{{ + OriginalEnd: core.TextPos(len(content)), + VirtualEnd: core.TextPos(len(text)), + Kind: spanmap.KindVerbatim, + Features: spanmap.FeatureAll, + }}), + DiagnosticDirectives: []ast.MappedDiagnosticDirective{{ + VirtualRange: core.NewTextRange(0, len(text)), + OriginalRange: core.NewTextRange(0, len(content)), + Policy: ast.MappedDiagnosticDirectivePolicyIgnore, + DiagnosticCode: 2353, + }}, + }, nil + }, + }, map[string]string{"/src/Component.vue": text}, []string{"/src/Component.vue"}) + + file := program.GetSourceFile("/src/Component.vue") + diags := program.GetSemanticDiagnostics(t.Context(), file) + assert.Equal(t, len(diags), 1) + assert.Equal(t, diags[0].Code(), int32(2322)) +} + func TestCompositeProjectContentMapperSupplementalRoots(t *testing.T) { t.Parallel() contentMapperHost := fakeContentMapperHost{transform: func(fileName string, content string) (contentmapper.Result, error) { diff --git a/tsc/internal/compiler/program.go b/tsc/internal/compiler/program.go index 06854eada0bb7..1d8b563973cb9 100644 --- a/tsc/internal/compiler/program.go +++ b/tsc/internal/compiler/program.go @@ -1744,7 +1744,8 @@ func applyContentMapperDiagnosticDirectives(sourceFile *ast.SourceFile, diags [] return false } for i, directive := range directives { - if diag.Pos() >= directive.VirtualRange.Pos() && diag.Pos() < directive.VirtualRange.End() { + if diag.Pos() >= directive.VirtualRange.Pos() && diag.Pos() < directive.VirtualRange.End() && + (directive.DiagnosticCode == 0 || diag.Code() == directive.DiagnosticCode) { used[i] = true return true } diff --git a/tsc/internal/contentmapper/host.go b/tsc/internal/contentmapper/host.go index 36875da96e65b..75fc592f766b4 100644 --- a/tsc/internal/contentmapper/host.go +++ b/tsc/internal/contentmapper/host.go @@ -49,6 +49,7 @@ const ( DiagnosticDirectiveErrorKindExpectMissingUnusedDiagnostic DiagnosticDirectiveErrorKindInvalidUnusedDiagnosticIndex DiagnosticDirectiveErrorKindOverlap + DiagnosticDirectiveErrorKindInvalidDiagnosticCode ) // DiagnosticDirectiveError reports an invalid diagnostic directive in a transform response. diff --git a/tsc/internal/contentmapper/host_test.go b/tsc/internal/contentmapper/host_test.go index 5cbcaa5e64c8f..b879f89cf5869 100644 --- a/tsc/internal/contentmapper/host_test.go +++ b/tsc/internal/contentmapper/host_test.go @@ -476,6 +476,20 @@ func TestRunnerTransformDiagnosticDirectives(t *testing.T) { assert.Equal(t, directive.UnusedCode, int32(2578)) assert.Equal(t, directive.UnusedMessageText, "Unused framework directive.") assert.Equal(t, directive.Source, "mapper") + diagnosticCode := int32(2353) + result, err = transform(contentmapper.MappedOutput{ + Text: "virtual source", Extension: ".ts", + DiagnosticDirectives: protocolDiagnosticDirectives([]contentmapper.MappedDiagnosticDirective{{ + OriginalStart: 0, + OriginalLength: 9, + VirtualStart: 8, + VirtualEnd: 14, + Policy: contentmapper.DiagnosticDirectivePolicyIgnore, + DiagnosticCode: &diagnosticCode, + }}), + }) + assert.NilError(t, err) + assert.Equal(t, result.DiagnosticDirectives[0].DiagnosticCode, diagnosticCode) unusedIndex := 1 result, err = transform(contentmapper.MappedOutput{ Text: "virtual source", Extension: ".ts", @@ -525,6 +539,15 @@ func TestRunnerTransformDiagnosticDirectives(t *testing.T) { }}, kind: contentmapper.DiagnosticDirectiveErrorKindInvalidPolicy, }, + { + name: "invalid diagnostic code", + text: "x", + directives: []contentmapper.MappedDiagnosticDirective{{ + Policy: contentmapper.DiagnosticDirectivePolicyIgnore, + DiagnosticCode: new(int32), + }}, + kind: contentmapper.DiagnosticDirectiveErrorKindInvalidDiagnosticCode, + }, { name: "expect requires unused diagnostic", text: "x", @@ -588,6 +611,7 @@ func TestRunnerTransformDiagnosticDirectives(t *testing.T) { func TestMappedDiagnosticDirectiveJSON(t *testing.T) { t.Parallel() + diagnosticCode := int32(2353) tests := []struct { name string directive contentmapper.MappedDiagnosticDirective @@ -602,6 +626,16 @@ func TestMappedDiagnosticDirectiveJSON(t *testing.T) { }, want: `[0,9,8,14,0]`, }, + { + name: "selected ignore", + directive: contentmapper.MappedDiagnosticDirective{ + VirtualStart: 8, VirtualEnd: 14, + OriginalStart: 0, OriginalLength: 9, + Policy: contentmapper.DiagnosticDirectivePolicyIgnore, + DiagnosticCode: &diagnosticCode, + }, + want: `[0,9,8,14,0,2353]`, + }, { name: "expect", directive: contentmapper.MappedDiagnosticDirective{ @@ -877,18 +911,19 @@ func TestRunnerLeaseLifecycle(t *testing.T) { // recordingMapper captures project configuration and lifecycle requests for host protocol tests. type recordingMapper struct { - mu sync.Mutex - received string - receivedOptions string - receivedLocale string - projectHandles []string - closedHandles []string - transformHandle string - transformParams string - watchedFiles []string - configIdentity *string - dynamicConfig bool - optionDiagnostics []contentmapper.OptionDiagnosticResult + mu sync.Mutex + received string + receivedOptions string + receivedLocale string + receivedCodeSelectors bool + projectHandles []string + closedHandles []string + transformHandle string + transformParams string + watchedFiles []string + configIdentity *string + dynamicConfig bool + optionDiagnostics []contentmapper.OptionDiagnosticResult } type blockingMapper struct { @@ -917,6 +952,7 @@ func (m *recordingMapper) HandleRequest(ctx context.Context, method string, para } m.mu.Lock() m.receivedLocale = p.Locale + m.receivedCodeSelectors = p.Capabilities.DiagnosticDirectiveCodeSelectors m.mu.Unlock() return contentmapper.InitializeResult{PositionEncoding: contentmapper.PositionEncodingUTF8, DiagnosticSource: "mapper"}, nil case contentmapper.MethodOpenProject: @@ -1302,6 +1338,7 @@ func TestRunnerForwardsProjectOptions(t *testing.T) { assert.Equal(t, mapper.received, string(want)) assert.Equal(t, mapper.receivedOptions, `{"strictTemplates":true}`) assert.Equal(t, mapper.receivedLocale, "cs-CZ") + assert.Assert(t, mapper.receivedCodeSelectors) assert.Assert(t, !strings.Contains(mapper.transformParams, `"options"`)) assert.Assert(t, !strings.Contains(mapper.transformParams, `"compilerOptions"`)) } diff --git a/tsc/internal/contentmapper/hostimpl.go b/tsc/internal/contentmapper/hostimpl.go index a1ce6789d28d6..54fcf0d6adf8d 100644 --- a/tsc/internal/contentmapper/hostimpl.go +++ b/tsc/internal/contentmapper/hostimpl.go @@ -45,6 +45,15 @@ type InitializeParams struct { Locale string `json:"locale,omitempty"` // PositionEncodings lists the coordinate spaces the host accepts. PositionEncodings []PositionEncoding `json:"positionEncodings"` + // Capabilities describes optional protocol extensions the host can decode. + Capabilities InitializeCapabilities `json:"capabilities"` +} + +type InitializeCapabilities struct { + // DiagnosticDirectiveCodeSelectors allows an ignore tuple's sixth element to + // select one TypeScript diagnostic code instead of suppressing every code in + // the tuple's virtual range. + DiagnosticDirectiveCodeSelectors bool `json:"diagnosticDirectiveCodeSelectors"` } // InitializeResult is the mapper's response to the initialize request. @@ -141,8 +150,10 @@ type DiagnosticDirectives struct { } // MappedDiagnosticDirective is encoded as -// [originalStart, originalLength, virtualStart, virtualEnd, policy, unusedExpectDirectiveIndex?]. -// An omitted index selects the only unused-expect diagnostic and is invalid when there is not exactly one. +// [originalStart, originalLength, virtualStart, virtualEnd, policy, policyValue?]. +// For expect directives policyValue is the unused-expect diagnostic index. An omitted index selects the only +// unused-expect diagnostic and is invalid when there is not exactly one. For ignore directives policyValue is an +// optional TypeScript diagnostic-code selector. type MappedDiagnosticDirective struct { OriginalStart int OriginalLength int @@ -150,6 +161,7 @@ type MappedDiagnosticDirective struct { VirtualEnd int Policy DiagnosticDirectivePolicy UnusedExpectDirectiveIndex *int + DiagnosticCode *int32 } var ( @@ -159,7 +171,9 @@ var ( func (d MappedDiagnosticDirective) MarshalJSONTo(enc *json.Encoder) error { tuple := []any{d.OriginalStart, d.OriginalLength, d.VirtualStart, d.VirtualEnd, d.Policy} - if d.UnusedExpectDirectiveIndex != nil { + if d.Policy == DiagnosticDirectivePolicyIgnore && d.DiagnosticCode != nil { + tuple = append(tuple, *d.DiagnosticCode) + } else if d.Policy == DiagnosticDirectivePolicyExpect && d.UnusedExpectDirectiveIndex != nil { tuple = append(tuple, *d.UnusedExpectDirectiveIndex) } return json.MarshalEncode(enc, tuple) @@ -181,9 +195,20 @@ func (d *MappedDiagnosticDirective) UnmarshalJSONFrom(dec *json.Decoder) error { } } if len(tuple) == 6 { - var index int - d.UnusedExpectDirectiveIndex = &index - if err := json.Unmarshal(tuple[5], d.UnusedExpectDirectiveIndex); err != nil { + var field any + switch d.Policy { + case DiagnosticDirectivePolicyIgnore: + var code int32 + d.DiagnosticCode = &code + field = d.DiagnosticCode + case DiagnosticDirectivePolicyExpect: + var index int + d.UnusedExpectDirectiveIndex = &index + field = d.UnusedExpectDirectiveIndex + default: + return fmt.Errorf("invalid diagnostic directive policy %d with sixth tuple element", d.Policy) + } + if err := json.Unmarshal(tuple[5], field); err != nil { return fmt.Errorf("invalid diagnostic directive tuple element 5: %w", err) } } @@ -1094,6 +1119,9 @@ func handshake(ctx context.Context, conn ipc.Conn, diagnosticLocale locale.Local raw, err := conn.Call(ctx, MethodInitialize, InitializeParams{ Locale: diagnosticLocale.String(), PositionEncodings: []PositionEncoding{PositionEncodingUTF8, PositionEncodingUTF16}, + Capabilities: InitializeCapabilities{ + DiagnosticDirectiveCodeSelectors: true, + }, }) if err != nil { return "", "", err @@ -1224,7 +1252,13 @@ func normalizeDiagnosticDirectives(diagnosticDirectives *DiagnosticDirectives, v normalized := ast.MappedDiagnosticDirective{Source: diagnosticSource} switch directive.Policy { case DiagnosticDirectivePolicyIgnore: + if directive.DiagnosticCode != nil && *directive.DiagnosticCode <= 0 { + return nil, directiveError(DiagnosticDirectiveErrorKindInvalidDiagnosticCode) + } normalized.Policy = ast.MappedDiagnosticDirectivePolicyIgnore + if directive.DiagnosticCode != nil { + normalized.DiagnosticCode = *directive.DiagnosticCode + } case DiagnosticDirectivePolicyExpect: unusedDiagnosticIndex := 0 if directive.UnusedExpectDirectiveIndex != nil { From a47bfeb0f5fb25eb3e40fecf1130b58b34d50c70 Mon Sep 17 00:00:00 2001 From: ubugeeei Date: Mon, 16 Nov 2026 01:58:48 +0900 Subject: [PATCH 2/7] fix(content-mapper): preserve selectors in API packets Co-authored-by: miyaoka <1443118+miyaoka@users.noreply.github.com> --- packages/typescript/src/api/node/node.ts | 3 +- packages/typescript/src/api/node/protocol.ts | 2 +- packages/typescript/test/encoder.test.ts | 34 +++++++++++++++ tsc/internal/api/encoder/encoder.go | 7 ++-- tsc/internal/api/encoder/encoder_test.go | 42 +++++++++++++------ tsc/internal/compiler/contentmapper_test.go | 24 +++++++++++ tsc/internal/compiler/fileloader.go | 2 + .../diagnosticMessages.generated.json | 1 + .../diagnostics/diagnosticMessages.json | 4 ++ .../diagnostics/diagnostics_generated.go | 3 ++ 10 files changed, 104 insertions(+), 18 deletions(-) diff --git a/packages/typescript/src/api/node/node.ts b/packages/typescript/src/api/node/node.ts index 77f2c9366fcd8..ccc6e7c211c9d 100644 --- a/packages/typescript/src/api/node/node.ts +++ b/packages/typescript/src/api/node/node.ts @@ -397,7 +397,7 @@ export class RemoteSourceFile extends RemoteNode implements SourceFileInfo { const count = reader.readArrayHeader(); const directives = Array(count); for (let i = 0; i < count; i++) { - if (reader.readArrayHeader() !== 6) throw new Error("Invalid diagnostic directive"); + if (reader.readArrayHeader() !== 7) throw new Error("Invalid diagnostic directive"); const originalStart = reader.readUint(); const originalLength = reader.readUint(); const virtualStart = reader.readUint(); @@ -406,6 +406,7 @@ export class RemoteSourceFile extends RemoteNode implements SourceFileInfo { originalRange: { pos: originalStart, end: originalStart + originalLength }, virtualRange: { pos: virtualStart, end: virtualStart + virtualLength }, policy: reader.readUint(), + diagnosticCode: reader.readUint(), unusedCode: reader.readUint(), }; } diff --git a/packages/typescript/src/api/node/protocol.ts b/packages/typescript/src/api/node/protocol.ts index bf01e29560665..29bb072c5edf2 100644 --- a/packages/typescript/src/api/node/protocol.ts +++ b/packages/typescript/src/api/node/protocol.ts @@ -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; diff --git a/packages/typescript/test/encoder.test.ts b/packages/typescript/test/encoder.test.ts index 4c5f6250704b5..c0546e96da743 100644 --- a/packages/typescript/test/encoder.test.ts +++ b/packages/typescript/test/encoder.test.ts @@ -47,7 +47,9 @@ import { 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, @@ -105,6 +107,38 @@ describe("Encoder", { concurrency }, () => { assert.strictEqual(decoded.canonicalSourceFileName, undefined); }); + test("decodes content mapper diagnostic-code selectors", () => { + const encoded = encodeSourceFile(makeSF("text", "/component.vue.ts", [])); + const encodedView = new DataView(encoded.buffer, encoded.byteOffset, encoded.byteLength); + const nodesOffset = encodedView.getUint32(HEADER_OFFSET_NODES, true); + const directive = Uint8Array.from([ + 0x91, // one directive + 0x97, // seven-element tuple + 0, 1, // original range [0, 1) + 2, 3, // virtual range [2, 5) + 0, // ignore policy + 0xcd, 9, 49, // diagnostic code 2353 + 0, // unused diagnostic code + ]); + const withDirective = new Uint8Array(encoded.length + directive.length); + withDirective.set(encoded.subarray(0, nodesOffset)); + withDirective.set(directive, nodesOffset); + withDirective.set(encoded.subarray(nodesOffset), nodesOffset + directive.length); + const view = new DataView(withDirective.buffer); + const extendedDataOffset = view.getUint32(HEADER_OFFSET_EXTENDED_DATA, true); + view.setUint32(extendedDataOffset + 72, 0, true); + view.setUint32(HEADER_OFFSET_STRUCTURED_DATA, nodesOffset, true); + view.setUint32(HEADER_OFFSET_NODES, nodesOffset + directive.length, true); + + assert.deepStrictEqual(decode(withDirective).diagnosticDirectives, [{ + originalRange: { pos: 0, end: 1 }, + virtualRange: { pos: 2, end: 5 }, + policy: 0, + diagnosticCode: 2353, + unusedCode: 0, + }]); + }); + test("keeps adjacent surrogate string-table entries separate", () => { const high = String.fromCharCode(0xD800); const low = String.fromCharCode(0xDC00); diff --git a/tsc/internal/api/encoder/encoder.go b/tsc/internal/api/encoder/encoder.go index 39367ebc9fae8..89be2c2891a98 100644 --- a/tsc/internal/api/encoder/encoder.go +++ b/tsc/internal/api/encoder/encoder.go @@ -68,7 +68,7 @@ const ( ) const ( - ProtocolVersion uint8 = 9 + ProtocolVersion uint8 = 10 ) // Source File Binary Format @@ -183,7 +183,7 @@ const ( // // Diagnostic directives are msgpack arrays of normalized tuples in UTF-16 coordinates: // -// [originalStart: uint, originalLength: uint, virtualStart: uint, virtualLength: uint, policy: uint, unusedCode: uint] +// [originalStart: uint, originalLength: uint, virtualStart: uint, virtualLength: uint, policy: uint, diagnosticCode: uint, unusedCode: uint] // // An offset of 0xFFFFFFFF indicates no data (empty array). // @@ -850,7 +850,7 @@ func encodeDiagnosticDirectives(directives []ast.MappedDiagnosticDirective, virt offset := uint32(len(*buf)) *buf = msgpackWriteArrayHeader(*buf, len(directives)) for _, directive := range directives { - *buf = msgpackWriteArrayHeader(*buf, 6) + *buf = msgpackWriteArrayHeader(*buf, 7) originalStart := originalPositions.UTF8ToUTF16(directive.OriginalRange.Pos()) originalEnd := originalPositions.UTF8ToUTF16(directive.OriginalRange.End()) virtualStart := virtualPositions.UTF8ToUTF16(directive.VirtualRange.Pos()) @@ -860,6 +860,7 @@ func encodeDiagnosticDirectives(directives []ast.MappedDiagnosticDirective, virt *buf = msgpackWriteUint(*buf, uint32(virtualStart)) *buf = msgpackWriteUint(*buf, uint32(virtualEnd-virtualStart)) *buf = msgpackWriteUint(*buf, uint32(directive.Policy)) + *buf = msgpackWriteUint(*buf, uint32(directive.DiagnosticCode)) *buf = msgpackWriteUint(*buf, uint32(directive.UnusedCode)) } return offset diff --git a/tsc/internal/api/encoder/encoder_test.go b/tsc/internal/api/encoder/encoder_test.go index 2652a44212051..38558b5814328 100644 --- a/tsc/internal/api/encoder/encoder_test.go +++ b/tsc/internal/api/encoder/encoder_test.go @@ -39,8 +39,8 @@ func TestEncodeSourceFile(t *testing.T) { func TestEncodeContentMapperSourceFileMetadata(t *testing.T) { t.Parallel() - if encoder.ProtocolVersion != 9 { - t.Fatalf("protocol version = %d, want 9", encoder.ProtocolVersion) + if encoder.ProtocolVersion != 10 { + t.Fatalf("protocol version = %d, want 10", encoder.ProtocolVersion) } sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ FileName: "/component.vue", @@ -50,14 +50,23 @@ func TestEncodeContentMapperSourceFileMetadata(t *testing.T) { OriginalText: "đŸ˜€original", ContentMapper: "mapper@1.0.0", VirtualFileName: "/component.vue.ts", - DiagnosticDirectives: []ast.MappedDiagnosticDirective{{ - OriginalRange: core.NewTextRange(4, 5), - VirtualRange: core.NewTextRange(4, 11), - Policy: ast.MappedDiagnosticDirectivePolicyExpect, - UnusedCode: 2578, - UnusedMessageText: "Unused framework directive.", - Source: "mapper", - }}, + DiagnosticDirectives: []ast.MappedDiagnosticDirective{ + { + OriginalRange: core.NewTextRange(4, 5), + VirtualRange: core.NewTextRange(4, 11), + Policy: ast.MappedDiagnosticDirectivePolicyIgnore, + DiagnosticCode: 2353, + Source: "mapper", + }, + { + OriginalRange: core.NewTextRange(4, 5), + VirtualRange: core.NewTextRange(4, 11), + Policy: ast.MappedDiagnosticDirectivePolicyExpect, + UnusedCode: 2578, + UnusedMessageText: "Unused framework directive.", + Source: "mapper", + }, + }, }) buf, _, err := encoder.EncodeSourceFile(sourceFile) @@ -75,12 +84,19 @@ func TestEncodeContentMapperSourceFileMetadata(t *testing.T) { assert.Equal(t, encodedString(buf, virtualFileNameIndex), "/component.vue.ts") structuredDataOffset := readUint32(buf, encoder.HeaderOffsetStructuredData) directiveOffset := structuredDataOffset + diagnosticDirectivesOffset - assert.DeepEqual(t, buf[directiveOffset:directiveOffset+10], []byte{ - 0x91, // one directive - 0x96, // six-element tuple + assert.DeepEqual(t, buf[directiveOffset:directiveOffset+21], []byte{ + 0x92, // two directives + 0x97, // seven-element selected-ignore tuple + 2, 1, // original range [2, 3) in UTF-16 + 2, 7, // virtual range [2, 9) in UTF-16 + 0, // ignore policy + 0xcd, 9, 49, // diagnostic code 2353 + 0, // unused diagnostic code + 0x97, // seven-element expect tuple 2, 1, // original range [2, 3) in UTF-16 2, 7, // virtual range [2, 9) in UTF-16 1, // expect policy + 0, // no diagnostic-code selector 0xcd, 10, 18, // unused diagnostic code 2578 }) } diff --git a/tsc/internal/compiler/contentmapper_test.go b/tsc/internal/compiler/contentmapper_test.go index ce0e8a444037e..53c5bd4f69da9 100644 --- a/tsc/internal/compiler/contentmapper_test.go +++ b/tsc/internal/compiler/contentmapper_test.go @@ -1046,6 +1046,30 @@ func TestContentMapperSourceFileState(t *testing.T) { }) assert.Assert(t, found, "expected a localized project response diagnostic, got: %v", programDiagnostics) }) + + t.Run("invalid diagnostic code is localized", func(t *testing.T) { + t.Parallel() + program := newContentMapperProgram(t, fakeContentMapperHost{ + transform: func(fileName string, content string) (contentmapper.Result, error) { + return contentmapper.Result{}, contentmapper.NewTransformError( + contentmapper.TransformErrorKindResponse, + &contentmapper.DiagnosticDirectiveError{ + Kind: contentmapper.DiagnosticDirectiveErrorKindInvalidDiagnosticCode, + Index: 2, + SupplementalIndex: -1, + }, + ) + }, + }, map[string]string{"/src/fail.vue": "original"}, []string{"/src/fail.vue"}) + programDiagnostics := collectContentMapperDiagnostics(program) + found := slices.ContainsFunc(programDiagnostics, func(diagnostic *ast.Diagnostic) bool { + return slices.ContainsFunc(diagnostic.MessageChain(), func(message *ast.Diagnostic) bool { + return message.Code() == diagnostics.Diagnostic_directive_0_returned_by_the_content_mapper_has_a_diagnostic_code_selector_that_must_be_positive.Code() && + message.String() == "Diagnostic directive 2 returned by the content mapper has a diagnostic code selector that must be positive." + }) + }) + assert.Assert(t, found, "expected an invalid diagnostic-code selector diagnostic, got: %v", programDiagnostics) + }) } func TestContentMapperProjectErrorDiagnostics(t *testing.T) { diff --git a/tsc/internal/compiler/fileloader.go b/tsc/internal/compiler/fileloader.go index 901ecd67b0ef5..d586c3a7e423f 100644 --- a/tsc/internal/compiler/fileloader.go +++ b/tsc/internal/compiler/fileloader.go @@ -516,6 +516,8 @@ func contentMapperTransformDiagnostic(file *ast.SourceFile, label string, err er detail = ast.NewCompilerDiagnostic(diagnostics.Diagnostic_directive_0_returned_by_the_content_mapper_must_specify_unusedExpectDirectiveIndex_when_there_is_not_exactly_one_unusedExpectDirectiveDiagnostics_entry, directiveError.Index) case contentmapper.DiagnosticDirectiveErrorKindInvalidUnusedDiagnosticIndex: detail = ast.NewCompilerDiagnostic(diagnostics.Diagnostic_directive_0_returned_by_the_content_mapper_has_an_invalid_unusedExpectDirectiveIndex, directiveError.Index) + case contentmapper.DiagnosticDirectiveErrorKindInvalidDiagnosticCode: + detail = ast.NewCompilerDiagnostic(diagnostics.Diagnostic_directive_0_returned_by_the_content_mapper_has_a_diagnostic_code_selector_that_must_be_positive, directiveError.Index) case contentmapper.DiagnosticDirectiveErrorKindOverlap: detail = ast.NewCompilerDiagnostic(diagnostics.The_content_mapper_returned_diagnostic_directives_with_overlapping_virtual_ranges) } diff --git a/tsc/internal/diagnostics/diagnosticMessages.generated.json b/tsc/internal/diagnostics/diagnosticMessages.generated.json index 300558dd1c833..3caf0e8f6eca2 100644 --- a/tsc/internal/diagnostics/diagnosticMessages.generated.json +++ b/tsc/internal/diagnostics/diagnosticMessages.generated.json @@ -1975,6 +1975,7 @@ "This_relative_import_path_is_unsafe_to_rewrite_because_it_appears_not_to_match_the_content_mapper_s__18139": "This relative import path is unsafe to rewrite because it appears not to match the content mapper's 'include', but does after resolution with 'rootDirs' and 'moduleSuffixes'.", "File_0_has_a_registered_content_mapper_extension_but_does_not_match_any_content_mapper_s_include_18140": "File '{0}' has a registered content mapper extension but does not match any content mapper's 'include'.", "Content_mapper_0_must_specify_extensions_in_tsconfig_or_its_package_manifest_18141": "Content mapper '{0}' must specify 'extensions' in tsconfig or its package manifest.", + "Diagnostic_directive_0_returned_by_the_content_mapper_has_a_diagnostic_code_selector_that_must_be_po_18142": "Diagnostic directive {0} returned by the content mapper has a diagnostic code selector that must be positive.", "nodenext_if_module_is_nodenext_node16_if_module_is_node16_or_node18_otherwise_bundler_69010": "`nodenext` if `module` is `nodenext`; `node16` if `module` is `node16` or `node18`; otherwise, `bundler`.", "File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module_80001": "File is a CommonJS module; it may be converted to an ES module.", "This_constructor_function_may_be_converted_to_a_class_declaration_80002": "This constructor function may be converted to a class declaration.", diff --git a/tsc/internal/diagnostics/diagnosticMessages.json b/tsc/internal/diagnostics/diagnosticMessages.json index ecf6595ffd641..3018757744dd7 100644 --- a/tsc/internal/diagnostics/diagnosticMessages.json +++ b/tsc/internal/diagnostics/diagnosticMessages.json @@ -8968,6 +8968,10 @@ "category": "Error", "code": 18141 }, + "Diagnostic directive {0} returned by the content mapper has a diagnostic code selector that must be positive.": { + "category": "Message", + "code": 18142 + }, "Content mapper '{0}' is also registered for the extension '{1}', but has a conflicting 'outputExtensions' mapping. Each registration for this extension must produce the same output file name.": { "category": "Error", "code": 18127 diff --git a/tsc/internal/diagnostics/diagnostics_generated.go b/tsc/internal/diagnostics/diagnostics_generated.go index 0d016471319dd..29f63724bae0a 100644 --- a/tsc/internal/diagnostics/diagnostics_generated.go +++ b/tsc/internal/diagnostics/diagnostics_generated.go @@ -3954,6 +3954,8 @@ var File_0_has_a_registered_content_mapper_extension_but_does_not_match_any_cont var Content_mapper_0_must_specify_extensions_in_tsconfig_or_its_package_manifest = &Message{code: 18141, category: CategoryError, key: "Content_mapper_0_must_specify_extensions_in_tsconfig_or_its_package_manifest_18141", text: "Content mapper '{0}' must specify 'extensions' in tsconfig or its package manifest."} +var Diagnostic_directive_0_returned_by_the_content_mapper_has_a_diagnostic_code_selector_that_must_be_positive = &Message{code: 18142, category: CategoryMessage, key: "Diagnostic_directive_0_returned_by_the_content_mapper_has_a_diagnostic_code_selector_that_must_be_po_18142", text: "Diagnostic directive {0} returned by the content mapper has a diagnostic code selector that must be positive."} + var X_nodenext_if_module_is_nodenext_node16_if_module_is_node16_or_node18_otherwise_bundler = &Message{code: 69010, category: CategoryMessage, key: "nodenext_if_module_is_nodenext_node16_if_module_is_node16_or_node18_otherwise_bundler_69010", text: "`nodenext` if `module` is `nodenext`; `node16` if `module` is `node16` or `node18`; otherwise, `bundler`."} var File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module = &Message{code: 80001, category: CategorySuggestion, key: "File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module_80001", text: "File is a CommonJS module; it may be converted to an ES module."} @@ -6471,6 +6473,7 @@ var allMessages = [...]**Message{ &This_relative_import_path_is_unsafe_to_rewrite_because_it_appears_not_to_match_the_content_mapper_s_include_but_does_after_resolution_with_rootDirs_and_moduleSuffixes, &File_0_has_a_registered_content_mapper_extension_but_does_not_match_any_content_mapper_s_include, &Content_mapper_0_must_specify_extensions_in_tsconfig_or_its_package_manifest, + &Diagnostic_directive_0_returned_by_the_content_mapper_has_a_diagnostic_code_selector_that_must_be_positive, &X_nodenext_if_module_is_nodenext_node16_if_module_is_node16_or_node18_otherwise_bundler, &File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module, &This_constructor_function_may_be_converted_to_a_class_declaration, From d92ceeb7c624f61fd6faa4248658d5b72829aa20 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 20 Nov 2026 14:57:48 -0800 Subject: [PATCH 3/7] Use discriminated union of tuples, ignore accepting variadic codes --- .../schemas/contentmapper.schema.json | 140 ++++++----- packages/typescript/src/api/node/msgpack.ts | 100 ++++++++ packages/typescript/src/api/node/node.ts | 15 +- packages/typescript/src/ast/ast.ts | 3 +- packages/typescript/test/async/api.test.ts | 25 +- packages/typescript/test/encoder.test.ts | 46 +++- packages/typescript/test/sync/api.test.ts | 25 +- packages/typescript/test/wtf8.test.ts | 39 +++- tools/scripts/tsc/generate-contentmapper.ts | 219 ++++++++++++------ tsc/internal/api/encoder/encoder.go | 7 +- tsc/internal/api/encoder/encoder_test.go | 36 ++- tsc/internal/ast/ast.go | 11 +- tsc/internal/compiler/contentmapper_test.go | 68 ++++-- tsc/internal/compiler/program.go | 2 +- tsc/internal/contentmapper/host_test.go | 85 ++++--- tsc/internal/contentmapper/hostimpl.go | 13 +- .../contentmapper/protocol_generated.go | 74 +++--- 17 files changed, 637 insertions(+), 271 deletions(-) diff --git a/packages/typescript/schemas/contentmapper.schema.json b/packages/typescript/schemas/contentmapper.schema.json index 483b87e3ab2ae..ab49e00666e5e 100644 --- a/packages/typescript/schemas/contentmapper.schema.json +++ b/packages/typescript/schemas/contentmapper.schema.json @@ -51,24 +51,10 @@ "$ref": "#/definitions/PositionEncoding" }, "description": "Coordinate spaces accepted by the host." - }, - "capabilities": { - "$ref": "#/definitions/InitializeCapabilities", - "description": "Optional protocol extensions supported by the host. Absent in older hosts." } }, "required": ["positionEncodings"] }, - "InitializeCapabilities": { - "type": "object", - "description": "Optional protocol extensions supported by the host.", - "properties": { - "diagnosticDirectiveCodeSelectors": { - "type": "boolean", - "description": "Ignore directives may select TypeScript diagnostic codes." - } - } - }, "InitializeResult": { "type": "object", "description": "Response to the initialize request.", @@ -424,54 +410,94 @@ "required": ["unusedExpectDirectiveDiagnostics", "directives"] }, "MappedDiagnosticDirective": { - "type": "array", - "description": "[originalStart, originalLength, virtualStart, virtualEnd, policy, policyValue?]. For expect directives, policyValue is an unused-expect diagnostic index; an omitted index selects the only shared diagnostic and requires exactly one. For ignore directives, policyValue is an optional TypeScript diagnostic code. Expect directives require a valid original range.", "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. When diagnostic codes are given, only matching diagnostics are suppressed.", + "items": [ + { + "type": "integer", + "title": "originalStart", + "description": "Original start offset. It is ignored if the original range is invalid." + }, + { + "type": "integer", + "title": "originalLength", + "description": "Original span length. It is ignored 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." + }, + { + "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 for expect directives, or a positive TypeScript diagnostic code for ignore 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", + "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": 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 }] - }, - "else": { - "items": [{}, {}, {}, {}, {}, { "minimum": 1, "maximum": 2147483647 }] - } + ] }, "SupplementalOutput": { "$ref": "#/definitions/MappedOutput" diff --git a/packages/typescript/src/api/node/msgpack.ts b/packages/typescript/src/api/node/msgpack.ts index b5da5f0e1890b..89307d7700365 100644 --- a/packages/typescript/src/api/node/msgpack.ts +++ b/packages/typescript/src/api/node/msgpack.ts @@ -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; diff --git a/packages/typescript/src/api/node/node.ts b/packages/typescript/src/api/node/node.ts index ccc6e7c211c9d..372440b915109 100644 --- a/packages/typescript/src/api/node/node.ts +++ b/packages/typescript/src/api/node/node.ts @@ -397,17 +397,24 @@ export class RemoteSourceFile extends RemoteNode implements SourceFileInfo { const count = reader.readArrayHeader(); const directives = Array(count); for (let i = 0; i < count; i++) { - if (reader.readArrayHeader() !== 7) 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(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(), - diagnosticCode: reader.readUint(), - unusedCode: reader.readUint(), + policy, + unusedCode, + diagnosticCodes, }; } return this._cachedDiagnosticDirectives = directives; diff --git a/packages/typescript/src/ast/ast.ts b/packages/typescript/src/ast/ast.ts index 5b5c6b3ebfb36..7f587085ef92f 100644 --- a/packages/typescript/src/ast/ast.ts +++ b/packages/typescript/src/ast/ast.ts @@ -175,8 +175,9 @@ export interface MappedDiagnosticDirective { readonly originalRange: ReadonlyTextRange; readonly virtualRange: ReadonlyTextRange; readonly policy: DiagnosticDirectivePolicy; - readonly diagnosticCode: number; readonly unusedCode: number; + /** For ignore directives, an empty list selects all diagnostic codes. */ + readonly diagnosticCodes: readonly number[]; } export interface SourceFile extends Node { diff --git a/packages/typescript/test/async/api.test.ts b/packages/typescript/test/async/api.test.ts index 582672974ff4f..99bc92dcb09b6 100644 --- a/packages/typescript/test/async/api.test.ts +++ b/packages/typescript/test/async/api.test.ts @@ -3111,13 +3111,19 @@ 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-")); t.after(() => rmSync(directory, { recursive: true, force: true })); const mapperDirectory = join(directory, "node_modules", "mapper"); mkdirSync(mapperDirectory, { recursive: true }); + const virtualText = ` + export {}; + const object: { known: string } = { extra: 1 }; + const value: string = 1; + const missing = missingName; + `; const mapperScript = ` import { createMessageConnection } from ${JSON.stringify(new URL("../../vendor/vscode-jsonrpc/lib/node/main.js", import.meta.url).href)}; const connection = createMessageConnection(process.stdin, process.stdout); @@ -3125,8 +3131,12 @@ export const other: { value: string } = box;`, connection.onRequest("openProject", () => ({})); connection.onRequest("closeProject", () => null); connection.onRequest("transform", ({ content }) => ({ - text: "export {};", + text: ${JSON.stringify(virtualText)}, extension: ".ts", + diagnosticDirectives: { + unusedExpectDirectiveDiagnostics: [], + directives: [[0, 0, 0, ${virtualText.length}, 0, 2353, 2322]], + }, supplemental: [{ text: content, extension: ".ts" }], })); connection.listen(); @@ -3153,6 +3163,17 @@ 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: virtualText.length }, + policy: 0, + unusedCode: 0, + diagnosticCodes: [2353, 2322], + }]); + assert.deepEqual((await program.getSemanticDiagnostics(mappedFileName)).map(diagnostic => diagnostic.code), [2304]); const supplementalFileName = toRootedFilePath(join(directory, "app.box.0.ts"), undefined); assert.equal(existsSync(supplementalFileName), false); assert.ok(await program.getSourceFile(supplementalFileName)); diff --git a/packages/typescript/test/encoder.test.ts b/packages/typescript/test/encoder.test.ts index aa5e492bb1069..240f9d2cc15aa 100644 --- a/packages/typescript/test/encoder.test.ts +++ b/packages/typescript/test/encoder.test.ts @@ -41,6 +41,7 @@ import { encodeSourceFile, uint8ArrayToBase64, } from "../src/api/node/encoder.ts"; +import { MsgpackWriter } from "../src/api/node/msgpack.ts"; import { RemoteNode, RemoteNodeList, @@ -118,19 +119,28 @@ describe("Encoder", { concurrency }, () => { assert.strictEqual(decoded.canonicalSourceFileName, undefined); }); - test("decodes content mapper diagnostic-code selectors", () => { + test("decodes extensible content mapper diagnostic directives", () => { const encoded = encodeSourceFile(makeSF("text", "/component.vue.ts", [])); const encodedView = new DataView(encoded.buffer, encoded.byteOffset, encoded.byteLength); const nodesOffset = encodedView.getUint32(HEADER_OFFSET_NODES, true); - const directive = Uint8Array.from([ - 0x91, // one directive - 0x97, // seven-element tuple - 0, 1, // original range [0, 1) - 2, 3, // virtual range [2, 5) - 0, // ignore policy - 0xcd, 9, 49, // diagnostic code 2353 - 0, // unused diagnostic code - ]); + const writer = new MsgpackWriter(); + writer.writeArrayHeader(3); + writer.writeArrayHeader(8); + for (const value of [0, 1, 2, 3, 0, 0]) writer.writeUint(value); + writer.writeArrayHeader(2); + for (const code of [2353, 2322]) writer.writeUint(code); + writer.writeArrayHeader(2); // An unknown future field. + writer.writeBool(true); + writer.writeArrayHeader(1); + writer.writeUint(42); + writer.writeArrayHeader(7); + for (const value of [1, 1, 0, 1, 1, 2578]) writer.writeUint(value); + writer.writeArrayHeader(0); + writer.writeArrayHeader(7); + for (const value of [0, 1, 0, 1, 0, 0]) writer.writeUint(value); + writer.writeArrayHeader(16); + for (let code = 1; code <= 16; code++) writer.writeUint(code); + const directive = writer.finish(); const withDirective = new Uint8Array(encoded.length + directive.length); withDirective.set(encoded.subarray(0, nodesOffset)); withDirective.set(directive, nodesOffset); @@ -145,9 +155,23 @@ describe("Encoder", { concurrency }, () => { originalRange: { pos: 0, end: 1 }, virtualRange: { pos: 2, end: 5 }, policy: 0, - diagnosticCode: 2353, unusedCode: 0, + diagnosticCodes: [2353, 2322], + }, { + originalRange: { pos: 1, end: 2 }, + virtualRange: { pos: 0, end: 1 }, + policy: 1, + unusedCode: 2578, + diagnosticCodes: [], + }, { + originalRange: { pos: 0, end: 1 }, + virtualRange: { pos: 0, end: 1 }, + policy: 0, + unusedCode: 0, + diagnosticCodes: Array.from({ length: 16 }, (_, i) => i + 1), }]); + withDirective[nodesOffset + 1] = 0x96; + assert.throws(() => decode(withDirective).diagnosticDirectives, /Invalid diagnostic directive/); }); test("keeps adjacent surrogate string-table entries separate", () => { diff --git a/packages/typescript/test/sync/api.test.ts b/packages/typescript/test/sync/api.test.ts index caeabc14aa17e..de6ee08661173 100644 --- a/packages/typescript/test/sync/api.test.ts +++ b/packages/typescript/test/sync/api.test.ts @@ -2927,13 +2927,19 @@ 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, }, t => { const directory = mkdtempSync(join(tmpdir(), "ts-project-membership-")); t.after(() => rmSync(directory, { recursive: true, force: true })); const mapperDirectory = join(directory, "node_modules", "mapper"); mkdirSync(mapperDirectory, { recursive: true }); + const virtualText = ` + export {}; + const object: { known: string } = { extra: 1 }; + const value: string = 1; + const missing = missingName; + `; const mapperScript = ` import { createMessageConnection } from ${JSON.stringify(new URL("../../vendor/vscode-jsonrpc/lib/node/main.js", import.meta.url).href)}; const connection = createMessageConnection(process.stdin, process.stdout); @@ -2941,8 +2947,12 @@ export const other: { value: string } = box;`, connection.onRequest("openProject", () => ({})); connection.onRequest("closeProject", () => null); connection.onRequest("transform", ({ content }) => ({ - text: "export {};", + text: ${JSON.stringify(virtualText)}, extension: ".ts", + diagnosticDirectives: { + unusedExpectDirectiveDiagnostics: [], + directives: [[0, 0, 0, ${virtualText.length}, 0, 2353, 2322]], + }, supplemental: [{ text: content, extension: ".ts" }], })); connection.listen(); @@ -2969,6 +2979,17 @@ export const other: { value: string } = box;`, using api = new API({ cwd: directory, runExternalCode: true }); const snapshot = api.createSnapshot({ openProjects: [configFileName] }); const program = snapshot.getConfiguredProject(configFileName)!.program; + const mappedFileName = toRootedFilePath(join(directory, "app.box"), undefined); + const mappedFile = program.getSourceFile(mappedFileName); + assert.ok(mappedFile); + assert.deepEqual(mappedFile.diagnosticDirectives, [{ + originalRange: { pos: 0, end: 0 }, + virtualRange: { pos: 0, end: virtualText.length }, + policy: 0, + unusedCode: 0, + diagnosticCodes: [2353, 2322], + }]); + assert.deepEqual((program.getSemanticDiagnostics(mappedFileName)).map(diagnostic => diagnostic.code), [2304]); const supplementalFileName = toRootedFilePath(join(directory, "app.box.0.ts"), undefined); assert.equal(existsSync(supplementalFileName), false); assert.ok(program.getSourceFile(supplementalFileName)); diff --git a/packages/typescript/test/wtf8.test.ts b/packages/typescript/test/wtf8.test.ts index 94480e2fd1c1a..e29e2bb8bd580 100644 --- a/packages/typescript/test/wtf8.test.ts +++ b/packages/typescript/test/wtf8.test.ts @@ -89,11 +89,48 @@ describe("Wtf8Decoder", { concurrency }, () => { }); }); -describe("Msgpack strings", { concurrency }, () => { +describe("Msgpack", { concurrency }, () => { test("round-trip lone surrogates", () => { const text = `high:${String.fromCharCode(0xD800)} low:${String.fromCharCode(0xDFFF)}`; const writer = new MsgpackWriter(); writer.writeString(text); assert.equal(new MsgpackReader(writer.finish()).readString(), text); }); + + test("skips values without consuming the next value", () => { + const values = [ + [0xc0], + [0xff], + [0xcd, 0, 1], + [0xd3, 0, 0, 0, 0, 0, 0, 0, 1], + [0xca, 0, 0, 0, 0], + [0xa1, 0x61], + [0xd9, 1, 0x61], + [0xda, 0, 1, 0x61], + [0xdb, 0, 0, 0, 1, 0x61], + [0xc4, 1, 0xff], + [0xc5, 0, 1, 0xff], + [0xc6, 0, 0, 0, 1, 0xff], + [0xd4, 0, 0xff], + [0xc7, 1, 0, 0xff], + [0xc8, 0, 1, 0, 0xff], + [0xc9, 0, 0, 0, 1, 0, 0xff], + [0x92, 0xc3, 0x81, 0xa1, 0x6b, 0xc0], + [0xdc, 0, 1, 0xc0], + [0xdd, 0, 0, 0, 1, 0xc0], + [0xde, 0, 1, 0, 1], + [0xdf, 0, 0, 0, 1, 0, 1], + ]; + for (const value of values) { + const reader = new MsgpackReader(Uint8Array.from([...value, 42])); + reader.skipValue(); + assert.strictEqual(reader.readUint(), 42, `after ${value.map(byte => byte.toString(16)).join(" ")}`); + } + }); + + test("rejects invalid or truncated values while skipping", () => { + for (const data of [[0xc1], [0xc4, 2, 1], [0x91], [0xd9]]) { + assert.throws(() => new MsgpackReader(Uint8Array.from(data)).skipValue()); + } + }); }); diff --git a/tools/scripts/tsc/generate-contentmapper.ts b/tools/scripts/tsc/generate-contentmapper.ts index 62685cfb86294..471063afd07e0 100644 --- a/tools/scripts/tsc/generate-contentmapper.ts +++ b/tools/scripts/tsc/generate-contentmapper.ts @@ -21,6 +21,7 @@ interface Schema { properties?: Record; required?: string[]; items?: Schema | Schema[]; + additionalItems?: Schema | boolean; anyOf?: Schema[]; oneOf?: Schema[]; allOf?: Schema[]; @@ -210,8 +211,6 @@ function goType(schema: Schema, definitions: Record): string { return "string"; case "integer": return schema.minimum === -2147483648 && schema.maximum === 2147483647 ? "int32" : "int"; - case "boolean": - return "bool"; case "array": assert(schema.items && !Array.isArray(schema.items), "Unsupported array items"); return `[]${goType(schema.items, definitions)}`; @@ -239,6 +238,53 @@ function fieldDescription(name: string, schema: Schema, definitions: Record(); + for (const variant of schema.oneOf) { + const items = variant.items; + if (!Array.isArray(items)) throw new Error("Unsupported MappedDiagnosticDirective policy variant"); + assert( + variant.type === "array" && items.length >= 5 + && variant.minItems === 5 && items[4].title === "policy" + && (items[4].const === 0 || items[4].const === 1), + "Unsupported MappedDiagnosticDirective policy variant", + ); + assert(!variants.has(items[4].const), "Duplicate MappedDiagnosticDirective policy variant"); + variants.set(items[4].const, variant); + } + const ignore = variants.get(0); + const expect = variants.get(1); + assert(ignore && expect, "MappedDiagnosticDirective must define ignore and expect variants"); + assert( + typeof ignore.additionalItems === "object" && ignore.additionalItems.type === "integer" + && ignore.additionalItems.minimum === 1 && ignore.additionalItems.maximum === 2147483647 + && ignore.maxItems === undefined, + "Unsupported MappedDiagnosticDirective ignore variant", + ); + const expectItems = expect.items; + const expectMaxItems = expect.maxItems; + if (!Array.isArray(expectItems)) throw new Error("Unsupported MappedDiagnosticDirective expect variant"); + assert( + expectItems.length === 6 && expectItems[5].title === "unusedExpectDirectiveIndex" + && expectItems[5].type === "integer" && expectItems[5].minimum === 0 + && expectMaxItems === 6, + "Unsupported MappedDiagnosticDirective expect variant", + ); + for (const variant of [ignore, expect]) { + const items = variant.items; + assert(Array.isArray(items), "Unsupported MappedDiagnosticDirective tuple items"); + assert( + items.slice(0, 4).every((item, index) => + item.title === ["originalStart", "originalLength", "virtualStart", "virtualEnd"][index] + && item.type === "integer" + ), + "Unsupported MappedDiagnosticDirective range fields", + ); + } + return { ignore, expect, expectMaxItems }; +} + function typeDescription(name: string, text: string | undefined): string[] { if (!text) return []; const normalized = text.trim().replace(/\s+/g, " "); @@ -345,82 +391,101 @@ export function generateContentMapper(document: SchemaDocument): string { for (const value of schema.enum) lines.push(`\t${name}${names[String(value)]} ${name} = ${JSON.stringify(value)}`); lines.push(")"); } - else if (Array.isArray(schema.items)) { - assert(schema.type === "array" && schema.minItems === 5 && schema.maxItems === 6 && schema.items.length === 6, `Unsupported ${name} tuple`); - if (name === "SpanMapSegment") { - spanMapTuple(document); - lines.push(`type ${name} = spanmap.SegmentTuple`); - } - else if (schema.items.every(item => item.title)) { - const names = schema.items.map(item => goName(item.title!)); - assert(name === "MappedDiagnosticDirective" && names[4] === "Policy" && names[5] === "UnusedExpectDirectiveIndex", `Unsupported ${name} tuple fields`); - assert(schema.title, `Missing ${name} tuple title`); - const label = schema.title[0].toLowerCase() + schema.title.slice(1); - assert(schema.maxItems - schema.minItems === 1, `Unsupported ${name} tuple optional fields`); - assert(schema.items.every((item, index) => index === 4 ? item.$ref === "#/definitions/DiagnosticDirectivePolicy" : item.type === "integer"), `Unsupported ${name} tuple items`); - lines.push(`type ${name} struct {`); - for (const [index, item] of schema.items.entries()) { - const field = names[index]; - lines.push(...fieldDescription(field, item, definitions).map(line => `\t${line}`)); - lines.push(`\t${field} ${index >= schema.minItems ? "*" : ""}${goType(item, definitions)}`); - } - lines.push("\t// DiagnosticCode selects one TypeScript diagnostic code for an ignore directive.", "\tDiagnosticCode *int32"); - lines.push( - "}", - "", - "var (", - `\t_ json.MarshalerTo = ${name}{}`, - `\t_ json.UnmarshalerFrom = (*${name})(nil)`, - ")", - "", - `func (d ${name}) MarshalJSONTo(enc *json.Encoder) error {`, - `\ttuple := []any{${names.slice(0, schema.minItems).map(field => `d.${field}`).join(", ")}}`, - "\tif d.Policy == DiagnosticDirectivePolicyIgnore && d.DiagnosticCode != nil {", - "\t\ttuple = append(tuple, *d.DiagnosticCode)", - `\t} else if d.Policy == DiagnosticDirectivePolicyExpect && d.${names[schema.minItems]} != nil {`, - `\t\ttuple = append(tuple, *d.${names[schema.minItems]})`, - "\t}", - "\treturn json.MarshalEncode(enc, tuple)", - "}", - "", - `func (d *${name}) UnmarshalJSONFrom(dec *json.Decoder) error {`, - "\tvar tuple []json.Value", - "\tif err := json.UnmarshalDecode(dec, &tuple); err != nil {", - "\t\treturn err", - "\t}", - `\tif len(tuple) != ${schema.minItems} && len(tuple) != ${schema.maxItems} {`, - `\t\treturn fmt.Errorf(${JSON.stringify(`${label} tuple must contain ${schema.minItems} or ${schema.maxItems} elements, got %d`)}, len(tuple))`, - "\t}", - `\t*d = ${name}{}`, - `\tfields := []any{${names.slice(0, schema.minItems).map(field => `&d.${field}`).join(", ")}}`, - "\tfor i, field := range fields {", - "\t\tif err := json.Unmarshal(tuple[i], field); err != nil {", - `\t\t\treturn fmt.Errorf(${JSON.stringify(`invalid ${label} tuple element %d: %w`)}, i, err)`, - "\t\t}", - "\t}", - `\tif len(tuple) == ${schema.maxItems} {`, - "\t\tvar field any", - "\t\tswitch d.Policy {", - "\t\tcase DiagnosticDirectivePolicyIgnore:", - "\t\t\td.DiagnosticCode = new(int32)", - "\t\t\tfield = d.DiagnosticCode", - "\t\tcase DiagnosticDirectivePolicyExpect:", - `\t\t\td.${names[schema.minItems]} = new(${goType(schema.items[schema.minItems], definitions)})`, - `\t\t\tfield = d.${names[schema.minItems]}`, - "\t\tdefault:", - '\t\t\treturn fmt.Errorf("invalid diagnostic directive policy %d with sixth tuple element", d.Policy)', - "\t\t}", - `\t\tif err := json.Unmarshal(tuple[${schema.minItems}], field); err != nil {`, - `\t\t\treturn fmt.Errorf(${JSON.stringify(`invalid ${label} tuple element ${schema.minItems}: %w`)}, err)`, - "\t\t}", - "\t}", - "\treturn nil", - "}", - ); - } - else { - throw new Error(`Unsupported ${name} tuple`); + else if (name === "MappedDiagnosticDirective" && schema.oneOf) { + const { ignore, expect, expectMaxItems } = mappedDiagnosticDirectiveVariants(schema); + assert(schema.title, "Missing MappedDiagnosticDirective title"); + const ignoreItems = ignore.items as Schema[]; + const expectItems = expect.items as Schema[]; + const policyNames = enumNames.DiagnosticDirectivePolicy; + const ignorePolicy = `DiagnosticDirectivePolicy${policyNames[String(ignoreItems[4].const)]}`; + const expectPolicy = `DiagnosticDirectivePolicy${policyNames[String(expectItems[4].const)]}`; + const fields = expectItems.slice(0, 4); + assert( + fields.every((field, index) => field.title === ["originalStart", "originalLength", "virtualStart", "virtualEnd"][index]), + "Unsupported MappedDiagnosticDirective fields", + ); + lines.push("type MappedDiagnosticDirective struct {"); + for (const field of fields) { + const name = goName(field.title!); + lines.push(...fieldDescription(name, field, definitions).map(line => `\t${line}`)); + lines.push(`\t${name} ${goType(field, definitions)}`); } + lines.push( + "\t// Policy is the diagnostic directive policy: 0 ignores diagnostics; 1 expects a diagnostic.", + "\tPolicy DiagnosticDirectivePolicy", + "\t// UnusedExpectDirectiveIndex selects the unused diagnostic for an expect directive.", + "\tUnusedExpectDirectiveIndex *int", + "\t// DiagnosticCodes selects TypeScript diagnostic codes for an ignore directive; empty selects all codes.", + "\tDiagnosticCodes []int32", + "}", + "", + "var (", + "\t_ json.MarshalerTo = MappedDiagnosticDirective{}", + "\t_ json.UnmarshalerFrom = (*MappedDiagnosticDirective)(nil)", + ")", + "", + "func (d MappedDiagnosticDirective) MarshalJSONTo(enc *json.Encoder) error {", + "\ttuple := []any{d.OriginalStart, d.OriginalLength, d.VirtualStart, d.VirtualEnd, d.Policy}", + `\tswitch d.Policy {`, + `\tcase ${ignorePolicy}:`, + "\t\tfor _, code := range d.DiagnosticCodes {", + "\t\t\ttuple = append(tuple, code)", + "\t\t}", + `\tcase ${expectPolicy}:`, + "\t\tif d.UnusedExpectDirectiveIndex != nil {", + "\t\t\ttuple = append(tuple, *d.UnusedExpectDirectiveIndex)", + "\t\t}", + "\t}", + "\treturn json.MarshalEncode(enc, tuple)", + "}", + "", + "func (d *MappedDiagnosticDirective) UnmarshalJSONFrom(dec *json.Decoder) error {", + "\tvar tuple []json.Value", + "\tif err := json.UnmarshalDecode(dec, &tuple); err != nil {", + "\t\treturn err", + "\t}", + `\tif len(tuple) < ${ignore.minItems} {`, + `\t\treturn fmt.Errorf("diagnostic directive tuple must contain at least ${ignore.minItems} elements, got %d", len(tuple))`, + "\t}", + "\t*d = MappedDiagnosticDirective{}", + "\tfields := []any{&d.OriginalStart, &d.OriginalLength, &d.VirtualStart, &d.VirtualEnd, &d.Policy}", + "\tfor i, field := range fields {", + "\t\tif err := json.Unmarshal(tuple[i], field); err != nil {", + '\t\t\treturn fmt.Errorf("invalid diagnostic directive tuple element %d: %w", i, err)', + "\t\t}", + "\t}", + "\tswitch d.Policy {", + `\tcase ${ignorePolicy}:`, + `\t\tif len(tuple) > ${ignore.minItems} {`, + `\t\t\td.DiagnosticCodes = make([]int32, len(tuple)-${ignore.minItems})`, + "\t\t\tfor i := range d.DiagnosticCodes {", + `\t\t\t\tif err := json.Unmarshal(tuple[i+${ignore.minItems}], &d.DiagnosticCodes[i]); err != nil {`, + `\t\t\t\t\treturn fmt.Errorf("invalid diagnostic directive tuple element %d: %w", i+${ignore.minItems}, err)`, + "\t\t\t\t}", + "\t\t\t}", + "\t\t}", + `\tcase ${expectPolicy}:`, + `\t\tif len(tuple) > ${expectMaxItems} {`, + `\t\t\treturn fmt.Errorf("expect diagnostic directive tuple must contain at most ${expectMaxItems} elements, got %d", len(tuple))`, + "\t\t}", + `\t\tif len(tuple) == ${expectMaxItems} {`, + "\t\t\td.UnusedExpectDirectiveIndex = new(int)", + `\t\t\tif err := json.Unmarshal(tuple[${expectMaxItems - 1}], d.UnusedExpectDirectiveIndex); err != nil {`, + `\t\t\t\treturn fmt.Errorf("invalid diagnostic directive tuple element ${expectMaxItems - 1}: %w", err)`, + "\t\t\t}", + "\t\t}", + "\tdefault:", + "\t\tif len(tuple) > 5 {", + '\t\t\treturn fmt.Errorf("invalid diagnostic directive policy %d with additional tuple elements", d.Policy)', + "\t\t}", + "\t}", + "\treturn nil", + "}", + ); + } + else if (name === "SpanMapSegment") { + spanMapTuple(document); + lines.push(`type ${name} = spanmap.SegmentTuple`); } else { throw new Error(`Unsupported definition: ${name}`); diff --git a/tsc/internal/api/encoder/encoder.go b/tsc/internal/api/encoder/encoder.go index 89be2c2891a98..a1a7d922a6d7f 100644 --- a/tsc/internal/api/encoder/encoder.go +++ b/tsc/internal/api/encoder/encoder.go @@ -183,7 +183,7 @@ const ( // // Diagnostic directives are msgpack arrays of normalized tuples in UTF-16 coordinates: // -// [originalStart: uint, originalLength: uint, virtualStart: uint, virtualLength: uint, policy: uint, diagnosticCode: uint, unusedCode: uint] +// [originalStart: uint, originalLength: uint, virtualStart: uint, virtualLength: uint, policy: uint, unusedCode: uint, diagnosticCodes: uint[]] // // An offset of 0xFFFFFFFF indicates no data (empty array). // @@ -860,8 +860,11 @@ func encodeDiagnosticDirectives(directives []ast.MappedDiagnosticDirective, virt *buf = msgpackWriteUint(*buf, uint32(virtualStart)) *buf = msgpackWriteUint(*buf, uint32(virtualEnd-virtualStart)) *buf = msgpackWriteUint(*buf, uint32(directive.Policy)) - *buf = msgpackWriteUint(*buf, uint32(directive.DiagnosticCode)) *buf = msgpackWriteUint(*buf, uint32(directive.UnusedCode)) + *buf = msgpackWriteArrayHeader(*buf, len(directive.DiagnosticCodes)) + for _, code := range directive.DiagnosticCodes { + *buf = msgpackWriteUint(*buf, uint32(code)) + } } return offset } diff --git a/tsc/internal/api/encoder/encoder_test.go b/tsc/internal/api/encoder/encoder_test.go index 38558b5814328..ed99209634c37 100644 --- a/tsc/internal/api/encoder/encoder_test.go +++ b/tsc/internal/api/encoder/encoder_test.go @@ -52,11 +52,11 @@ func TestEncodeContentMapperSourceFileMetadata(t *testing.T) { VirtualFileName: "/component.vue.ts", DiagnosticDirectives: []ast.MappedDiagnosticDirective{ { - OriginalRange: core.NewTextRange(4, 5), - VirtualRange: core.NewTextRange(4, 11), - Policy: ast.MappedDiagnosticDirectivePolicyIgnore, - DiagnosticCode: 2353, - Source: "mapper", + OriginalRange: core.NewTextRange(4, 5), + VirtualRange: core.NewTextRange(4, 11), + Policy: ast.MappedDiagnosticDirectivePolicyIgnore, + DiagnosticCodes: []int32{2353, 2322}, + Source: "mapper", }, { OriginalRange: core.NewTextRange(4, 5), @@ -66,6 +66,12 @@ func TestEncodeContentMapperSourceFileMetadata(t *testing.T) { UnusedMessageText: "Unused framework directive.", Source: "mapper", }, + { + OriginalRange: core.NewTextRange(4, 5), + VirtualRange: core.NewTextRange(4, 11), + Policy: ast.MappedDiagnosticDirectivePolicyIgnore, + DiagnosticCodes: []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, + }, }, }) @@ -84,21 +90,31 @@ func TestEncodeContentMapperSourceFileMetadata(t *testing.T) { assert.Equal(t, encodedString(buf, virtualFileNameIndex), "/component.vue.ts") structuredDataOffset := readUint32(buf, encoder.HeaderOffsetStructuredData) directiveOffset := structuredDataOffset + diagnosticDirectivesOffset - assert.DeepEqual(t, buf[directiveOffset:directiveOffset+21], []byte{ - 0x92, // two directives + expected := []byte{ + 0x93, // three directives 0x97, // seven-element selected-ignore tuple 2, 1, // original range [2, 3) in UTF-16 2, 7, // virtual range [2, 9) in UTF-16 0, // ignore policy + 0, // unused diagnostic code + 0x92, // two diagnostic-code selectors 0xcd, 9, 49, // diagnostic code 2353 - 0, // unused diagnostic code + 0xcd, 9, 18, // diagnostic code 2322 0x97, // seven-element expect tuple 2, 1, // original range [2, 3) in UTF-16 2, 7, // virtual range [2, 9) in UTF-16 1, // expect policy - 0, // no diagnostic-code selector 0xcd, 10, 18, // unused diagnostic code 2578 - }) + 0x90, // no diagnostic-code selectors + 0x97, // seven-element selected-ignore tuple + 2, 1, // original range [2, 3) in UTF-16 + 2, 7, // virtual range [2, 9) in UTF-16 + 0, // ignore policy + 0, // unused diagnostic code + 0xdc, 0, 16, // array16 with sixteen selectors + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + } + assert.DeepEqual(t, buf[directiveOffset:directiveOffset+uint32(len(expected))], expected) } func encodedString(buf []byte, index uint32) string { diff --git a/tsc/internal/ast/ast.go b/tsc/internal/ast/ast.go index 4ace2ed4d511b..fa18cde63bace 100644 --- a/tsc/internal/ast/ast.go +++ b/tsc/internal/ast/ast.go @@ -2543,11 +2543,12 @@ const ( ) type MappedDiagnosticDirective struct { - OriginalRange core.TextRange - VirtualRange core.TextRange - Policy MappedDiagnosticDirectivePolicy - DiagnosticCode int32 - UnusedCode int32 + OriginalRange core.TextRange + VirtualRange core.TextRange + Policy MappedDiagnosticDirectivePolicy + UnusedCode int32 + // DiagnosticCodes restricts an ignore directive; an empty slice matches all codes. + DiagnosticCodes []int32 UnusedMessageText string Source string } diff --git a/tsc/internal/compiler/contentmapper_test.go b/tsc/internal/compiler/contentmapper_test.go index 53c5bd4f69da9..56a5597a84e1e 100644 --- a/tsc/internal/compiler/contentmapper_test.go +++ b/tsc/internal/compiler/contentmapper_test.go @@ -866,37 +866,55 @@ func TestContentMapperDirectivesPreserveIncrementalGlobals(t *testing.T) { } } -func TestContentMapperDiagnosticDirectiveCodeSelector(t *testing.T) { +func TestContentMapperDiagnosticDirectiveCodeSelectors(t *testing.T) { t.Parallel() const text = ` const object: { known: string } = { extra: 1 }; const value: string = 1; +const missing = missingName; ` - program := newContentMapperProgram(t, fakeContentMapperHost{ - transform: func(fileName string, content string) (contentmapper.Result, error) { - return contentmapper.Result{ - Text: text, - VirtualExtension: ".ts", - Mappings: spanmap.New([]spanmap.Segment{{ - OriginalEnd: core.TextPos(len(content)), - VirtualEnd: core.TextPos(len(text)), - Kind: spanmap.KindVerbatim, - Features: spanmap.FeatureAll, - }}), - DiagnosticDirectives: []ast.MappedDiagnosticDirective{{ - VirtualRange: core.NewTextRange(0, len(text)), - OriginalRange: core.NewTextRange(0, len(content)), - Policy: ast.MappedDiagnosticDirectivePolicyIgnore, - DiagnosticCode: 2353, - }}, - }, nil - }, - }, map[string]string{"/src/Component.vue": text}, []string{"/src/Component.vue"}) + for _, test := range []struct { + name string + codes []int32 + expected []int32 + }{ + {name: "all codes", expected: []int32{}}, + {name: "one code", codes: []int32{2353}, expected: []int32{2322, 2304}}, + {name: "multiple codes", codes: []int32{2353, 2322}, expected: []int32{2304}}, + {name: "no matching codes", codes: []int32{9999}, expected: []int32{2353, 2322, 2304}}, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + program := newContentMapperProgram(t, fakeContentMapperHost{ + transform: func(fileName string, content string) (contentmapper.Result, error) { + return contentmapper.Result{ + Text: text, + VirtualExtension: ".ts", + Mappings: spanmap.New([]spanmap.Segment{{ + OriginalEnd: core.TextPos(len(content)), + VirtualEnd: core.TextPos(len(text)), + Kind: spanmap.KindVerbatim, + Features: spanmap.FeatureAll, + }}), + DiagnosticDirectives: []ast.MappedDiagnosticDirective{{ + VirtualRange: core.NewTextRange(0, len(text)), + OriginalRange: core.NewTextRange(0, len(content)), + Policy: ast.MappedDiagnosticDirectivePolicyIgnore, + DiagnosticCodes: test.codes, + }}, + }, nil + }, + }, map[string]string{"/src/Component.vue": text}, []string{"/src/Component.vue"}) - file := program.GetSourceFile("/src/Component.vue") - diags := program.GetSemanticDiagnostics(t.Context(), file) - assert.Equal(t, len(diags), 1) - assert.Equal(t, diags[0].Code(), int32(2322)) + file := program.GetSourceFile("/src/Component.vue") + diags := program.GetSemanticDiagnostics(t.Context(), file) + codes := make([]int32, len(diags)) + for i, diagnostic := range diags { + codes[i] = diagnostic.Code() + } + assert.DeepEqual(t, codes, test.expected) + }) + } } func TestCompositeProjectContentMapperSupplementalRoots(t *testing.T) { diff --git a/tsc/internal/compiler/program.go b/tsc/internal/compiler/program.go index 38ed8528ad249..1bb52de223a00 100644 --- a/tsc/internal/compiler/program.go +++ b/tsc/internal/compiler/program.go @@ -1748,7 +1748,7 @@ func applyContentMapperDiagnosticDirectives(sourceFile *ast.SourceFile, diags [] } for i, directive := range directives { if diag.Pos() >= directive.VirtualRange.Pos() && diag.Pos() < directive.VirtualRange.End() && - (directive.DiagnosticCode == 0 || diag.Code() == directive.DiagnosticCode) { + (len(directive.DiagnosticCodes) == 0 || slices.Contains(directive.DiagnosticCodes, diag.Code())) { used[i] = true return true } diff --git a/tsc/internal/contentmapper/host_test.go b/tsc/internal/contentmapper/host_test.go index 0789b4e20a1ca..41c7cc8c82f5d 100644 --- a/tsc/internal/contentmapper/host_test.go +++ b/tsc/internal/contentmapper/host_test.go @@ -506,20 +506,19 @@ func TestRunnerTransformDiagnosticDirectives(t *testing.T) { assert.Equal(t, directive.UnusedCode, int32(2578)) assert.Equal(t, directive.UnusedMessageText, "Unused framework directive.") assert.Equal(t, directive.Source, "mapper") - diagnosticCode := int32(2353) result, err = transform(contentmapper.MappedOutput{ Text: "virtual source", Extension: ".ts", DiagnosticDirectives: protocolDiagnosticDirectives([]contentmapper.MappedDiagnosticDirective{{ - OriginalStart: 0, - OriginalLength: 9, - VirtualStart: 8, - VirtualEnd: 14, - Policy: contentmapper.DiagnosticDirectivePolicyIgnore, - DiagnosticCode: &diagnosticCode, + OriginalStart: 0, + OriginalLength: 9, + VirtualStart: 8, + VirtualEnd: 14, + Policy: contentmapper.DiagnosticDirectivePolicyIgnore, + DiagnosticCodes: []int32{2353, 2322}, }}), }) assert.NilError(t, err) - assert.Equal(t, result.DiagnosticDirectives[0].DiagnosticCode, diagnosticCode) + assert.DeepEqual(t, result.DiagnosticDirectives[0].DiagnosticCodes, []int32{2353, 2322}) unusedIndex := 1 result, err = transform(contentmapper.MappedOutput{ Text: "virtual source", Extension: ".ts", @@ -573,8 +572,17 @@ func TestRunnerTransformDiagnosticDirectives(t *testing.T) { name: "invalid diagnostic code", text: "x", directives: []contentmapper.MappedDiagnosticDirective{{ - Policy: contentmapper.DiagnosticDirectivePolicyIgnore, - DiagnosticCode: new(int32), + Policy: contentmapper.DiagnosticDirectivePolicyIgnore, + DiagnosticCodes: []int32{0}, + }}, + kind: contentmapper.DiagnosticDirectiveErrorKindInvalidDiagnosticCode, + }, + { + name: "invalid additional diagnostic code", + text: "x", + directives: []contentmapper.MappedDiagnosticDirective{{ + Policy: contentmapper.DiagnosticDirectivePolicyIgnore, + DiagnosticCodes: []int32{2353, -1}, }}, kind: contentmapper.DiagnosticDirectiveErrorKindInvalidDiagnosticCode, }, @@ -641,7 +649,6 @@ func TestRunnerTransformDiagnosticDirectives(t *testing.T) { func TestMappedDiagnosticDirectiveJSON(t *testing.T) { t.Parallel() - diagnosticCode := int32(2353) tests := []struct { name string directive contentmapper.MappedDiagnosticDirective @@ -661,8 +668,8 @@ func TestMappedDiagnosticDirectiveJSON(t *testing.T) { directive: contentmapper.MappedDiagnosticDirective{ VirtualStart: 8, VirtualEnd: 14, OriginalStart: 0, OriginalLength: 9, - Policy: contentmapper.DiagnosticDirectivePolicyIgnore, - DiagnosticCode: &diagnosticCode, + Policy: contentmapper.DiagnosticDirectivePolicyIgnore, + DiagnosticCodes: []int32{2353}, }, want: `[0,9,8,14,0,2353]`, }, @@ -688,12 +695,30 @@ func TestMappedDiagnosticDirectiveJSON(t *testing.T) { }) } + t.Run("multiple selected codes", func(t *testing.T) { + const data = `[0,9,8,14,0,2353,2322]` + var directive contentmapper.MappedDiagnosticDirective + assert.NilError(t, json.Unmarshal([]byte(data), &directive)) + assert.DeepEqual(t, directive.DiagnosticCodes, []int32{2353, 2322}) + encoded, err := json.Marshal(directive) + assert.NilError(t, err) + assert.Equal(t, string(encoded), data) + }) + for _, data := range []string{ `[0,0,0,0]`, - `[0,0,0,0,0,1,2]`, + `[0,0,0,0,1,1,2]`, + `[0,0,0,0,2,1]`, + } { + var directive contentmapper.MappedDiagnosticDirective + assert.ErrorContains(t, json.Unmarshal([]byte(data), &directive), "diagnostic directive") + } + for _, data := range []string{ + `[0,0,0,0,0,2353,"2322"]`, + `[0,0,0,0,0,2353,2147483648]`, } { var directive contentmapper.MappedDiagnosticDirective - assert.ErrorContains(t, json.Unmarshal([]byte(data), &directive), "diagnostic directive tuple") + assert.ErrorContains(t, json.Unmarshal([]byte(data), &directive), "invalid diagnostic directive tuple element 6") } unusedIndex := 1 @@ -970,19 +995,19 @@ func TestRunnerLeaseLifecycle(t *testing.T) { // recordingMapper captures project configuration and lifecycle requests for host protocol tests. type recordingMapper struct { - mu sync.Mutex - received string - receivedOptions string - receivedLocale string - receivedCodeSelectors bool - projectHandles []string - closedHandles []string - transformHandle string - transformParams string - watchedFiles []string - configIdentity *string - dynamicConfig bool - optionDiagnostics []contentmapper.OptionDiagnosticResult + mu sync.Mutex + received string + receivedOptions string + receivedLocale string + receivedInitialize string + projectHandles []string + closedHandles []string + transformHandle string + transformParams string + watchedFiles []string + configIdentity *string + dynamicConfig bool + optionDiagnostics []contentmapper.OptionDiagnosticResult } type blockingMapper struct { @@ -1011,7 +1036,7 @@ func (m *recordingMapper) HandleRequest(ctx context.Context, method string, para } m.mu.Lock() m.receivedLocale = p.Locale - m.receivedCodeSelectors = p.Capabilities != nil && p.Capabilities.DiagnosticDirectiveCodeSelectors + m.receivedInitialize = string(params) m.mu.Unlock() return contentmapper.InitializeResult{PositionEncoding: contentmapper.PositionEncodingUTF8, DiagnosticSource: "mapper"}, nil case contentmapper.MethodOpenProject: @@ -1397,7 +1422,7 @@ func TestRunnerForwardsProjectOptions(t *testing.T) { assert.Equal(t, mapper.received, string(want)) assert.Equal(t, mapper.receivedOptions, `{"strictTemplates":true}`) assert.Equal(t, mapper.receivedLocale, "cs-CZ") - assert.Assert(t, mapper.receivedCodeSelectors) + assert.Assert(t, !strings.Contains(mapper.receivedInitialize, `"diagnosticDirectiveCodeSelectors"`)) assert.Assert(t, !strings.Contains(mapper.transformParams, `"options"`)) assert.Assert(t, !strings.Contains(mapper.transformParams, `"compilerOptions"`)) } diff --git a/tsc/internal/contentmapper/hostimpl.go b/tsc/internal/contentmapper/hostimpl.go index 2001ff9ba8daf..7e7631c3bcf65 100644 --- a/tsc/internal/contentmapper/hostimpl.go +++ b/tsc/internal/contentmapper/hostimpl.go @@ -912,9 +912,6 @@ func handshake(ctx context.Context, conn ipc.Conn, diagnosticLocale locale.Local raw, err := conn.Call(ctx, MethodInitialize, InitializeParams{ Locale: diagnosticLocale.String(), PositionEncodings: []PositionEncoding{PositionEncodingUTF8, PositionEncodingUTF16}, - Capabilities: &InitializeCapabilities{ - DiagnosticDirectiveCodeSelectors: true, - }, }) if err != nil { return "", "", err @@ -1045,13 +1042,13 @@ func normalizeDiagnosticDirectives(diagnosticDirectives *DiagnosticDirectives, v normalized := ast.MappedDiagnosticDirective{Source: diagnosticSource} switch directive.Policy { case DiagnosticDirectivePolicyIgnore: - if directive.DiagnosticCode != nil && *directive.DiagnosticCode <= 0 { - return nil, directiveError(DiagnosticDirectiveErrorKindInvalidDiagnosticCode) + for _, code := range directive.DiagnosticCodes { + if code <= 0 { + return nil, directiveError(DiagnosticDirectiveErrorKindInvalidDiagnosticCode) + } } normalized.Policy = ast.MappedDiagnosticDirectivePolicyIgnore - if directive.DiagnosticCode != nil { - normalized.DiagnosticCode = *directive.DiagnosticCode - } + normalized.DiagnosticCodes = directive.DiagnosticCodes case DiagnosticDirectivePolicyExpect: unusedDiagnosticIndex := 0 if directive.UnusedExpectDirectiveIndex != nil { diff --git a/tsc/internal/contentmapper/protocol_generated.go b/tsc/internal/contentmapper/protocol_generated.go index 3c5b6613642e7..5ae2144df301c 100644 --- a/tsc/internal/contentmapper/protocol_generated.go +++ b/tsc/internal/contentmapper/protocol_generated.go @@ -23,14 +23,6 @@ type InitializeParams struct { Locale string `json:"locale,omitempty"` // PositionEncodings lists the coordinate spaces accepted by the host. PositionEncodings []PositionEncoding `json:"positionEncodings"` - // Capabilities contains the optional protocol extensions supported by the host. Absent in older hosts. - Capabilities *InitializeCapabilities `json:"capabilities,omitempty"` -} - -// InitializeCapabilities describes the optional protocol extensions supported by the host. -type InitializeCapabilities struct { - // DiagnosticDirectiveCodeSelectors is the ignore directives may select TypeScript diagnostic codes. - DiagnosticDirectiveCodeSelectors bool `json:"diagnosticDirectiveCodeSelectors,omitempty"` } // InitializeResult describes the response to the initialize request. @@ -132,14 +124,12 @@ type DiagnosticDirectives struct { Directives []MappedDiagnosticDirective `json:"directives"` } -// MappedDiagnosticDirective is encoded as [originalStart, originalLength, virtualStart, virtualEnd, policy, -// policyValue?]. For expect directives, policyValue is an unused-expect diagnostic index; an omitted index selects the -// only shared diagnostic and requires exactly one. For ignore directives, policyValue is an optional TypeScript -// diagnostic code. Expect directives require a valid original range. +// MappedDiagnosticDirective is encoded as [originalStart, originalLength, virtualStart, virtualEnd, 0, +// ...diagnosticCodes] or [originalStart, originalLength, virtualStart, virtualEnd, 1, unusedExpectDirectiveIndex?]. type MappedDiagnosticDirective struct { - // OriginalStart is the original start offset. It is ignored for ignore directives if the original range is invalid. + // OriginalStart is the original start offset. OriginalStart int - // OriginalLength is the original span length. It is ignored for ignore directives if the original range is invalid. + // OriginalLength is the original span length. OriginalLength int // VirtualStart is the inclusive virtual start offset. VirtualStart int @@ -147,10 +137,10 @@ type MappedDiagnosticDirective struct { VirtualEnd int // Policy is the diagnostic directive policy: 0 ignores diagnostics; 1 expects a diagnostic. Policy DiagnosticDirectivePolicy - // UnusedExpectDirectiveIndex is the index into unusedExpectDirectiveDiagnostics for expect directives, or a positive TypeScript diagnostic code for ignore directives. + // UnusedExpectDirectiveIndex selects the unused diagnostic for an expect directive. UnusedExpectDirectiveIndex *int - // DiagnosticCode selects one TypeScript diagnostic code for an ignore directive. - DiagnosticCode *int32 + // DiagnosticCodes selects TypeScript diagnostic codes for an ignore directive; empty selects all codes. + DiagnosticCodes []int32 } var ( @@ -160,10 +150,15 @@ var ( func (d MappedDiagnosticDirective) MarshalJSONTo(enc *json.Encoder) error { tuple := []any{d.OriginalStart, d.OriginalLength, d.VirtualStart, d.VirtualEnd, d.Policy} - if d.Policy == DiagnosticDirectivePolicyIgnore && d.DiagnosticCode != nil { - tuple = append(tuple, *d.DiagnosticCode) - } else if d.Policy == DiagnosticDirectivePolicyExpect && d.UnusedExpectDirectiveIndex != nil { - tuple = append(tuple, *d.UnusedExpectDirectiveIndex) + switch d.Policy { + case DiagnosticDirectivePolicyIgnore: + for _, code := range d.DiagnosticCodes { + tuple = append(tuple, code) + } + case DiagnosticDirectivePolicyExpect: + if d.UnusedExpectDirectiveIndex != nil { + tuple = append(tuple, *d.UnusedExpectDirectiveIndex) + } } return json.MarshalEncode(enc, tuple) } @@ -173,8 +168,8 @@ func (d *MappedDiagnosticDirective) UnmarshalJSONFrom(dec *json.Decoder) error { if err := json.UnmarshalDecode(dec, &tuple); err != nil { return err } - if len(tuple) != 5 && len(tuple) != 6 { - return fmt.Errorf("diagnostic directive tuple must contain 5 or 6 elements, got %d", len(tuple)) + if len(tuple) < 5 { + return fmt.Errorf("diagnostic directive tuple must contain at least 5 elements, got %d", len(tuple)) } *d = MappedDiagnosticDirective{} fields := []any{&d.OriginalStart, &d.OriginalLength, &d.VirtualStart, &d.VirtualEnd, &d.Policy} @@ -183,20 +178,29 @@ func (d *MappedDiagnosticDirective) UnmarshalJSONFrom(dec *json.Decoder) error { return fmt.Errorf("invalid diagnostic directive tuple element %d: %w", i, err) } } - if len(tuple) == 6 { - var field any - switch d.Policy { - case DiagnosticDirectivePolicyIgnore: - d.DiagnosticCode = new(int32) - field = d.DiagnosticCode - case DiagnosticDirectivePolicyExpect: + switch d.Policy { + case DiagnosticDirectivePolicyIgnore: + if len(tuple) > 5 { + d.DiagnosticCodes = make([]int32, len(tuple)-5) + for i := range d.DiagnosticCodes { + if err := json.Unmarshal(tuple[i+5], &d.DiagnosticCodes[i]); err != nil { + return fmt.Errorf("invalid diagnostic directive tuple element %d: %w", i+5, err) + } + } + } + case DiagnosticDirectivePolicyExpect: + if len(tuple) > 6 { + return fmt.Errorf("expect diagnostic directive tuple must contain at most 6 elements, got %d", len(tuple)) + } + if len(tuple) == 6 { d.UnusedExpectDirectiveIndex = new(int) - field = d.UnusedExpectDirectiveIndex - default: - return fmt.Errorf("invalid diagnostic directive policy %d with sixth tuple element", d.Policy) + if err := json.Unmarshal(tuple[5], d.UnusedExpectDirectiveIndex); err != nil { + return fmt.Errorf("invalid diagnostic directive tuple element 5: %w", err) + } } - if err := json.Unmarshal(tuple[5], field); err != nil { - return fmt.Errorf("invalid diagnostic directive tuple element 5: %w", err) + default: + if len(tuple) > 5 { + return fmt.Errorf("invalid diagnostic directive policy %d with additional tuple elements", d.Policy) } } return nil From f33301cc9a8b210b48cb19123b11615d9f782c1b Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 20 Nov 2026 15:42:34 -0800 Subject: [PATCH 4/7] Preserve union constituent comments in unified Go types --- .../typescript/schemas/contentmapper.schema.json | 6 +++--- tools/scripts/tsc/generate-contentmapper.ts | 13 +++++++++++-- tsc/internal/contentmapper/protocol_generated.go | 6 ++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/typescript/schemas/contentmapper.schema.json b/packages/typescript/schemas/contentmapper.schema.json index ab49e00666e5e..bebe6c7ddde91 100644 --- a/packages/typescript/schemas/contentmapper.schema.json +++ b/packages/typescript/schemas/contentmapper.schema.json @@ -415,17 +415,17 @@ "oneOf": [ { "type": "array", - "description": "Ignore directive suppressing TypeScript diagnostics in the virtual range. When diagnostic codes are given, only matching diagnostics are suppressed.", + "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. It is ignored if the original range is invalid." + "description": "Original start offset." }, { "type": "integer", "title": "originalLength", - "description": "Original span length. It is ignored if the original range is invalid." + "description": "Original span length." }, { "type": "integer", diff --git a/tools/scripts/tsc/generate-contentmapper.ts b/tools/scripts/tsc/generate-contentmapper.ts index 471063afd07e0..404692df8841b 100644 --- a/tools/scripts/tsc/generate-contentmapper.ts +++ b/tools/scripts/tsc/generate-contentmapper.ts @@ -278,9 +278,11 @@ function mappedDiagnosticDirectiveVariants(schema: Schema): { ignore: Schema; ex items.slice(0, 4).every((item, index) => item.title === ["originalStart", "originalLength", "virtualStart", "virtualEnd"][index] && item.type === "integer" + && JSON.stringify(item) === JSON.stringify(expectItems[index]) ), - "Unsupported MappedDiagnosticDirective range fields", + "MappedDiagnosticDirective policy variants must share range fields", ); + assert(variant.description, "Missing MappedDiagnosticDirective policy variant description"); } return { ignore, expect, expectMaxItems }; } @@ -288,7 +290,10 @@ function mappedDiagnosticDirectiveVariants(schema: Schema): { ignore: Schema; ex function typeDescription(name: string, text: string | undefined): string[] { if (!text) return []; const normalized = text.trim().replace(/\s+/g, " "); - const description = `${name} ${normalized.startsWith("[") ? `is encoded as ${normalized}` : `describes ${nounPhrase(normalized)}`}`; + return commentLines(`${name} ${normalized.startsWith("[") ? `is encoded as ${normalized}` : `describes ${nounPhrase(normalized)}`}`); +} + +function commentLines(description: string): string[] { const lines: string[] = []; let line = "//"; for (const word of description.split(" ")) { @@ -404,6 +409,10 @@ export function generateContentMapper(document: SchemaDocument): string { fields.every((field, index) => field.title === ["originalStart", "originalLength", "virtualStart", "virtualEnd"][index]), "Unsupported MappedDiagnosticDirective fields", ); + for (const [policy, variant] of [[ignorePolicy, ignore], [expectPolicy, expect]] as const) { + const description = variant.description!.trim().replace(/\s+/g, " "); + lines.push("//", ...commentLines(`${policy} encodes ${/^[aeiou]/i.test(description) ? "an" : "a"} ${nounPhrase(description).replace(/^the /, "")}`)); + } lines.push("type MappedDiagnosticDirective struct {"); for (const field of fields) { const name = goName(field.title!); diff --git a/tsc/internal/contentmapper/protocol_generated.go b/tsc/internal/contentmapper/protocol_generated.go index 5ae2144df301c..1b06dcbdc09ba 100644 --- a/tsc/internal/contentmapper/protocol_generated.go +++ b/tsc/internal/contentmapper/protocol_generated.go @@ -126,6 +126,12 @@ type DiagnosticDirectives struct { // MappedDiagnosticDirective is encoded as [originalStart, originalLength, virtualStart, virtualEnd, 0, // ...diagnosticCodes] or [originalStart, originalLength, virtualStart, virtualEnd, 1, unusedExpectDirectiveIndex?]. +// +// DiagnosticDirectivePolicyIgnore encodes an 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. +// +// DiagnosticDirectivePolicyExpect encodes an 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. type MappedDiagnosticDirective struct { // OriginalStart is the original start offset. OriginalStart int From f3563c20465bd5b4b6f664d33ebc59fe721a7165 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 20 Nov 2026 15:47:51 -0800 Subject: [PATCH 5/7] Trim unit tests that only robots can understand --- packages/typescript/test/async/api.test.ts | 13 +--- packages/typescript/test/encoder.test.ts | 67 +++++++------------ packages/typescript/test/sync/api.test.ts | 13 +--- packages/typescript/test/wtf8.test.ts | 39 +---------- tsc/internal/api/encoder/encoder_test.go | 57 ++++------------ tsc/internal/compiler/contentmapper_test.go | 73 ++++++++------------- tsc/internal/contentmapper/host_test.go | 72 +++++--------------- 7 files changed, 83 insertions(+), 251 deletions(-) diff --git a/packages/typescript/test/async/api.test.ts b/packages/typescript/test/async/api.test.ts index 99bc92dcb09b6..d0a7f17f0539b 100644 --- a/packages/typescript/test/async/api.test.ts +++ b/packages/typescript/test/async/api.test.ts @@ -3118,12 +3118,6 @@ export const other: { value: string } = box;`, t.after(() => rmSync(directory, { recursive: true, force: true })); const mapperDirectory = join(directory, "node_modules", "mapper"); mkdirSync(mapperDirectory, { recursive: true }); - const virtualText = ` - export {}; - const object: { known: string } = { extra: 1 }; - const value: string = 1; - const missing = missingName; - `; const mapperScript = ` import { createMessageConnection } from ${JSON.stringify(new URL("../../vendor/vscode-jsonrpc/lib/node/main.js", import.meta.url).href)}; const connection = createMessageConnection(process.stdin, process.stdout); @@ -3131,11 +3125,11 @@ export const other: { value: string } = box;`, connection.onRequest("openProject", () => ({})); connection.onRequest("closeProject", () => null); connection.onRequest("transform", ({ content }) => ({ - text: ${JSON.stringify(virtualText)}, + text: "export {};", extension: ".ts", diagnosticDirectives: { unusedExpectDirectiveDiagnostics: [], - directives: [[0, 0, 0, ${virtualText.length}, 0, 2353, 2322]], + directives: [[0, 0, 0, 10, 0, 2353, 2322]], }, supplemental: [{ text: content, extension: ".ts" }], })); @@ -3168,12 +3162,11 @@ export const other: { value: string } = box;`, assert.ok(mappedFile); assert.deepEqual(mappedFile.diagnosticDirectives, [{ originalRange: { pos: 0, end: 0 }, - virtualRange: { pos: 0, end: virtualText.length }, + virtualRange: { pos: 0, end: 10 }, policy: 0, unusedCode: 0, diagnosticCodes: [2353, 2322], }]); - assert.deepEqual((await program.getSemanticDiagnostics(mappedFileName)).map(diagnostic => diagnostic.code), [2304]); const supplementalFileName = toRootedFilePath(join(directory, "app.box.0.ts"), undefined); assert.equal(existsSync(supplementalFileName), false); assert.ok(await program.getSourceFile(supplementalFileName)); diff --git a/packages/typescript/test/encoder.test.ts b/packages/typescript/test/encoder.test.ts index 240f9d2cc15aa..062b406179abd 100644 --- a/packages/typescript/test/encoder.test.ts +++ b/packages/typescript/test/encoder.test.ts @@ -119,59 +119,36 @@ describe("Encoder", { concurrency }, () => { assert.strictEqual(decoded.canonicalSourceFileName, undefined); }); - test("decodes extensible content mapper diagnostic directives", () => { - const encoded = encodeSourceFile(makeSF("text", "/component.vue.ts", [])); - const encodedView = new DataView(encoded.buffer, encoded.byteOffset, encoded.byteLength); - const nodesOffset = encodedView.getUint32(HEADER_OFFSET_NODES, true); + test("skips unknown trailing diagnostic directive fields", () => { const writer = new MsgpackWriter(); - writer.writeArrayHeader(3); - writer.writeArrayHeader(8); - for (const value of [0, 1, 2, 3, 0, 0]) writer.writeUint(value); - writer.writeArrayHeader(2); - for (const code of [2353, 2322]) writer.writeUint(code); - writer.writeArrayHeader(2); // An unknown future field. - writer.writeBool(true); - writer.writeArrayHeader(1); - writer.writeUint(42); - writer.writeArrayHeader(7); - for (const value of [1, 1, 0, 1, 1, 2578]) writer.writeUint(value); - writer.writeArrayHeader(0); - writer.writeArrayHeader(7); - for (const value of [0, 1, 0, 1, 0, 0]) writer.writeUint(value); - writer.writeArrayHeader(16); - for (let code = 1; code <= 16; code++) writer.writeUint(code); - const directive = writer.finish(); - const withDirective = new Uint8Array(encoded.length + directive.length); - withDirective.set(encoded.subarray(0, nodesOffset)); - withDirective.set(directive, nodesOffset); - withDirective.set(encoded.subarray(nodesOffset), nodesOffset + directive.length); - const view = new DataView(withDirective.buffer); - const extendedDataOffset = view.getUint32(HEADER_OFFSET_EXTENDED_DATA, true); - view.setUint32(extendedDataOffset + 72, 0, true); + 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 + directive.length, 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(withDirective).diagnosticDirectives, [{ + assert.deepStrictEqual(decode(withDirectives).diagnosticDirectives, [{ originalRange: { pos: 0, end: 1 }, virtualRange: { pos: 2, end: 5 }, policy: 0, unusedCode: 0, - diagnosticCodes: [2353, 2322], - }, { - originalRange: { pos: 1, end: 2 }, - virtualRange: { pos: 0, end: 1 }, - policy: 1, - unusedCode: 2578, - diagnosticCodes: [], - }, { - originalRange: { pos: 0, end: 1 }, - virtualRange: { pos: 0, end: 1 }, - policy: 0, - unusedCode: 0, - diagnosticCodes: Array.from({ length: 16 }, (_, i) => i + 1), + diagnosticCodes: [2322], }]); - withDirective[nodesOffset + 1] = 0x96; - assert.throws(() => decode(withDirective).diagnosticDirectives, /Invalid diagnostic directive/); }); test("keeps adjacent surrogate string-table entries separate", () => { diff --git a/packages/typescript/test/sync/api.test.ts b/packages/typescript/test/sync/api.test.ts index de6ee08661173..a4aac92b6e421 100644 --- a/packages/typescript/test/sync/api.test.ts +++ b/packages/typescript/test/sync/api.test.ts @@ -2934,12 +2934,6 @@ export const other: { value: string } = box;`, t.after(() => rmSync(directory, { recursive: true, force: true })); const mapperDirectory = join(directory, "node_modules", "mapper"); mkdirSync(mapperDirectory, { recursive: true }); - const virtualText = ` - export {}; - const object: { known: string } = { extra: 1 }; - const value: string = 1; - const missing = missingName; - `; const mapperScript = ` import { createMessageConnection } from ${JSON.stringify(new URL("../../vendor/vscode-jsonrpc/lib/node/main.js", import.meta.url).href)}; const connection = createMessageConnection(process.stdin, process.stdout); @@ -2947,11 +2941,11 @@ export const other: { value: string } = box;`, connection.onRequest("openProject", () => ({})); connection.onRequest("closeProject", () => null); connection.onRequest("transform", ({ content }) => ({ - text: ${JSON.stringify(virtualText)}, + text: "export {};", extension: ".ts", diagnosticDirectives: { unusedExpectDirectiveDiagnostics: [], - directives: [[0, 0, 0, ${virtualText.length}, 0, 2353, 2322]], + directives: [[0, 0, 0, 10, 0, 2353, 2322]], }, supplemental: [{ text: content, extension: ".ts" }], })); @@ -2984,12 +2978,11 @@ export const other: { value: string } = box;`, assert.ok(mappedFile); assert.deepEqual(mappedFile.diagnosticDirectives, [{ originalRange: { pos: 0, end: 0 }, - virtualRange: { pos: 0, end: virtualText.length }, + virtualRange: { pos: 0, end: 10 }, policy: 0, unusedCode: 0, diagnosticCodes: [2353, 2322], }]); - assert.deepEqual((program.getSemanticDiagnostics(mappedFileName)).map(diagnostic => diagnostic.code), [2304]); const supplementalFileName = toRootedFilePath(join(directory, "app.box.0.ts"), undefined); assert.equal(existsSync(supplementalFileName), false); assert.ok(program.getSourceFile(supplementalFileName)); diff --git a/packages/typescript/test/wtf8.test.ts b/packages/typescript/test/wtf8.test.ts index e29e2bb8bd580..94480e2fd1c1a 100644 --- a/packages/typescript/test/wtf8.test.ts +++ b/packages/typescript/test/wtf8.test.ts @@ -89,48 +89,11 @@ describe("Wtf8Decoder", { concurrency }, () => { }); }); -describe("Msgpack", { concurrency }, () => { +describe("Msgpack strings", { concurrency }, () => { test("round-trip lone surrogates", () => { const text = `high:${String.fromCharCode(0xD800)} low:${String.fromCharCode(0xDFFF)}`; const writer = new MsgpackWriter(); writer.writeString(text); assert.equal(new MsgpackReader(writer.finish()).readString(), text); }); - - test("skips values without consuming the next value", () => { - const values = [ - [0xc0], - [0xff], - [0xcd, 0, 1], - [0xd3, 0, 0, 0, 0, 0, 0, 0, 1], - [0xca, 0, 0, 0, 0], - [0xa1, 0x61], - [0xd9, 1, 0x61], - [0xda, 0, 1, 0x61], - [0xdb, 0, 0, 0, 1, 0x61], - [0xc4, 1, 0xff], - [0xc5, 0, 1, 0xff], - [0xc6, 0, 0, 0, 1, 0xff], - [0xd4, 0, 0xff], - [0xc7, 1, 0, 0xff], - [0xc8, 0, 1, 0, 0xff], - [0xc9, 0, 0, 0, 1, 0, 0xff], - [0x92, 0xc3, 0x81, 0xa1, 0x6b, 0xc0], - [0xdc, 0, 1, 0xc0], - [0xdd, 0, 0, 0, 1, 0xc0], - [0xde, 0, 1, 0, 1], - [0xdf, 0, 0, 0, 1, 0, 1], - ]; - for (const value of values) { - const reader = new MsgpackReader(Uint8Array.from([...value, 42])); - reader.skipValue(); - assert.strictEqual(reader.readUint(), 42, `after ${value.map(byte => byte.toString(16)).join(" ")}`); - } - }); - - test("rejects invalid or truncated values while skipping", () => { - for (const data of [[0xc1], [0xc4, 2, 1], [0x91], [0xd9]]) { - assert.throws(() => new MsgpackReader(Uint8Array.from(data)).skipValue()); - } - }); }); diff --git a/tsc/internal/api/encoder/encoder_test.go b/tsc/internal/api/encoder/encoder_test.go index ed99209634c37..1e97b88feba80 100644 --- a/tsc/internal/api/encoder/encoder_test.go +++ b/tsc/internal/api/encoder/encoder_test.go @@ -50,29 +50,14 @@ func TestEncodeContentMapperSourceFileMetadata(t *testing.T) { OriginalText: "đŸ˜€original", ContentMapper: "mapper@1.0.0", VirtualFileName: "/component.vue.ts", - DiagnosticDirectives: []ast.MappedDiagnosticDirective{ - { - OriginalRange: core.NewTextRange(4, 5), - VirtualRange: core.NewTextRange(4, 11), - Policy: ast.MappedDiagnosticDirectivePolicyIgnore, - DiagnosticCodes: []int32{2353, 2322}, - Source: "mapper", - }, - { - OriginalRange: core.NewTextRange(4, 5), - VirtualRange: core.NewTextRange(4, 11), - Policy: ast.MappedDiagnosticDirectivePolicyExpect, - UnusedCode: 2578, - UnusedMessageText: "Unused framework directive.", - Source: "mapper", - }, - { - OriginalRange: core.NewTextRange(4, 5), - VirtualRange: core.NewTextRange(4, 11), - Policy: ast.MappedDiagnosticDirectivePolicyIgnore, - DiagnosticCodes: []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, - }, - }, + DiagnosticDirectives: []ast.MappedDiagnosticDirective{{ + OriginalRange: core.NewTextRange(4, 5), + VirtualRange: core.NewTextRange(4, 11), + Policy: ast.MappedDiagnosticDirectivePolicyExpect, + UnusedCode: 2578, + UnusedMessageText: "Unused framework directive.", + Source: "mapper", + }}, }) buf, _, err := encoder.EncodeSourceFile(sourceFile) @@ -90,31 +75,15 @@ func TestEncodeContentMapperSourceFileMetadata(t *testing.T) { assert.Equal(t, encodedString(buf, virtualFileNameIndex), "/component.vue.ts") structuredDataOffset := readUint32(buf, encoder.HeaderOffsetStructuredData) directiveOffset := structuredDataOffset + diagnosticDirectivesOffset - expected := []byte{ - 0x93, // three directives - 0x97, // seven-element selected-ignore tuple - 2, 1, // original range [2, 3) in UTF-16 - 2, 7, // virtual range [2, 9) in UTF-16 - 0, // ignore policy - 0, // unused diagnostic code - 0x92, // two diagnostic-code selectors - 0xcd, 9, 49, // diagnostic code 2353 - 0xcd, 9, 18, // diagnostic code 2322 - 0x97, // seven-element expect tuple + assert.DeepEqual(t, buf[directiveOffset:directiveOffset+11], []byte{ + 0x91, // one directive + 0x97, // seven-element tuple 2, 1, // original range [2, 3) in UTF-16 2, 7, // virtual range [2, 9) in UTF-16 1, // expect policy 0xcd, 10, 18, // unused diagnostic code 2578 - 0x90, // no diagnostic-code selectors - 0x97, // seven-element selected-ignore tuple - 2, 1, // original range [2, 3) in UTF-16 - 2, 7, // virtual range [2, 9) in UTF-16 - 0, // ignore policy - 0, // unused diagnostic code - 0xdc, 0, 16, // array16 with sixteen selectors - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, - } - assert.DeepEqual(t, buf[directiveOffset:directiveOffset+uint32(len(expected))], expected) + 0x90, // no diagnostic codes + }) } func encodedString(buf []byte, index uint32) string { diff --git a/tsc/internal/compiler/contentmapper_test.go b/tsc/internal/compiler/contentmapper_test.go index 56a5597a84e1e..125906cec909f 100644 --- a/tsc/internal/compiler/contentmapper_test.go +++ b/tsc/internal/compiler/contentmapper_test.go @@ -866,55 +866,34 @@ func TestContentMapperDirectivesPreserveIncrementalGlobals(t *testing.T) { } } -func TestContentMapperDiagnosticDirectiveCodeSelectors(t *testing.T) { +func TestContentMapperDiagnosticDirectiveCodes(t *testing.T) { t.Parallel() - const text = ` -const object: { known: string } = { extra: 1 }; -const value: string = 1; -const missing = missingName; -` - for _, test := range []struct { - name string - codes []int32 - expected []int32 - }{ - {name: "all codes", expected: []int32{}}, - {name: "one code", codes: []int32{2353}, expected: []int32{2322, 2304}}, - {name: "multiple codes", codes: []int32{2353, 2322}, expected: []int32{2304}}, - {name: "no matching codes", codes: []int32{9999}, expected: []int32{2353, 2322, 2304}}, - } { - t.Run(test.name, func(t *testing.T) { - t.Parallel() - program := newContentMapperProgram(t, fakeContentMapperHost{ - transform: func(fileName string, content string) (contentmapper.Result, error) { - return contentmapper.Result{ - Text: text, - VirtualExtension: ".ts", - Mappings: spanmap.New([]spanmap.Segment{{ - OriginalEnd: core.TextPos(len(content)), - VirtualEnd: core.TextPos(len(text)), - Kind: spanmap.KindVerbatim, - Features: spanmap.FeatureAll, - }}), - DiagnosticDirectives: []ast.MappedDiagnosticDirective{{ - VirtualRange: core.NewTextRange(0, len(text)), - OriginalRange: core.NewTextRange(0, len(content)), - Policy: ast.MappedDiagnosticDirectivePolicyIgnore, - DiagnosticCodes: test.codes, - }}, - }, nil - }, - }, map[string]string{"/src/Component.vue": text}, []string{"/src/Component.vue"}) + // 2322: number is not assignable to string. 2304: cannot find name. + const text = "const a: string = 1;\nmissingName;\n" + program := newContentMapperProgram(t, fakeContentMapperHost{ + transform: func(fileName string, content string) (contentmapper.Result, error) { + return contentmapper.Result{ + Text: text, + VirtualExtension: ".ts", + Mappings: spanmap.New([]spanmap.Segment{{ + OriginalEnd: core.TextPos(len(content)), + VirtualEnd: core.TextPos(len(text)), + Kind: spanmap.KindVerbatim, + Features: spanmap.FeatureAll, + }}), + DiagnosticDirectives: []ast.MappedDiagnosticDirective{{ + VirtualRange: core.NewTextRange(0, len(text)), + OriginalRange: core.NewTextRange(0, len(content)), + Policy: ast.MappedDiagnosticDirectivePolicyIgnore, + DiagnosticCodes: []int32{2322}, + }}, + }, nil + }, + }, map[string]string{"/src/Component.vue": text}, []string{"/src/Component.vue"}) - file := program.GetSourceFile("/src/Component.vue") - diags := program.GetSemanticDiagnostics(t.Context(), file) - codes := make([]int32, len(diags)) - for i, diagnostic := range diags { - codes[i] = diagnostic.Code() - } - assert.DeepEqual(t, codes, test.expected) - }) - } + diags := program.GetSemanticDiagnostics(t.Context(), program.GetSourceFile("/src/Component.vue")) + assert.Equal(t, len(diags), 1) + assert.Equal(t, diags[0].Code(), int32(2304)) } func TestCompositeProjectContentMapperSupplementalRoots(t *testing.T) { diff --git a/tsc/internal/contentmapper/host_test.go b/tsc/internal/contentmapper/host_test.go index 41c7cc8c82f5d..12ea8b1f38649 100644 --- a/tsc/internal/contentmapper/host_test.go +++ b/tsc/internal/contentmapper/host_test.go @@ -506,19 +506,6 @@ func TestRunnerTransformDiagnosticDirectives(t *testing.T) { assert.Equal(t, directive.UnusedCode, int32(2578)) assert.Equal(t, directive.UnusedMessageText, "Unused framework directive.") assert.Equal(t, directive.Source, "mapper") - result, err = transform(contentmapper.MappedOutput{ - Text: "virtual source", Extension: ".ts", - DiagnosticDirectives: protocolDiagnosticDirectives([]contentmapper.MappedDiagnosticDirective{{ - OriginalStart: 0, - OriginalLength: 9, - VirtualStart: 8, - VirtualEnd: 14, - Policy: contentmapper.DiagnosticDirectivePolicyIgnore, - DiagnosticCodes: []int32{2353, 2322}, - }}), - }) - assert.NilError(t, err) - assert.DeepEqual(t, result.DiagnosticDirectives[0].DiagnosticCodes, []int32{2353, 2322}) unusedIndex := 1 result, err = transform(contentmapper.MappedOutput{ Text: "virtual source", Extension: ".ts", @@ -577,15 +564,6 @@ func TestRunnerTransformDiagnosticDirectives(t *testing.T) { }}, kind: contentmapper.DiagnosticDirectiveErrorKindInvalidDiagnosticCode, }, - { - name: "invalid additional diagnostic code", - text: "x", - directives: []contentmapper.MappedDiagnosticDirective{{ - Policy: contentmapper.DiagnosticDirectivePolicyIgnore, - DiagnosticCodes: []int32{2353, -1}, - }}, - kind: contentmapper.DiagnosticDirectiveErrorKindInvalidDiagnosticCode, - }, { name: "expect requires unused diagnostic", text: "x", @@ -669,9 +647,9 @@ func TestMappedDiagnosticDirectiveJSON(t *testing.T) { VirtualStart: 8, VirtualEnd: 14, OriginalStart: 0, OriginalLength: 9, Policy: contentmapper.DiagnosticDirectivePolicyIgnore, - DiagnosticCodes: []int32{2353}, + DiagnosticCodes: []int32{2353, 2322}, }, - want: `[0,9,8,14,0,2353]`, + want: `[0,9,8,14,0,2353,2322]`, }, { name: "expect", @@ -695,31 +673,14 @@ func TestMappedDiagnosticDirectiveJSON(t *testing.T) { }) } - t.Run("multiple selected codes", func(t *testing.T) { - const data = `[0,9,8,14,0,2353,2322]` - var directive contentmapper.MappedDiagnosticDirective - assert.NilError(t, json.Unmarshal([]byte(data), &directive)) - assert.DeepEqual(t, directive.DiagnosticCodes, []int32{2353, 2322}) - encoded, err := json.Marshal(directive) - assert.NilError(t, err) - assert.Equal(t, string(encoded), data) - }) - for _, data := range []string{ `[0,0,0,0]`, `[0,0,0,0,1,1,2]`, - `[0,0,0,0,2,1]`, + `[0,0,0,0,0,"2353"]`, } { var directive contentmapper.MappedDiagnosticDirective assert.ErrorContains(t, json.Unmarshal([]byte(data), &directive), "diagnostic directive") } - for _, data := range []string{ - `[0,0,0,0,0,2353,"2322"]`, - `[0,0,0,0,0,2353,2147483648]`, - } { - var directive contentmapper.MappedDiagnosticDirective - assert.ErrorContains(t, json.Unmarshal([]byte(data), &directive), "invalid diagnostic directive tuple element 6") - } unusedIndex := 1 diagnosticDirectives := contentmapper.DiagnosticDirectives{ @@ -995,19 +956,18 @@ func TestRunnerLeaseLifecycle(t *testing.T) { // recordingMapper captures project configuration and lifecycle requests for host protocol tests. type recordingMapper struct { - mu sync.Mutex - received string - receivedOptions string - receivedLocale string - receivedInitialize string - projectHandles []string - closedHandles []string - transformHandle string - transformParams string - watchedFiles []string - configIdentity *string - dynamicConfig bool - optionDiagnostics []contentmapper.OptionDiagnosticResult + mu sync.Mutex + received string + receivedOptions string + receivedLocale string + projectHandles []string + closedHandles []string + transformHandle string + transformParams string + watchedFiles []string + configIdentity *string + dynamicConfig bool + optionDiagnostics []contentmapper.OptionDiagnosticResult } type blockingMapper struct { @@ -1036,7 +996,6 @@ func (m *recordingMapper) HandleRequest(ctx context.Context, method string, para } m.mu.Lock() m.receivedLocale = p.Locale - m.receivedInitialize = string(params) m.mu.Unlock() return contentmapper.InitializeResult{PositionEncoding: contentmapper.PositionEncodingUTF8, DiagnosticSource: "mapper"}, nil case contentmapper.MethodOpenProject: @@ -1422,7 +1381,6 @@ func TestRunnerForwardsProjectOptions(t *testing.T) { assert.Equal(t, mapper.received, string(want)) assert.Equal(t, mapper.receivedOptions, `{"strictTemplates":true}`) assert.Equal(t, mapper.receivedLocale, "cs-CZ") - assert.Assert(t, !strings.Contains(mapper.receivedInitialize, `"diagnosticDirectiveCodeSelectors"`)) assert.Assert(t, !strings.Contains(mapper.transformParams, `"options"`)) assert.Assert(t, !strings.Contains(mapper.transformParams, `"compilerOptions"`)) } From 349a48da5f3074f62c4b6441c085a84c2b215cf5 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 20 Nov 2026 16:05:45 -0800 Subject: [PATCH 6/7] Replace unit tests with compiler tests --- tsc/internal/compiler/contentmapper_test.go | 54 ------------------- tsc/internal/contentmapper/host_test.go | 22 +------- .../contentmappertest/transforming.go | 24 ++++++--- ...pperDiagnosticDirectiveCodes.contentmapper | 21 ++++++++ ...rInvalidDiagnosticDirectives.contentmapper | 11 ++++ .../contentMapperDiagnosticDirectiveCodes.ts | 29 ++++++++++ ...ontentMapperInvalidDiagnosticDirectives.ts | 8 ++- 7 files changed, 86 insertions(+), 83 deletions(-) create mode 100644 tsc/testdata/baselines/reference/compiler/contentMapperDiagnosticDirectiveCodes.contentmapper create mode 100644 tsc/testdata/tests/cases/compiler/contentMapperDiagnosticDirectiveCodes.ts diff --git a/tsc/internal/compiler/contentmapper_test.go b/tsc/internal/compiler/contentmapper_test.go index 125906cec909f..08b3f9959679d 100644 --- a/tsc/internal/compiler/contentmapper_test.go +++ b/tsc/internal/compiler/contentmapper_test.go @@ -866,36 +866,6 @@ func TestContentMapperDirectivesPreserveIncrementalGlobals(t *testing.T) { } } -func TestContentMapperDiagnosticDirectiveCodes(t *testing.T) { - t.Parallel() - // 2322: number is not assignable to string. 2304: cannot find name. - const text = "const a: string = 1;\nmissingName;\n" - program := newContentMapperProgram(t, fakeContentMapperHost{ - transform: func(fileName string, content string) (contentmapper.Result, error) { - return contentmapper.Result{ - Text: text, - VirtualExtension: ".ts", - Mappings: spanmap.New([]spanmap.Segment{{ - OriginalEnd: core.TextPos(len(content)), - VirtualEnd: core.TextPos(len(text)), - Kind: spanmap.KindVerbatim, - Features: spanmap.FeatureAll, - }}), - DiagnosticDirectives: []ast.MappedDiagnosticDirective{{ - VirtualRange: core.NewTextRange(0, len(text)), - OriginalRange: core.NewTextRange(0, len(content)), - Policy: ast.MappedDiagnosticDirectivePolicyIgnore, - DiagnosticCodes: []int32{2322}, - }}, - }, nil - }, - }, map[string]string{"/src/Component.vue": text}, []string{"/src/Component.vue"}) - - diags := program.GetSemanticDiagnostics(t.Context(), program.GetSourceFile("/src/Component.vue")) - assert.Equal(t, len(diags), 1) - assert.Equal(t, diags[0].Code(), int32(2304)) -} - func TestCompositeProjectContentMapperSupplementalRoots(t *testing.T) { t.Parallel() contentMapperHost := fakeContentMapperHost{transform: func(fileName string, content string) (contentmapper.Result, error) { @@ -1043,30 +1013,6 @@ func TestContentMapperSourceFileState(t *testing.T) { }) assert.Assert(t, found, "expected a localized project response diagnostic, got: %v", programDiagnostics) }) - - t.Run("invalid diagnostic code is localized", func(t *testing.T) { - t.Parallel() - program := newContentMapperProgram(t, fakeContentMapperHost{ - transform: func(fileName string, content string) (contentmapper.Result, error) { - return contentmapper.Result{}, contentmapper.NewTransformError( - contentmapper.TransformErrorKindResponse, - &contentmapper.DiagnosticDirectiveError{ - Kind: contentmapper.DiagnosticDirectiveErrorKindInvalidDiagnosticCode, - Index: 2, - SupplementalIndex: -1, - }, - ) - }, - }, map[string]string{"/src/fail.vue": "original"}, []string{"/src/fail.vue"}) - programDiagnostics := collectContentMapperDiagnostics(program) - found := slices.ContainsFunc(programDiagnostics, func(diagnostic *ast.Diagnostic) bool { - return slices.ContainsFunc(diagnostic.MessageChain(), func(message *ast.Diagnostic) bool { - return message.Code() == diagnostics.Diagnostic_directive_0_returned_by_the_content_mapper_has_a_diagnostic_code_selector_that_must_be_positive.Code() && - message.String() == "Diagnostic directive 2 returned by the content mapper has a diagnostic code selector that must be positive." - }) - }) - assert.Assert(t, found, "expected an invalid diagnostic-code selector diagnostic, got: %v", programDiagnostics) - }) } func TestContentMapperProjectErrorDiagnostics(t *testing.T) { diff --git a/tsc/internal/contentmapper/host_test.go b/tsc/internal/contentmapper/host_test.go index 12ea8b1f38649..85e7463ac9e50 100644 --- a/tsc/internal/contentmapper/host_test.go +++ b/tsc/internal/contentmapper/host_test.go @@ -555,15 +555,6 @@ func TestRunnerTransformDiagnosticDirectives(t *testing.T) { }}, kind: contentmapper.DiagnosticDirectiveErrorKindInvalidPolicy, }, - { - name: "invalid diagnostic code", - text: "x", - directives: []contentmapper.MappedDiagnosticDirective{{ - Policy: contentmapper.DiagnosticDirectivePolicyIgnore, - DiagnosticCodes: []int32{0}, - }}, - kind: contentmapper.DiagnosticDirectiveErrorKindInvalidDiagnosticCode, - }, { name: "expect requires unused diagnostic", text: "x", @@ -641,16 +632,6 @@ func TestMappedDiagnosticDirectiveJSON(t *testing.T) { }, want: `[0,9,8,14,0]`, }, - { - name: "selected ignore", - directive: contentmapper.MappedDiagnosticDirective{ - VirtualStart: 8, VirtualEnd: 14, - OriginalStart: 0, OriginalLength: 9, - Policy: contentmapper.DiagnosticDirectivePolicyIgnore, - DiagnosticCodes: []int32{2353, 2322}, - }, - want: `[0,9,8,14,0,2353,2322]`, - }, { name: "expect", directive: contentmapper.MappedDiagnosticDirective{ @@ -676,10 +657,9 @@ func TestMappedDiagnosticDirectiveJSON(t *testing.T) { for _, data := range []string{ `[0,0,0,0]`, `[0,0,0,0,1,1,2]`, - `[0,0,0,0,0,"2353"]`, } { var directive contentmapper.MappedDiagnosticDirective - assert.ErrorContains(t, json.Unmarshal([]byte(data), &directive), "diagnostic directive") + assert.ErrorContains(t, json.Unmarshal([]byte(data), &directive), "diagnostic directive tuple") } unusedIndex := 1 diff --git a/tsc/internal/testutil/contentmappertest/transforming.go b/tsc/internal/testutil/contentmappertest/transforming.go index f9f4da0bf089d..b7a66776c047d 100644 --- a/tsc/internal/testutil/contentmappertest/transforming.go +++ b/tsc/internal/testutil/contentmappertest/transforming.go @@ -3,6 +3,7 @@ package contentmappertest import ( "context" "fmt" + "strconv" "strings" "sync" @@ -187,6 +188,8 @@ func diagnosticDirectives(content string, mappings *spanmap.SpanMap) *contentmap []contentmapper.MappedDiagnosticDirective{{Policy: contentmapper.DiagnosticDirectivePolicyExpect, UnusedExpectDirectiveIndex: &index}}, contentmapper.UnusedExpectDirectiveDiagnostic{}, ) + case "invalid-diagnostic-code": + return wrap([]contentmapper.MappedDiagnosticDirective{{Policy: contentmapper.DiagnosticDirectivePolicyIgnore, DiagnosticCodes: []int32{0}}}) case "overlap": return wrap([]contentmapper.MappedDiagnosticDirective{ {VirtualEnd: 2, Policy: contentmapper.DiagnosticDirectivePolicyIgnore}, @@ -210,10 +213,18 @@ func diagnosticDirectives(content string, mappings *spanmap.SpanMap) *contentmap var policy contentmapper.DiagnosticDirectivePolicy hasPolicy := false unusedDiagnosticIndex := -1 + var diagnosticCodes []int32 switch { - case trimmed == ignorePrefix: + case trimmed == ignorePrefix || strings.HasPrefix(trimmed, ignorePrefix+" "): policy = contentmapper.DiagnosticDirectivePolicyIgnore hasPolicy = true + for field := range strings.FieldsSeq(strings.TrimPrefix(trimmed, ignorePrefix)) { + code, err := strconv.ParseInt(field, 10, 32) + if err != nil { + panic(err) + } + diagnosticCodes = append(diagnosticCodes, int32(code)) + } case strings.HasPrefix(trimmed, expectPrefix+":"): policy = contentmapper.DiagnosticDirectivePolicyExpect hasPolicy = true @@ -232,11 +243,12 @@ func diagnosticDirectives(content string, mappings *spanmap.SpanMap) *contentmap virtualSpans := mappings.OriginalToVirtualSpans(core.NewTextRange(affectedStart, affectedStart+affectedLength), spanmap.FeatureAll) if len(virtualSpans) == 1 { directive := contentmapper.MappedDiagnosticDirective{ - OriginalStart: lineStart, - OriginalLength: lineEnd - lineStart, - VirtualStart: virtualSpans[0].Span.Pos(), - VirtualEnd: virtualSpans[0].Span.End(), - Policy: policy, + OriginalStart: lineStart, + OriginalLength: lineEnd - lineStart, + VirtualStart: virtualSpans[0].Span.Pos(), + VirtualEnd: virtualSpans[0].Span.End(), + Policy: policy, + DiagnosticCodes: diagnosticCodes, } if unusedDiagnosticIndex >= 0 { directive.UnusedExpectDirectiveIndex = &unusedDiagnosticIndex diff --git a/tsc/testdata/baselines/reference/compiler/contentMapperDiagnosticDirectiveCodes.contentmapper b/tsc/testdata/baselines/reference/compiler/contentMapperDiagnosticDirectiveCodes.contentmapper new file mode 100644 index 0000000000000..8cabac47981e7 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/contentMapperDiagnosticDirectiveCodes.contentmapper @@ -0,0 +1,21 @@ +//// [/widget.box] (ScriptKind: ScriptKindTS, ContentMapper: [.box]) +--- Original --- +// @box-ignore 2304 +const one: number = [missingName]; + +// @box-ignore 2304 2322 +const both: number = [missingName]; +--- Transformed --- +const __VERSION = "1.0.0"; +// @box-ignore 2304 +const one: number = [missingName]; + +// @box-ignore 2304 2322 +const both: number = [missingName]; + +=== Diagnostics === + +/widget.box:2:7 - error TS2322: Type 'any[]' is not assignable to type 'number'. + +2 const one: number = [missingName]; + ~~~ diff --git a/tsc/testdata/baselines/reference/compiler/contentMapperInvalidDiagnosticDirectives.contentmapper b/tsc/testdata/baselines/reference/compiler/contentMapperInvalidDiagnosticDirectives.contentmapper index 180f687f380ae..469f23a3624f1 100644 --- a/tsc/testdata/baselines/reference/compiler/contentMapperInvalidDiagnosticDirectives.contentmapper +++ b/tsc/testdata/baselines/reference/compiler/contentMapperInvalidDiagnosticDirectives.contentmapper @@ -23,6 +23,11 @@ // @box-invalid-directive: overlap --- Transformed --- +//// [/invalidDiagnosticCode.invalidcode] (ScriptKind: ScriptKindTS, ContentMapper: [.invalidcode]) +--- Original --- +// @box-invalid-directive: invalid-diagnostic-code +--- Transformed --- + === Diagnostics === /expectWithoutUnusedDiagnostic.expectmissing:1:1 - error TS18069: The content mapper 'mapper' failed to transform this file. @@ -31,6 +36,12 @@ 1 ~ +/invalidDiagnosticCode.invalidcode:1:1 - error TS18069: The content mapper 'mapper' failed to transform this file. + Diagnostic directive 0 returned by the content mapper has a diagnostic code selector that must be positive. + +1 + ~ + /invalidPolicy.invalidpolicy:1:1 - error TS18069: The content mapper 'mapper' failed to transform this file. The content mapper returned a diagnostic directive with invalid policy '2'. diff --git a/tsc/testdata/tests/cases/compiler/contentMapperDiagnosticDirectiveCodes.ts b/tsc/testdata/tests/cases/compiler/contentMapperDiagnosticDirectiveCodes.ts new file mode 100644 index 0000000000000..c5d9261f9d7e7 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/contentMapperDiagnosticDirectiveCodes.ts @@ -0,0 +1,29 @@ +// @runExternalCode: true +// @noTypesAndSymbols: true + +// @Filename: /tsconfig.json +{ + "compilerOptions": { + "target": "es2020", + "module": "esnext", + "moduleResolution": "bundler", + "strict": true + }, + "contentMappers": [ + { "package": "mapper", "extensions": [".box"] } + ] +} + +// @Filename: /node_modules/mapper/package.json +{ + "name": "mapper", + "version": "1.0.0", + "typescript": { "contentMapper": { "exec": ["compiler-test-mapper"], "compilerOptions": ["target", "jsx"] } } +} + +// @Filename: /widget.box +// @box-ignore 2304 +const one: number = [missingName]; + +// @box-ignore 2304 2322 +const both: number = [missingName]; diff --git a/tsc/testdata/tests/cases/compiler/contentMapperInvalidDiagnosticDirectives.ts b/tsc/testdata/tests/cases/compiler/contentMapperInvalidDiagnosticDirectives.ts index a32c1023f36ca..84ab3add31a03 100644 --- a/tsc/testdata/tests/cases/compiler/contentMapperInvalidDiagnosticDirectives.ts +++ b/tsc/testdata/tests/cases/compiler/contentMapperInvalidDiagnosticDirectives.ts @@ -14,7 +14,8 @@ { "package": "mapper", "extensions": [".invalidpolicy"] }, { "package": "mapper", "extensions": [".expectmissing"] }, { "package": "mapper", "extensions": [".invalidunusedindex"] }, - { "package": "mapper", "extensions": [".overlap"] } + { "package": "mapper", "extensions": [".overlap"] }, + { "package": "mapper", "extensions": [".invalidcode"] } ] } @@ -41,4 +42,7 @@ // @box-invalid-directive: invalid-unused-diagnostic-index // @Filename: /overlap.overlap -// @box-invalid-directive: overlap \ No newline at end of file +// @box-invalid-directive: overlap + +// @Filename: /invalidDiagnosticCode.invalidcode +// @box-invalid-directive: invalid-diagnostic-code \ No newline at end of file From f4ae9cb712209b2caddfcd54fb56edd1f2273835 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 20 Nov 2026 16:38:44 -0800 Subject: [PATCH 7/7] Restore minimum position validation --- packages/typescript/schemas/contentmapper.schema.json | 2 ++ tools/scripts/tsc/generate-contentmapper.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/typescript/schemas/contentmapper.schema.json b/packages/typescript/schemas/contentmapper.schema.json index bebe6c7ddde91..91614d49a3599 100644 --- a/packages/typescript/schemas/contentmapper.schema.json +++ b/packages/typescript/schemas/contentmapper.schema.json @@ -462,11 +462,13 @@ { "type": "integer", "title": "originalStart", + "minimum": 0, "description": "Original start offset." }, { "type": "integer", "title": "originalLength", + "minimum": 0, "description": "Original span length." }, { diff --git a/tools/scripts/tsc/generate-contentmapper.ts b/tools/scripts/tsc/generate-contentmapper.ts index 404692df8841b..f0b5bdcc7a87c 100644 --- a/tools/scripts/tsc/generate-contentmapper.ts +++ b/tools/scripts/tsc/generate-contentmapper.ts @@ -278,7 +278,7 @@ function mappedDiagnosticDirectiveVariants(schema: Schema): { ignore: Schema; ex items.slice(0, 4).every((item, index) => item.title === ["originalStart", "originalLength", "virtualStart", "virtualEnd"][index] && item.type === "integer" - && JSON.stringify(item) === JSON.stringify(expectItems[index]) + && item.description === expectItems[index].description ), "MappedDiagnosticDirective policy variants must share range fields", );