Skip to content

Commit

Permalink
fix(compiler): improve error message when a module imports itself (#1…
Browse files Browse the repository at this point in the history
…4646)

Closes #14644
  • Loading branch information
Dzmitry Shylovich authored and chuckjaz committed Mar 7, 2017
1 parent f2adb29 commit 6bc6482
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/@angular/compiler/src/metadata_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ export class CompileMetadataResolver {
}

if (importedModuleType) {
if (this._checkSelfImport(moduleType, importedModuleType)) return;
const importedModuleSummary = this.getNgModuleSummary(importedModuleType);
if (!importedModuleSummary) {
this._reportError(
Expand Down Expand Up @@ -567,6 +568,15 @@ export class CompileMetadataResolver {
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 {
if (this._directiveResolver.isDirective(type)) {
return 'directive';
Expand Down
10 changes: 10 additions & 0 deletions modules/@angular/compiler/test/metadata_resolver_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ export function main() {
.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',
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
@NgModule({declarations: [MyBrokenComp1]})
Expand All @@ -144,6 +153,7 @@ export function main() {
expect(() => resolver.loadNgModuleDirectiveAndPipeMetadata(SomeModule, true))
.toThrowError(`Can't resolve all parameters for MyBrokenComp1: (?).`);
}));

it('should throw with descriptive error message when a directive is passed to imports',
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
@NgModule({imports: [ComponentWithoutModuleId]})
Expand Down

0 comments on commit 6bc6482

Please sign in to comment.