From bf22f2df8833d222e8e98700be2ccbdbe678f5bb Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Mon, 6 Nov 2017 10:01:27 -0800 Subject: [PATCH] fix(compiler): report a reasonable error with invalid metadata (#20062) The compiler would throw an internal exception if an import using the `ngModule` syntax and the module as not a resolvable symbol. Fixes: #20049 --- packages/compiler/src/metadata_resolver.ts | 18 ++++++++++-------- .../compiler/test/metadata_resolver_spec.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/compiler/src/metadata_resolver.ts b/packages/compiler/src/metadata_resolver.ts index b64968eebe..f9d71b4fab 100644 --- a/packages/compiler/src/metadata_resolver.ts +++ b/packages/compiler/src/metadata_resolver.ts @@ -664,16 +664,18 @@ export class CompileMetadataResolver { } private _getTypeDescriptor(type: Type): string { - if (this.isDirective(type)) { - return 'directive'; - } + if (isValidType(type)) { + if (this.isDirective(type)) { + return 'directive'; + } - if (this.isPipe(type)) { - return 'pipe'; - } + if (this.isPipe(type)) { + return 'pipe'; + } - if (this.isNgModule(type)) { - return 'module'; + if (this.isNgModule(type)) { + return 'module'; + } } if ((type as any).provide) { diff --git a/packages/compiler/test/metadata_resolver_spec.ts b/packages/compiler/test/metadata_resolver_spec.ts index df9e3b1c3a..313d95c7d6 100644 --- a/packages/compiler/test/metadata_resolver_spec.ts +++ b/packages/compiler/test/metadata_resolver_spec.ts @@ -399,6 +399,18 @@ export function main() { expect(() => resolver.getNgModuleMetadata(ModuleWithComponentInBootstrap)).not.toThrow(); })); + + // #20049 + it('should throw a reasonable message when an invalid import is given', + inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => { + @NgModule({imports: [{ngModule: true as any}]}) + class InvalidModule { + } + + expect(() => { resolver.getNgModuleMetadata(InvalidModule); }) + .toThrowError( + `Unexpected value '[object Object]' imported by the module 'InvalidModule'. Please add a @NgModule annotation.`); + })); }); it('should dedupe declarations in NgModule',