feat(ivy): support $any when type-checking templates (#29698)

This commit adds support in the template type-checking engine for the $any
cast operation.

Testing strategy: TCB tests included.

PR Close #29698
This commit is contained in:
Alex Rickabaugh 2019-04-04 15:53:43 -07:00 committed by Ben Lesh
parent bea85ffe9c
commit 42262e4e8c
2 changed files with 14 additions and 1 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {AST, BindingPipe, BindingType, BoundTarget, ImplicitReceiver, PropertyRead, TmplAstBoundAttribute, TmplAstBoundText, TmplAstElement, TmplAstNode, TmplAstReference, TmplAstTemplate, TmplAstTextAttribute, TmplAstVariable} from '@angular/compiler';
import {AST, BindingPipe, BindingType, BoundTarget, ImplicitReceiver, MethodCall, PropertyRead, TmplAstBoundAttribute, TmplAstBoundText, TmplAstElement, TmplAstNode, TmplAstReference, TmplAstTemplate, TmplAstTextAttribute, TmplAstVariable} from '@angular/compiler';
import * as ts from 'typescript';
import {Reference} from '../../imports';
@ -813,6 +813,13 @@ function tcbResolve(ast: AST, tcb: Context, scope: Scope): ts.Expression|null {
}
const args = ast.args.map(arg => tcbExpression(arg, tcb, scope));
return tsCallMethod(pipe, 'transform', [expr, ...args]);
} else if (
ast instanceof MethodCall && ast.receiver instanceof ImplicitReceiver &&
ast.name === '$any' && ast.args.length === 1) {
const expr = tcbExpression(ast.args[0], tcb, scope);
const exprAsAny =
ts.createAsExpression(expr, ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
return ts.createParen(exprAsAny);
} else {
// This AST isn't special after all.
return null;

View File

@ -76,6 +76,12 @@ describe('type check blocks', () => {
expect(block).not.toContain('.style = ');
});
it('should handle $any casts', () => {
const TEMPLATE = `{{$any(a)}}`;
const block = tcb(TEMPLATE);
expect(block).toContain('(ctx.a as any);');
});
describe('config', () => {
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',