From ea95c391c1c8ff754610e3cb301651c9aff3693e Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Tue, 6 Sep 2016 09:55:48 -0700 Subject: [PATCH] fix(compiler): error when `NgModule.bootstrap` contains `undefined` or `null` --- .../@angular/compiler/src/metadata_resolver.ts | 11 ++++++++--- .../compiler/test/metadata_resolver_spec.ts | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/modules/@angular/compiler/src/metadata_resolver.ts b/modules/@angular/compiler/src/metadata_resolver.ts index 88070a4543..0c71523f4e 100644 --- a/modules/@angular/compiler/src/metadata_resolver.ts +++ b/modules/@angular/compiler/src/metadata_resolver.ts @@ -304,9 +304,14 @@ export class CompileMetadataResolver { .map(type => this.getTypeMetadata(type, staticTypeModuleUrl(type)))); } if (meta.bootstrap) { - bootstrapComponents.push( - ...flattenArray(meta.bootstrap) - .map(type => this.getTypeMetadata(type, staticTypeModuleUrl(type)))); + const typeMetadata = flattenArray(meta.bootstrap).map(type => { + if (!isValidType(type)) { + throw new Error( + `Unexpected value '${stringify(type)}' used in the bootstrap property of module '${stringify(moduleType)}'`); + } + return this.getTypeMetadata(type, staticTypeModuleUrl(type)); + }); + bootstrapComponents.push(...typeMetadata); } entryComponents.push(...bootstrapComponents); if (meta.schemas) { diff --git a/modules/@angular/compiler/test/metadata_resolver_spec.ts b/modules/@angular/compiler/test/metadata_resolver_spec.ts index 025e74736a..9e0b0acb85 100644 --- a/modules/@angular/compiler/test/metadata_resolver_spec.ts +++ b/modules/@angular/compiler/test/metadata_resolver_spec.ts @@ -140,6 +140,23 @@ export function main() { `Invalid viewProviders for "MyBrokenComp4" - only instances of Provider and Type are allowed, got: [?null?, ...]`); })); + it('should throw with descriptive error message when null or undefined is passed to module bootstrap', + inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => { + @NgModule({bootstrap: [null]}) + class ModuleWithNullBootstrap { + } + @NgModule({bootstrap: [undefined]}) + class ModuleWithUndefinedBootstrap { + } + + expect(() => resolver.getNgModuleMetadata(ModuleWithNullBootstrap)) + .toThrowError( + `Unexpected value 'null' used in the bootstrap property of module 'ModuleWithNullBootstrap'`); + expect(() => resolver.getNgModuleMetadata(ModuleWithUndefinedBootstrap)) + .toThrowError( + `Unexpected value 'undefined' used in the bootstrap property of module 'ModuleWithUndefinedBootstrap'`); + })); + it('should throw an error when the interpolation config has invalid symbols', inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => { expect(() => resolver.getDirectiveMetadata(ComponentWithInvalidInterpolation1))