feat(ElementInjector): throw if multiple directives define the same host injectable

relates to #2015
This commit is contained in:
Victor Berchet 2015-06-02 17:47:14 +02:00
parent 309ef0f354
commit 6a6b43de07
2 changed files with 21 additions and 2 deletions

View File

@ -466,8 +466,14 @@ export class ProtoElementInjector {
private static _createHostInjectorBindingData(bindings: List<ResolvedBinding>,
bd: List<BindingData>) {
var visitedIds: Map<number, boolean> = MapWrapper.create();
ListWrapper.forEach(bindings, b => {
ListWrapper.forEach(b.resolvedHostInjectables, b => {
if (MapWrapper.contains(visitedIds, b.key.id)) {
throw new BaseException(
`Multiple directives defined the same host injectable: "${stringify(b.key.token)}"`);
}
MapWrapper.set(visitedIds, b.key.id, true);
ListWrapper.push(bd, new BindingData(ProtoElementInjector._createBinding(b), LIGHT_DOM));
});
});

View File

@ -106,7 +106,7 @@ class NeedsDirectiveFromParent {
@Injectable()
class NeedsDirectiveFromParentOrSelf {
dependency: SimpleDirective;
constructor(@Parent({self:true}) dependency: SimpleDirective) { this.dependency = dependency; }
constructor(@Parent({self: true}) dependency: SimpleDirective) { this.dependency = dependency; }
}
@Injectable()
@ -478,8 +478,21 @@ export function main() {
expect(pei.getBindingAtIndex(i).key.token).toBe(i);
}
});
});
it('should throw whenever multiple directives declare the same host injectable', () => {
expect(() => {
createPei(null, 0, [
DirectiveBinding.createFromType(SimpleDirective, new dirAnn.Component({
hostInjector: [bind('injectable1').toValue('injectable1')]
})),
DirectiveBinding.createFromType(SomeOtherDirective, new dirAnn.Component({
hostInjector: [bind('injectable1').toValue('injectable2')]
}))
]);
}).toThrowError('Multiple directives defined the same host injectable: "injectable1"');
});
});
});
describe("ElementInjector", () => {