From 3c15916e17c634a9c9419ea24835ac5f840a6978 Mon Sep 17 00:00:00 2001 From: Bowen Ni Date: Tue, 14 Mar 2017 17:12:18 -0700 Subject: [PATCH] fix(compiler): Improve error message for missing annotations (#14724) Currently, it says: Unexpected value 'FuzzyTimePipe in javascript/angular2/example/search/fuzzy_time.ts' declared by the module 'SearchModule in javascript/angular2/example/search/search_module.ts' The new error message also suggests: 'Please add a @Pipe/@Directive/@Component annotation.' --- packages/compiler/src/metadata_resolver.ts | 4 +-- .../compiler/test/metadata_resolver_spec.ts | 28 +++++++++++++++++-- packages/core/test/linker/integration_spec.ts | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/compiler/src/metadata_resolver.ts b/packages/compiler/src/metadata_resolver.ts index 16661fb64c..e51380b64d 100644 --- a/packages/compiler/src/metadata_resolver.ts +++ b/packages/compiler/src/metadata_resolver.ts @@ -433,7 +433,7 @@ export class CompileMetadataResolver { if (!importedModuleSummary) { this._reportError( syntaxError( - `Unexpected ${this._getTypeDescriptor(importedType)} '${stringifyType(importedType)}' imported by the module '${stringifyType(moduleType)}'`), + `Unexpected ${this._getTypeDescriptor(importedType)} '${stringifyType(importedType)}' imported by the module '${stringifyType(moduleType)}'. Please add a @NgModule annotation.`), moduleType); return; } @@ -491,7 +491,7 @@ export class CompileMetadataResolver { } else { this._reportError( syntaxError( - `Unexpected ${this._getTypeDescriptor(declaredType)} '${stringifyType(declaredType)}' declared by the module '${stringifyType(moduleType)}'`), + `Unexpected ${this._getTypeDescriptor(declaredType)} '${stringifyType(declaredType)}' declared by the module '${stringifyType(moduleType)}'. Please add a @Pipe/@Directive/@Component annotation.`), moduleType); return; } diff --git a/packages/compiler/test/metadata_resolver_spec.ts b/packages/compiler/test/metadata_resolver_spec.ts index 3cd1040e60..d7f2cad69e 100644 --- a/packages/compiler/test/metadata_resolver_spec.ts +++ b/packages/compiler/test/metadata_resolver_spec.ts @@ -162,7 +162,7 @@ export function main() { expect( () => resolver.loadNgModuleDirectiveAndPipeMetadata(ModuleWithImportedComponent, true)) .toThrowError( - `Unexpected directive 'ComponentWithoutModuleId' imported by the module 'ModuleWithImportedComponent'`); + `Unexpected directive 'ComponentWithoutModuleId' imported by the module 'ModuleWithImportedComponent'. Please add a @NgModule annotation.`); })); it('should throw with descriptive error message when a pipe is passed to imports', @@ -175,7 +175,7 @@ export function main() { } expect(() => resolver.loadNgModuleDirectiveAndPipeMetadata(ModuleWithImportedPipe, true)) .toThrowError( - `Unexpected pipe 'SomePipe' imported by the module 'ModuleWithImportedPipe'`); + `Unexpected pipe 'SomePipe' imported by the module 'ModuleWithImportedPipe'. Please add a @NgModule annotation.`); })); it('should throw with descriptive error message when a module is passed to declarations', @@ -188,7 +188,29 @@ export function main() { } expect(() => resolver.loadNgModuleDirectiveAndPipeMetadata(ModuleWithDeclaredModule, true)) .toThrowError( - `Unexpected module 'SomeModule' declared by the module 'ModuleWithDeclaredModule'`); + `Unexpected module 'SomeModule' declared by the module 'ModuleWithDeclaredModule'. Please add a @Pipe/@Directive/@Component annotation.`); + })); + + it('should throw with descriptive error message when a declared pipe is missing annotation', + inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => { + class SomePipe {} + @NgModule({declarations: [SomePipe]}) + class ModuleWithDeclaredModule { + } + expect(() => resolver.loadNgModuleDirectiveAndPipeMetadata(ModuleWithDeclaredModule, true)) + .toThrowError( + `Unexpected value 'SomePipe' declared by the module 'ModuleWithDeclaredModule'. Please add a @Pipe/@Directive/@Component annotation.`); + })); + + it('should throw with descriptive error message when an imported module is missing annotation', + inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => { + class SomeModule {} + @NgModule({imports: [SomeModule]}) + class ModuleWithImportedModule { + } + expect(() => resolver.loadNgModuleDirectiveAndPipeMetadata(ModuleWithImportedModule, true)) + .toThrowError( + `Unexpected value 'SomeModule' imported by the module 'ModuleWithImportedModule'. Please add a @NgModule annotation.`); })); it('should throw with descriptive error message when null is passed to declarations', diff --git a/packages/core/test/linker/integration_spec.ts b/packages/core/test/linker/integration_spec.ts index e828043a57..604b0eb770 100644 --- a/packages/core/test/linker/integration_spec.ts +++ b/packages/core/test/linker/integration_spec.ts @@ -1279,7 +1279,7 @@ function declareTests({useJit}: {useJit: boolean}) { expect(() => TestBed.createComponent(MyComp)) .toThrowError( - `Unexpected value '${stringify(SomeDirectiveMissingAnnotation)}' declared by the module 'DynamicTestModule'`); + `Unexpected value '${stringify(SomeDirectiveMissingAnnotation)}' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.`); }); it('should report a meaningful error when a component is missing view annotation', () => {