fix(ivy): don't crash on an unknown localref target (#33454)

Previously the template binder would crash when encountering an unknown
localref (# reference) such as `<div #ref="foo">` when no directive has
`exportAs: "foo"`.

With this commit, the compiler instead generates a template diagnostic error
informing the user about the invalid reference.

PR Close #33454
This commit is contained in:
Alex Rickabaugh
2019-10-28 15:27:55 -07:00
committed by atscott
parent 72eba7745f
commit 9db59d010d
9 changed files with 161 additions and 20 deletions

View File

@ -3137,6 +3137,24 @@ runInEachFileSystem(os => {
});
});
describe('local refs', () => {
it('should not generate an error when a local ref is unresolved' +
' (outside of template type-checking)',
() => {
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
template: '<div #ref="unknownTarget"></div>',
})
export class TestCmp {}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toBe(0);
});
});
describe('multiple local refs', () => {
const getComponentScript = (template: string): string => `
import {Component, Directive, NgModule} from '@angular/core';

View File

@ -714,6 +714,27 @@ export declare class AnimationEvent {
env.driveMain();
});
it('should report an error with an unknown local ref target', () => {
env.write('test.ts', `
import {Component, NgModule} from '@angular/core';
@Component({
selector: 'test',
template: '<div #ref="unknownTarget"></div>',
})
class TestCmp {}
@NgModule({
declarations: [TestCmp],
})
class Module {}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toBe(1);
expect(diags[0].messageText).toBe(`No directive found with exportAs 'unknownTarget'.`);
expect(getSourceCodeForDiagnostic(diags[0])).toBe('unknownTarget');
});
it('should report an error with pipe bindings', () => {
env.write('test.ts', `
import {CommonModule} from '@angular/common';