From 456f2e70af222f3970efce5e23e989d31f2b1ed1 Mon Sep 17 00:00:00 2001 From: JoostK Date: Sun, 26 May 2019 12:14:29 +0200 Subject: [PATCH] fix(ivy): type checking - handle $implicit ng-template variables (#30675) When an `ng-template` element has a variable declaration without a value, it is assigned the value of the `$implicit` property in the embedded view's context. The template compiler inserts a property access to `$implicit` for template variables without a value, however the type-check code generation logic did not. This resulted in incorrect type-checking code being generated. Fixes FW-1326 PR Close #30675 --- .../src/ngtsc/typecheck/src/type_check_block.ts | 2 +- .../src/ngtsc/typecheck/test/type_check_block_spec.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts b/packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts index 34ceffb1f0..cb568f9911 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts @@ -122,7 +122,7 @@ class TcbVariableOp extends TcbOp { const id = this.tcb.allocateId(); const initializer = ts.createPropertyAccess( /* expression */ ctx, - /* name */ this.variable.value); + /* name */ this.variable.value || '$implicit'); // Declare the variable, and return its identifier. this.scope.addStatement(tsCreateVariable(id, initializer)); diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts index e17e4f1a02..d215831881 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts @@ -45,6 +45,16 @@ describe('type check blocks', () => { expect(tcb(TEMPLATE)).toContain('_t1.htmlFor = "test";'); }); + it('should handle implicit vars on ng-template', () => { + const TEMPLATE = ``; + expect(tcb(TEMPLATE)).toContain('var _t2 = _t1.$implicit;'); + }); + + it('should handle implicit vars when using microsyntax', () => { + const TEMPLATE = `
`; + expect(tcb(TEMPLATE)).toContain('var _t2 = _t1.$implicit;'); + }); + it('should generate a forward element reference correctly', () => { const TEMPLATE = ` {{ i.value }}