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
This commit is contained in:
Kristiyan Kostadinov
2021-06-19 08:07:14 +02:00
committed by Dylan Hunn
parent 6b2a475532
commit 699a8b43cb
3 changed files with 40 additions and 0 deletions
@@ -129,6 +129,13 @@ describe('type check blocks diagnostics', () => {
'(null as any ? (((ctx).a /*3,4*/) /*3,4*/)!.method /*6,12*/(((ctx).b /*13,14*/) /*13,14*/) : undefined) /*3,15*/');
});
it('should annotate safe keyed reads', () => {
const TEMPLATE = `{{ a?.[0] }}`;
expect(tcbWithSpans(TEMPLATE))
.toContain(
'(null as any ? (((ctx).a /*3,4*/) /*3,4*/)![0 /*7,8*/] /*3,9*/ : undefined) /*3,9*/');
});
it('should annotate $any casts', () => {
const TEMPLATE = `{{ $any(a) }}`;
expect(tcbWithSpans(TEMPLATE)).toContain('(((ctx).a /*8,9*/) /*8,9*/ as any) /*3,10*/');
@@ -394,6 +394,7 @@ runInEachFileSystem(() => {
<div [inputA]="person?.address?.street"></div>
<div [inputA]="person ? person.address : noPersonError"></div>
<div [inputA]="person?.speak()"></div>
<div [inputA]="person?.cars?.[1].engine"></div>
`;
const testValues = setup(
[
@@ -405,9 +406,14 @@ runInEachFileSystem(() => {
street: string;
}
interface Car {
engine: string;
}
interface Person {
address: Address;
speak(): string;
cars?: Car[];
}
export class Cmp {person?: Person; noPersonError = 'no person'}
`,
@@ -448,6 +454,19 @@ runInEachFileSystem(() => {
.toEqual('string | undefined');
});
it('safe keyed reads', () => {
const nodes = getAstElements(templateTypeChecker, cmp);
const safeKeyedRead = nodes[3].inputs[0].value as ASTWithSource;
const keyedReadSymbol = templateTypeChecker.getSymbolOfNode(safeKeyedRead, cmp)!;
assertExpressionSymbol(keyedReadSymbol);
expect(program.getTypeChecker().symbolToString(keyedReadSymbol.tsSymbol!))
.toEqual('engine');
expect((keyedReadSymbol.tsSymbol!.declarations![0] as ts.PropertyDeclaration)
.parent.name!.getText())
.toEqual('Car');
expect(program.getTypeChecker().typeToString(keyedReadSymbol.tsType)).toEqual('string');
});
it('ternary expressions', () => {
const nodes = getAstElements(templateTypeChecker, cmp);