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:
@ -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';
|
||||
|
@ -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';
|
||||
|
Reference in New Issue
Block a user