Skip to content

Commit bf22f2d

Browse files
chuckjazvicb
authored andcommitted
fix(compiler): report a reasonable error with invalid metadata (angular#20062)
The compiler would throw an internal exception if an import using the `ngModule` syntax and the module as not a resolvable symbol. Fixes: angular#20049
1 parent 8802016 commit bf22f2d

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

packages/compiler/src/metadata_resolver.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -664,16 +664,18 @@ export class CompileMetadataResolver {
664664
}
665665

666666
private _getTypeDescriptor(type: Type): string {
667-
if (this.isDirective(type)) {
668-
return 'directive';
669-
}
667+
if (isValidType(type)) {
668+
if (this.isDirective(type)) {
669+
return 'directive';
670+
}
670671

671-
if (this.isPipe(type)) {
672-
return 'pipe';
673-
}
672+
if (this.isPipe(type)) {
673+
return 'pipe';
674+
}
674675

675-
if (this.isNgModule(type)) {
676-
return 'module';
676+
if (this.isNgModule(type)) {
677+
return 'module';
678+
}
677679
}
678680

679681
if ((type as any).provide) {

packages/compiler/test/metadata_resolver_spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,18 @@ export function main() {
399399

400400
expect(() => resolver.getNgModuleMetadata(ModuleWithComponentInBootstrap)).not.toThrow();
401401
}));
402+
403+
// #20049
404+
it('should throw a reasonable message when an invalid import is given',
405+
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
406+
@NgModule({imports: [{ngModule: true as any}]})
407+
class InvalidModule {
408+
}
409+
410+
expect(() => { resolver.getNgModuleMetadata(InvalidModule); })
411+
.toThrowError(
412+
`Unexpected value '[object Object]' imported by the module 'InvalidModule'. Please add a @NgModule annotation.`);
413+
}));
402414
});
403415

404416
it('should dedupe declarations in NgModule',

0 commit comments

Comments
 (0)