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
This commit is contained in:
JoostK 2019-05-26 12:14:29 +02:00 committed by Misko Hevery
parent 4da5e9a156
commit 456f2e70af
2 changed files with 11 additions and 1 deletions

View File

@ -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));

View File

@ -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 = `<ng-template let-a></ng-template>`;
expect(tcb(TEMPLATE)).toContain('var _t2 = _t1.$implicit;');
});
it('should handle implicit vars when using microsyntax', () => {
const TEMPLATE = `<div *ngFor="let user of users"></div>`;
expect(tcb(TEMPLATE)).toContain('var _t2 = _t1.$implicit;');
});
it('should generate a forward element reference correctly', () => {
const TEMPLATE = `
{{ i.value }}