Skip to content

Commit

Permalink
fix(compiler): properly report missing DI tokens
Browse files Browse the repository at this point in the history
Fixes #8245
  • Loading branch information
pkozlowski-opensource committed Jun 9, 2016
1 parent e213939 commit 16caa5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion modules/@angular/compiler/src/metadata_resolver.ts
Expand Up @@ -260,11 +260,12 @@ export class CompileMetadataResolver {

getDependenciesMetadata(typeOrFunc: Type | Function,
dependencies: any[]): cpl.CompileDiDependencyMetadata[] {
let hasUnknownDeps = false;
let params = isPresent(dependencies) ? dependencies : this._reflector.parameters(typeOrFunc);
if (isBlank(params)) {
params = [];
}
return params.map((param) => {
let dependenciesMetadata: cpl.CompileDiDependencyMetadata[] = params.map((param) => {
if (isBlank(param)) {
return null;
}
Expand Down Expand Up @@ -306,6 +307,7 @@ export class CompileMetadataResolver {
token = param;
}
if (isBlank(token)) {
hasUnknownDeps = true;
return null;
}
return new cpl.CompileDiDependencyMetadata({
Expand All @@ -320,6 +322,15 @@ export class CompileMetadataResolver {
});

});

if (hasUnknownDeps) {
let depsTokens = dependenciesMetadata.map((dep) => {
return dep ? stringify(dep.token) : '?';
}).join(', ');
throw new BaseException(`Can't resolve all parameters for ${stringify(typeOrFunc)}: (${depsTokens}).`);
}

return dependenciesMetadata;
}

getTokenMetadata(token: any): cpl.CompileTokenMetadata {
Expand Down
16 changes: 16 additions & 0 deletions modules/@angular/compiler/test/metadata_resolver_spec.ts
Expand Up @@ -78,6 +78,14 @@ export function main() {
.toThrowError(`Expected 'styles' to be an array of strings.`);
}
}));

it('should throw with descriptive error message when provider token can not be resolved',
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
if (!IS_DART) {
expect(() => resolver.getDirectiveMetadata(MyBrokenComp1))
.toThrowError(`Can't resolve all parameters for MyBrokenComp1: (?).`);
}
}));
});

describe('getViewDirectivesMetadata', () => {
Expand Down Expand Up @@ -148,3 +156,11 @@ class ComponentWithEverything implements OnChanges,
ngAfterViewInit(): void {}
ngAfterViewChecked(): void {}
}

interface NonExisting {
}

@Component({selector: 'my-broken-comp', template: ''})
class MyBrokenComp1 {
constructor(public dependency: NonExisting) {}
}

0 comments on commit 16caa5b

Please sign in to comment.