fix(ivy): translate WriteKeyExpr expressions properly (#28523)

Translation of WriteKeyExpr expressions was not implemented in the ngtsc
expression translator. This resulted in binding expressions like
"target[key] = $event" not compiling.

This commit fixes the bug by implementing WriteKeyExpr translation.

PR Close #28523
This commit is contained in:
Alex Rickabaugh 2019-02-01 09:48:17 -08:00 committed by Misko Hevery
parent 3477610f6d
commit f8b67712bc
2 changed files with 27 additions and 2 deletions

View File

@ -166,8 +166,14 @@ class ExpressionTranslatorVisitor implements ExpressionVisitor, StatementVisitor
return context.isStatement ? result : ts.createParen(result);
}
visitWriteKeyExpr(expr: WriteKeyExpr, context: Context): never {
throw new Error('Method not implemented.');
visitWriteKeyExpr(expr: WriteKeyExpr, context: Context): ts.Expression {
const exprContext = context.withExpressionMode;
const lhs = ts.createElementAccess(
expr.receiver.visitExpression(this, exprContext),
expr.index.visitExpression(this, exprContext), );
const rhs = expr.value.visitExpression(this, exprContext);
const result: ts.Expression = ts.createBinary(lhs, ts.SyntaxKind.EqualsToken, rhs);
return context.isStatement ? result : ts.createParen(result);
}
visitWritePropExpr(expr: WritePropExpr, context: Context): ts.BinaryExpression {

View File

@ -863,6 +863,25 @@ describe('ngtsc behavioral tests', () => {
expect(jsContents).toMatch(contentQueryRegExp('ViewContainerRef', true));
});
it('should compile expressions that write keys', () => {
env.tsconfig();
env.write(`test.ts`, `
import {Component, ContentChild, TemplateRef, ViewContainerRef, forwardRef} from '@angular/core';
@Component({
selector: 'test',
template: '<div (click)="test[key] = $event">',
})
class TestCmp {
test: any;
key: string;
}
`);
env.driveMain();
expect(env.getContents('test.js')).toContain('test[key] = $event');
});
it('should generate host listeners for components', () => {
env.tsconfig();
env.write(`test.ts`, `