fix(compiler): Fixed ?. operator to short-circut execution (#9965)

Fixes: #9850
This commit is contained in:
Chuck Jazdzewski
2016-07-11 12:58:56 -07:00
committed by GitHub
parent e6b24437a9
commit 4ec2a30942
2 changed files with 219 additions and 56 deletions

View File

@ -277,6 +277,36 @@ export function main() {
ctx.detectChanges(false);
expect(renderLog.log).toEqual(['someProp=MTV']);
}));
it('should support short-circuting safe navigation', fakeAsync(() => {
const ctx = _bindSimpleValue('value?.address.city', PersonHolder);
ctx.componentInstance.value = null;
ctx.detectChanges(false);
expect(renderLog.log).toEqual(['someProp=null']);
}));
it('should support nested short-circuting safe navigation', fakeAsync(() => {
const ctx = _bindSimpleValue('value.value?.address.city', PersonHolderHolder);
ctx.componentInstance.value = new PersonHolder();
ctx.detectChanges(false);
expect(renderLog.log).toEqual(['someProp=null']);
}));
it('should support chained short-circuting safe navigation', fakeAsync(() => {
const ctx = _bindSimpleValue('value?.value?.address.city', PersonHolderHolder);
ctx.detectChanges(false);
expect(renderLog.log).toEqual(['someProp=null']);
}));
it('should still throw if right-side would throw', fakeAsync(() => {
expect(() => {
const ctx = _bindSimpleValue('value?.address.city', PersonHolder);
const person = new Person();
person.address = null;
ctx.componentInstance.value = person;
ctx.detectChanges(false);
}).toThrow();
}));
});
it('should support method calls', fakeAsync(() => {
@ -1460,3 +1490,22 @@ class Uninitialized {
class TestData {
public a: any;
}
@Component({selector: 'root'})
class TestDataWithGetter {
public fn: Function;
get a() { return this.fn(); }
}
class Holder<T> {
value: T;
}
@Component({selector: 'root'})
class PersonHolder extends Holder<Person> {
}
@Component({selector: 'root'})
class PersonHolderHolder extends Holder<Holder<Person>> {
}