Skip to content

Commit 473dd3e

Browse files
atscottthePunderWoman
authored andcommitted
fix(compiler-cli): attach source spans to object literal keys in TCB
Previously, object literal keys in the TCB did not have source spans attached. This made it difficult for the Language Service to distinguish between keys and values, leading to incorrect completion contexts and diagnostic locations. This commit ensures that source spans are properly attached to the keys in the TCB.
1 parent 00905c1 commit 473dd3e

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

packages/compiler-cli/src/ngtsc/typecheck/src/expression.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ class AstTranslator implements AstVisitor {
244244
const value = this.translate(ast.values[idx]);
245245

246246
if (key.kind === 'property') {
247-
return ts.factory.createPropertyAssignment(ts.factory.createStringLiteral(key.key), value);
247+
const keyNode = ts.factory.createStringLiteral(key.key);
248+
addParseSpanInfo(keyNode, key.sourceSpan);
249+
return ts.factory.createPropertyAssignment(keyNode, value);
248250
} else {
249251
return ts.factory.createSpreadAssignment(value);
250252
}

packages/compiler-cli/src/ngtsc/typecheck/test/input_signal_diagnostics_spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ runInEachFileSystem(() => {
289289
<div dir [gen]="false" [other]="'text'"
290290
#ref="dir" (click)="ref.tester = {t: 1, u: 0}">`,
291291
expected: [
292-
`TestComponent.html(3, 61): Type 'number' is not assignable to type 'boolean'.`,
293-
`TestComponent.html(3, 67): Type 'number' is not assignable to type 'string'.`,
292+
`TestComponent.html(3, 58): Type 'number' is not assignable to type 'boolean'.`,
293+
`TestComponent.html(3, 64): Type 'number' is not assignable to type 'string'.`,
294294
],
295295
},
296296
{
@@ -311,8 +311,8 @@ runInEachFileSystem(() => {
311311
<div dir [gen]="false" [other]="'text'"
312312
#ref="dir" (click)="ref.tester = {t: 1, u: 0}">`,
313313
expected: [
314-
`TestComponent.html(3, 61): Type 'number' is not assignable to type 'boolean'.`,
315-
`TestComponent.html(3, 67): Type 'number' is not assignable to type 'string'.`,
314+
`TestComponent.html(3, 58): Type 'number' is not assignable to type 'boolean'.`,
315+
`TestComponent.html(3, 64): Type 'number' is not assignable to type 'string'.`,
316316
],
317317
},
318318
{
@@ -333,8 +333,8 @@ runInEachFileSystem(() => {
333333
<div dir [gen]="false" [other]="{u: null}"
334334
#ref="dir" (click)="ref.tester = {t: 1, u: 0}">`,
335335
expected: [
336-
`TestComponent.html(3, 57): Type 'number' is not assignable to type 'boolean'.`,
337-
`TestComponent.html(3, 63): Type 'number' is not assignable to type 'null'.`,
336+
`TestComponent.html(3, 54): Type 'number' is not assignable to type 'boolean'.`,
337+
`TestComponent.html(3, 60): Type 'number' is not assignable to type 'null'.`,
338338
],
339339
},
340340
// differing Write and ReadT
@@ -400,9 +400,9 @@ runInEachFileSystem(() => {
400400
component: `prop: HTMLElement = null!`,
401401
expected: [
402402
// This verifies that the `ref.tester.t` is correctly inferred to be `HTMLElement`.
403-
`TestComponent.html(3, 46): Type 'number' is not assignable to type 'HTMLElement'.`,
403+
`TestComponent.html(3, 43): Type 'number' is not assignable to type 'HTMLElement'.`,
404404
// This verifies that the `bla` input value is still a `string` when accessed.
405-
`TestComponent.html(3, 59): Type 'string' is not assignable to type 'never'.`,
405+
`TestComponent.html(3, 49): Type 'string' is not assignable to type 'never'.`,
406406
],
407407
},
408408
{
@@ -422,7 +422,7 @@ runInEachFileSystem(() => {
422422
component: `prop: HTMLElement = null!`,
423423
expected: [
424424
// This verifies that the `ref.tester.t` is correctly inferred to be `HTMLElement`.
425-
`TestComponent.html(1, 60): Type 'number' is not assignable to type 'HTMLElement'.`,
425+
`TestComponent.html(1, 57): Type 'number' is not assignable to type 'HTMLElement'.`,
426426
],
427427
},
428428
];

packages/compiler-cli/src/ngtsc/typecheck/test/model_signal_diagnostics_spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ runInEachFileSystem(() => {
285285
<div dir [gen]="false" [other]="'text'"
286286
#ref="dir" (click)="ref.tester = {t: 1, u: 0}">`,
287287
expected: [
288-
`TestComponent.html(3, 61): Type 'number' is not assignable to type 'boolean'.`,
289-
`TestComponent.html(3, 67): Type 'number' is not assignable to type 'string'.`,
288+
`TestComponent.html(3, 58): Type 'number' is not assignable to type 'boolean'.`,
289+
`TestComponent.html(3, 64): Type 'number' is not assignable to type 'string'.`,
290290
],
291291
},
292292
{
@@ -308,8 +308,8 @@ runInEachFileSystem(() => {
308308
<div dir [gen]="false" [other]="'text'"
309309
#ref="dir" (click)="ref.tester = {t: 1, u: 0}">`,
310310
expected: [
311-
`TestComponent.html(3, 61): Type 'number' is not assignable to type 'boolean'.`,
312-
`TestComponent.html(3, 67): Type 'number' is not assignable to type 'string'.`,
311+
`TestComponent.html(3, 58): Type 'number' is not assignable to type 'boolean'.`,
312+
`TestComponent.html(3, 64): Type 'number' is not assignable to type 'string'.`,
313313
],
314314
},
315315
{
@@ -334,8 +334,8 @@ runInEachFileSystem(() => {
334334
<div dir [gen]="false" [other]="{u: null}"
335335
#ref="dir" (click)="ref.tester = {t: 1, u: 0}">`,
336336
expected: [
337-
`TestComponent.html(3, 57): Type 'number' is not assignable to type 'boolean'.`,
338-
`TestComponent.html(3, 63): Type 'number' is not assignable to type 'null'.`,
337+
`TestComponent.html(3, 54): Type 'number' is not assignable to type 'boolean'.`,
338+
`TestComponent.html(3, 60): Type 'number' is not assignable to type 'null'.`,
339339
],
340340
},
341341
{
@@ -356,7 +356,7 @@ runInEachFileSystem(() => {
356356
component: `prop: HTMLElement = null!`,
357357
expected: [
358358
// This verifies that the `ref.tester.ts` is correctly inferred to be `HTMLElement`.
359-
`TestComponent.html(1, 60): Type 'number' is not assignable to type 'HTMLElement'.`,
359+
`TestComponent.html(1, 57): Type 'number' is not assignable to type 'HTMLElement'.`,
360360
],
361361
},
362362
{
@@ -490,8 +490,8 @@ runInEachFileSystem(() => {
490490
otherVal!: string;
491491
`,
492492
expected: [
493-
`TestComponent.html(3, 61): Type 'number' is not assignable to type 'boolean'.`,
494-
`TestComponent.html(3, 67): Type 'number' is not assignable to type 'string'.`,
493+
`TestComponent.html(3, 58): Type 'number' is not assignable to type 'boolean'.`,
494+
`TestComponent.html(3, 64): Type 'number' is not assignable to type 'string'.`,
495495
],
496496
},
497497
{
@@ -520,8 +520,8 @@ runInEachFileSystem(() => {
520520
otherVal!: string;
521521
`,
522522
expected: [
523-
`TestComponent.html(3, 61): Type 'number' is not assignable to type 'boolean'.`,
524-
`TestComponent.html(3, 67): Type 'number' is not assignable to type 'string'.`,
523+
`TestComponent.html(3, 58): Type 'number' is not assignable to type 'boolean'.`,
524+
`TestComponent.html(3, 64): Type 'number' is not assignable to type 'string'.`,
525525
],
526526
},
527527
{
@@ -550,8 +550,8 @@ runInEachFileSystem(() => {
550550
otherVal!: {u: null};
551551
`,
552552
expected: [
553-
`TestComponent.html(3, 57): Type 'number' is not assignable to type 'boolean'.`,
554-
`TestComponent.html(3, 63): Type 'number' is not assignable to type 'null'.`,
553+
`TestComponent.html(3, 54): Type 'number' is not assignable to type 'boolean'.`,
554+
`TestComponent.html(3, 60): Type 'number' is not assignable to type 'null'.`,
555555
],
556556
},
557557
{
@@ -572,7 +572,7 @@ runInEachFileSystem(() => {
572572
component: `prop: HTMLElement = null!`,
573573
expected: [
574574
// This verifies that the `ref.tester.ts` is correctly inferred to be `HTMLElement`.
575-
`TestComponent.html(1, 62): Type 'number' is not assignable to type 'HTMLElement'.`,
575+
`TestComponent.html(1, 59): Type 'number' is not assignable to type 'HTMLElement'.`,
576576
],
577577
},
578578
{

packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('type check blocks diagnostics', () => {
4040
// statement, which would wrap it into parenthesis that clutter the expected output.
4141
const TEMPLATE = '{{ m({foo: a, bar: b}) }}';
4242
expect(tcbWithSpans(TEMPLATE)).toContain(
43-
'(this).m /*3,4*/({ "foo": ((this).a /*11,12*/) /*11,12*/, "bar": ((this).b /*19,20*/) /*19,20*/ } /*5,21*/) /*3,22*/',
43+
'(this).m /*3,4*/({ "foo" /*6,9*/: ((this).a /*11,12*/) /*11,12*/, "bar" /*14,17*/: ((this).b /*19,20*/) /*19,20*/ } /*5,21*/) /*3,22*/',
4444
);
4545
});
4646

@@ -49,7 +49,7 @@ describe('type check blocks diagnostics', () => {
4949
// statement, which would wrap it into parenthesis that clutter the expected output.
5050
const TEMPLATE = '{{ m({a, b}) }}';
5151
expect(tcbWithSpans(TEMPLATE)).toContain(
52-
'((this).m /*3,4*/({ "a": ((this).a /*6,7*/) /*6,7*/, "b": ((this).b /*9,10*/) /*9,10*/ } /*5,11*/) /*3,12*/)',
52+
'((this).m /*3,4*/({ "a" /*6,7*/: ((this).a /*6,7*/) /*6,7*/, "b" /*9,10*/: ((this).b /*9,10*/) /*9,10*/ } /*5,11*/) /*3,12*/)',
5353
);
5454
});
5555

0 commit comments

Comments
 (0)