Skip to content

Commit 6a6b43d

Browse files
committed
feat(ElementInjector): throw if multiple directives define the same host injectable
relates to #2015
1 parent 309ef0f commit 6a6b43d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

modules/angular2/src/core/compiler/element_injector.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,14 @@ export class ProtoElementInjector {
466466

467467
private static _createHostInjectorBindingData(bindings: List<ResolvedBinding>,
468468
bd: List<BindingData>) {
469+
var visitedIds: Map<number, boolean> = MapWrapper.create();
469470
ListWrapper.forEach(bindings, b => {
470471
ListWrapper.forEach(b.resolvedHostInjectables, b => {
472+
if (MapWrapper.contains(visitedIds, b.key.id)) {
473+
throw new BaseException(
474+
`Multiple directives defined the same host injectable: "${stringify(b.key.token)}"`);
475+
}
476+
MapWrapper.set(visitedIds, b.key.id, true);
471477
ListWrapper.push(bd, new BindingData(ProtoElementInjector._createBinding(b), LIGHT_DOM));
472478
});
473479
});

modules/angular2/test/core/compiler/element_injector_spec.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class NeedsDirectiveFromParent {
106106
@Injectable()
107107
class NeedsDirectiveFromParentOrSelf {
108108
dependency: SimpleDirective;
109-
constructor(@Parent({self:true}) dependency: SimpleDirective) { this.dependency = dependency; }
109+
constructor(@Parent({self: true}) dependency: SimpleDirective) { this.dependency = dependency; }
110110
}
111111

112112
@Injectable()
@@ -478,8 +478,21 @@ export function main() {
478478
expect(pei.getBindingAtIndex(i).key.token).toBe(i);
479479
}
480480
});
481-
});
482481

482+
it('should throw whenever multiple directives declare the same host injectable', () => {
483+
expect(() => {
484+
createPei(null, 0, [
485+
DirectiveBinding.createFromType(SimpleDirective, new dirAnn.Component({
486+
hostInjector: [bind('injectable1').toValue('injectable1')]
487+
})),
488+
DirectiveBinding.createFromType(SomeOtherDirective, new dirAnn.Component({
489+
hostInjector: [bind('injectable1').toValue('injectable2')]
490+
}))
491+
]);
492+
}).toThrowError('Multiple directives defined the same host injectable: "injectable1"');
493+
});
494+
495+
});
483496
});
484497

485498
describe("ElementInjector", () => {

0 commit comments

Comments
 (0)