From b3aaf561ee6fdfa5c1ad9b8c181c5e71acc468d2 Mon Sep 17 00:00:00 2001 From: ubugeeei Date: Mon, 16 Nov 2026 01:31:47 +0900 Subject: [PATCH 1/2] 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/2] 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,