Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -43,12 +43,9 @@ export class HostDirectivesResolver {
for (const current of directives) {
const hostMeta = flattenInheritedDirectiveMetadata(this.metaReader, current.directive);

// This case has been checked for already, but we keep the assertion here so that the
// user gets a better error message than "Cannot read property foo of null" in case
// something slipped through.
// This case has been checked for already and produces a diagnostic
if (hostMeta === null) {
throw new Error(
`Could not resolve host directive metadata of ${current.directive.debugName}`);
continue;
}

if (hostMeta.hostDirectives) {
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-cli/src/ngtsc/metadata/src/inheritance.ts
Expand Up @@ -21,10 +21,10 @@ import {ClassPropertyMapping, ClassPropertyName} from './property_mapping';
* followed.
*/
export function flattenInheritedDirectiveMetadata(
reader: MetadataReader, dir: Reference<ClassDeclaration>): DirectiveMeta {
reader: MetadataReader, dir: Reference<ClassDeclaration>): DirectiveMeta|null {
const topMeta = reader.getDirectiveMetadata(dir);
if (topMeta === null) {
throw new Error(`Metadata not found for directive: ${dir.debugName}`);
return null;
}
if (topMeta.baseClass === null) {
return topMeta;
Expand Down
8 changes: 7 additions & 1 deletion packages/compiler-cli/src/ngtsc/scope/src/typecheck.ts
Expand Up @@ -99,6 +99,9 @@ export class TypeCheckScopeRegistry {
for (const meta of dependencies) {
if (meta.kind === MetaKind.Directive && meta.selector !== null) {
const extMeta = this.getTypeCheckDirectiveMetadata(meta.ref);
if (extMeta === null) {
continue;
}
matcher.addSelectables(
CssSelector.parse(meta.selector),
[...this.hostDirectivesResolver.resolve(extMeta), extMeta]);
Expand All @@ -125,13 +128,16 @@ export class TypeCheckScopeRegistry {
return typeCheckScope;
}

getTypeCheckDirectiveMetadata(ref: Reference<ClassDeclaration>): DirectiveMeta {
getTypeCheckDirectiveMetadata(ref: Reference<ClassDeclaration>): DirectiveMeta|null {
const clazz = ref.node;
if (this.flattenedDirectiveMetaCache.has(clazz)) {
return this.flattenedDirectiveMetaCache.get(clazz)!;
}

const meta = flattenInheritedDirectiveMetadata(this.metaReader, ref);
if (meta === null) {
return null;
}
this.flattenedDirectiveMetaCache.set(clazz, meta);
return meta;
}
Expand Down
18 changes: 18 additions & 0 deletions packages/compiler-cli/test/ngtsc/host_directives_spec.ts
Expand Up @@ -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';
Expand Down