fix(compiler): improve error message when a module imports itself (#14646)

Closes #14644
This commit is contained in:
Dzmitry Shylovich 2017-03-07 04:00:25 +03:00 committed by Chuck Jazdzewski
parent f2adb2900d
commit 6bc6482765
2 changed files with 20 additions and 0 deletions

View File

@ -425,6 +425,7 @@ export class CompileMetadataResolver {
} }
if (importedModuleType) { if (importedModuleType) {
if (this._checkSelfImport(moduleType, importedModuleType)) return;
const importedModuleSummary = this.getNgModuleSummary(importedModuleType); const importedModuleSummary = this.getNgModuleSummary(importedModuleType);
if (!importedModuleSummary) { if (!importedModuleSummary) {
this._reportError( this._reportError(
@ -567,6 +568,15 @@ export class CompileMetadataResolver {
return compileMeta; return compileMeta;
} }
private _checkSelfImport(moduleType: Type<any>, importedModuleType: Type<any>): boolean {
if (moduleType === importedModuleType) {
this._reportError(
syntaxError(`'${stringifyType(moduleType)}' module can't import itself`), moduleType);
return true;
}
return false;
}
private _getTypeDescriptor(type: Type<any>): string { private _getTypeDescriptor(type: Type<any>): string {
if (this._directiveResolver.isDirective(type)) { if (this._directiveResolver.isDirective(type)) {
return 'directive'; return 'directive';

View File

@ -135,6 +135,15 @@ export function main() {
.toThrowError(`Expected 'styles' to be an array of strings.`); .toThrowError(`Expected 'styles' to be an array of strings.`);
})); }));
it('should throw with descriptive error message when a module imports itself',
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
@NgModule({imports: [SomeModule]})
class SomeModule {
}
expect(() => resolver.loadNgModuleDirectiveAndPipeMetadata(SomeModule, true))
.toThrowError(`'SomeModule' module can't import itself`);
}));
it('should throw with descriptive error message when provider token can not be resolved', it('should throw with descriptive error message when provider token can not be resolved',
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => { inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
@NgModule({declarations: [MyBrokenComp1]}) @NgModule({declarations: [MyBrokenComp1]})
@ -144,6 +153,7 @@ export function main() {
expect(() => resolver.loadNgModuleDirectiveAndPipeMetadata(SomeModule, true)) expect(() => resolver.loadNgModuleDirectiveAndPipeMetadata(SomeModule, true))
.toThrowError(`Can't resolve all parameters for MyBrokenComp1: (?).`); .toThrowError(`Can't resolve all parameters for MyBrokenComp1: (?).`);
})); }));
it('should throw with descriptive error message when a directive is passed to imports', it('should throw with descriptive error message when a directive is passed to imports',
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => { inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
@NgModule({imports: [ComponentWithoutModuleId]}) @NgModule({imports: [ComponentWithoutModuleId]})