fix(compiler-cli): Produce diagnostic rather than crash when using invalid hostDirective (#48314)

Because the language service uses the compiler, we try to produce as
much useful information as possible rather than throwing hard errors.
Hard errors cause the compiler to crash. While this can be acceptable
when compiling a program as part of a regular build, this is undesirable
for the language service.

PR Close #48314
This commit is contained in:
Andrew Scott
2022-12-01 11:42:33 -08:00
committed by Andrew Kushnir
parent 38421578a2
commit 27eaded62d
4 changed files with 29 additions and 8 deletions
@@ -451,6 +451,24 @@ runInEachFileSystem(() => {
expect(messages).toEqual(['Host directive HostDir must be standalone']);
});
it('should produce a diagnostic if a host directive is not a directive', () => {
env.write('test.ts', `
import {Directive, Pipe, Component, NgModule} from '@angular/core';
@Pipe({name: 'hostDir'})
export class HostDir {}
@Directive({
hostDirectives: [HostDir],
})
export class Dir {}
`);
const messages = env.driveDiagnostics().map(extractMessage);
expect(messages).toEqual(
['HostDir must be a standalone directive to be used as a host directive']);
});
it('should produce a diagnostic if a host directive is a component', () => {
env.write('test.ts', `
import {Directive, Component, NgModule} from '@angular/core';