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.'
This commit is contained in:
Bowen Ni 2017-03-14 17:12:18 -07:00 committed by Chuck Jazdzewski
parent ff60c041f6
commit 3c15916e17
3 changed files with 28 additions and 6 deletions

View File

@ -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;
}

View File

@ -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',

View File

@ -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', () => {