fix(compiler): error when `NgModule.bootstrap` contains `undefined` or `null`

This commit is contained in:
Tobias Bosch 2016-09-06 09:55:48 -07:00 committed by Martin Probst
parent aa9b617c9d
commit ea95c391c1
2 changed files with 25 additions and 3 deletions

View File

@ -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) {

View File

@ -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))