From 6a6b43de0765f2b0fd9b6a66820fb778a189fc53 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 2 Jun 2015 17:47:14 +0200 Subject: [PATCH] feat(ElementInjector): throw if multiple directives define the same host injectable relates to #2015 --- .../src/core/compiler/element_injector.ts | 6 ++++++ .../test/core/compiler/element_injector_spec.ts | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/modules/angular2/src/core/compiler/element_injector.ts b/modules/angular2/src/core/compiler/element_injector.ts index 20a42d4793..0e0e6279ab 100644 --- a/modules/angular2/src/core/compiler/element_injector.ts +++ b/modules/angular2/src/core/compiler/element_injector.ts @@ -466,8 +466,14 @@ export class ProtoElementInjector { private static _createHostInjectorBindingData(bindings: List, bd: List) { + var visitedIds: Map = 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)); }); }); diff --git a/modules/angular2/test/core/compiler/element_injector_spec.ts b/modules/angular2/test/core/compiler/element_injector_spec.ts index 7231514e77..dce49534f4 100644 --- a/modules/angular2/test/core/compiler/element_injector_spec.ts +++ b/modules/angular2/test/core/compiler/element_injector_spec.ts @@ -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", () => {