8000 test(compiler-cli): add additional safe keyed read tests (#42421) · angular/angular@699a8b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 699a8b4

Browse files
crisbetodylhunn
authored andcommitted
test(compiler-cli): add additional safe keyed read tests (#42421)
This commit adds some tests that were mistakenly omitted from the original change for safe keyed reads/writes. PR Close #42421
1 parent 6b2a475 commit 699a8b4

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ describe('type check blocks diagnostics', () => {
129129
'(null as any ? (((ctx).a /*3,4*/) /*3,4*/)!.method /*6,12*/(((ctx).b /*13,14*/) /*13,14*/) : undefined) /*3,15*/');
130130
});
131131

132+
it('should annotate safe keyed reads', () => {
133+
const TEMPLATE = `{{ a?.[0] }}`;
134+
expect(tcbWithSpans(TEMPLATE))
135+
.toContain(
136+
'(null as any ? (((ctx).a /*3,4*/) /*3,4*/)![0 /*7,8*/] /*3,9*/ : undefined) /*3,9*/');
137+
});
138+
132139
it('should annotate $any casts', () => {
133140
const TEMPLATE = `{{ $any(a) }}`;
134141
expect(tcbWithSpans(TEMPLATE)).toContain('(((ctx).a /*8,9*/) /*8,9*/ as any) /*3,10*/');

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ runInEachFileSystem(() => {
394394
<div [inputA]="person?.address?.street"></div>
395395
<div [inputA]="person ? person.address : noPersonError"></div>
396396
<div [inputA]="person?.speak()"></div>
397+
<div [inputA]="person?.cars?.[1].engine"></div>
397398
`;
398399
const testValues = setup(
399400
[
@@ -405,9 +406,14 @@ runInEachFileSystem(() => {
405406
street: string;
406407
}
407408
409+
interface Car {
410+
engine: string;
411+
}
412+
408413
interface Person {
409414
address: Address;
410415
speak(): string;
416+
cars?: Car[];
411417
}
412418
export class Cmp {person?: Person; noPersonError = 'no person'}
413419
`,
@@ -448,6 +454,19 @@ runInEachFileSystem(() => {
448454
.toEqual('string | undefined');
449455
});
450456

457+
it('safe keyed reads', () => {
458+
const nodes = getAstElements(templateTypeChecker, cmp);
459+
const safeKeyedRead = nodes[3].inputs[0].value as ASTWithSource;
460+
const keyedReadSymbol = templateTypeChecker.getSymbolOfNode(safeKeyedRead, cmp)!;
461+
assertExpressionSymbol(keyedReadSymbol);
462+
expect(program.getTypeChecker().symbolToString(keyedReadSymbol.tsSymbol!))
463+
.toEqual('engine');
464+
expect((keyedReadSymbol.tsSymbol!.declarations![0] as ts.PropertyDeclaration)
465+
.parent.name!.getText())
466+
.toEqual('Car');
467+
expect(program.getTypeChecker().typeToString(keyedReadSymbol.tsType)).toEqual('string');
468+
});
469+
451470
it('ternary expressions', () => {
452471
const nodes = getAstElements(templateTypeChecker, cmp);
453472

packages/language-service/ivy/test/quick_info_spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,20 @@ describe('quick info', () => {
357357
expectedDisplayString: '(variable) name: { readonly name: "name"; }'
358358
});
359359
});
360+
361+
it('should work for safe keyed reads', () => {
362+
expectQuickInfo({
363+
templateOverride: `<div>{{constNames?.[0¦]}}</div>`,
364+
expectedSpanText: '0',
365+
expectedDisplayString: '(property) 0: {\n readonly name: "name";\n}'
366+
});
367+
368+
expectQuickInfo({
369+
templateOverride: `<div>{{constNames?.[0]?.na¦me}}</div>`,
370+
expectedSpanText: 'constNames?.[0]?.name',
371+
expectedDisplayString: '(property) name: "name"'
372+
});
373+
});
360374
});
361375

362376
describe('pipes', () => {

0 commit comments

Comments
 (0)
0