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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/typescript/src/api/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export class RemoteSourceFile extends RemoteNode implements SourceFileInfo {
const count = reader.readArrayHeader();
const directives = Array<MappedDiagnosticDirective>(count);
for (let i = 0; i < count; i++) {
if (reader.readArrayHeader() !== 6) throw new Error("Invalid diagnostic directive");
if (reader.readArrayHeader() !== 7) throw new Error("Invalid diagnostic directive");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intended tuples to be open-ended, not even needing a capability advertisement in order to grow the length. If you move diagnosticCode after unusedCode, and change this to be >= 7, it seems like that would work.

const originalStart = reader.readUint();
const originalLength = reader.readUint();
const virtualStart = reader.readUint();
Expand All @@ -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(),
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript/src/api/node/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const PROTOCOL_VERSION = 9;
export const PROTOCOL_VERSION = 10;

export const HEADER_OFFSET_METADATA = 0;
export const HEADER_OFFSET_HASH_LO0 = 4;
Expand Down
1 change: 1 addition & 0 deletions packages/typescript/src/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export interface MappedDiagnosticDirective {
readonly originalRange: ReadonlyTextRange;
readonly virtualRange: ReadonlyTextRange;
readonly policy: DiagnosticDirectivePolicy;
readonly diagnosticCode: number;
Comment thread
ubugeeei marked this conversation as resolved.
Resolved
readonly unusedCode: number;
}

Expand Down
34 changes: 34 additions & 0 deletions packages/typescript/test/encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions tsc/internal/api/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const (
)

const (
ProtocolVersion uint8 = 9
ProtocolVersion uint8 = 10
)

// Source File Binary Format
Expand Down Expand Up @@ -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).
//
Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand Down
42 changes: 29 additions & 13 deletions tsc/internal/api/encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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
})
}
Expand Down
1 change: 1 addition & 0 deletions tsc/internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,7 @@ type MappedDiagnosticDirective struct {
OriginalRange core.TextRange
VirtualRange core.TextRange
Policy MappedDiagnosticDirectivePolicy
DiagnosticCode int32
UnusedCode int32
UnusedMessageText string
Source string
Expand Down
57 changes: 57 additions & 0 deletions tsc/internal/compiler/contentmapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1013,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) {
Expand Down
2 changes: 2 additions & 0 deletions tsc/internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion tsc/internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions tsc/internal/contentmapper/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
DiagnosticDirectiveErrorKindExpectMissingUnusedDiagnostic
DiagnosticDirectiveErrorKindInvalidUnusedDiagnosticIndex
DiagnosticDirectiveErrorKindOverlap
DiagnosticDirectiveErrorKindInvalidDiagnosticCode
)

// DiagnosticDirectiveError reports an invalid diagnostic directive in a transform response.
Expand Down
61 changes: 49 additions & 12 deletions tsc/internal/contentmapper/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"`))
}
Expand Down
Loading