fix(compiler): short-circut expressions with an index (#13263)

Fixes #13254
This commit is contained in:
Chuck Jazdzewski 2016-12-06 10:40:15 -08:00 committed by Alex Rickabaugh
parent 4bd8f58552
commit f31c9470fa
2 changed files with 14 additions and 2 deletions

View File

@ -284,9 +284,14 @@ class _AstToIrVisitor implements cdAst.AstVisitor {
} }
visitKeyedRead(ast: cdAst.KeyedRead, mode: _Mode): any { visitKeyedRead(ast: cdAst.KeyedRead, mode: _Mode): any {
const leftMostSafe = this.leftMostSafeNode(ast);
if (leftMostSafe) {
return this.convertSafeAccess(ast, leftMostSafe, mode);
} else {
return convertToStatementIfNeeded( return convertToStatementIfNeeded(
mode, this.visit(ast.obj, _Mode.Expression).key(this.visit(ast.key, _Mode.Expression))); mode, this.visit(ast.obj, _Mode.Expression).key(this.visit(ast.key, _Mode.Expression)));
} }
}
visitKeyedWrite(ast: cdAst.KeyedWrite, mode: _Mode): any { visitKeyedWrite(ast: cdAst.KeyedWrite, mode: _Mode): any {
const obj: o.Expression = this.visit(ast.obj, _Mode.Expression); const obj: o.Expression = this.visit(ast.obj, _Mode.Expression);

View File

@ -305,6 +305,12 @@ export function main() {
expect(renderLog.log).toEqual(['someProp=null']); expect(renderLog.log).toEqual(['someProp=null']);
})); }));
it('should support short-circuting array index operations', fakeAsync(() => {
const ctx = _bindSimpleValue('value?.phones[0]', PersonHolder);
ctx.detectChanges(false);
expect(renderLog.log).toEqual(['someProp=null']);
}));
it('should still throw if right-side would throw', fakeAsync(() => { it('should still throw if right-side would throw', fakeAsync(() => {
expect(() => { expect(() => {
const ctx = _bindSimpleValue('value?.address.city', PersonHolder); const ctx = _bindSimpleValue('value?.address.city', PersonHolder);
@ -1515,6 +1521,7 @@ class Person {
age: number; age: number;
name: string; name: string;
address: Address = null; address: Address = null;
phones: number[];
init(name: string, address: Address = null) { init(name: string, address: Address = null) {
this.name = name; this.name = name;