X Tutup
Skip to content

Commit f7d7789

Browse files
committed
fix(decorators): stop directives inheriting parent class decorators.
Fixes #2291
1 parent 9c19eb9 commit f7d7789

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

modules/angular2/src/util/decorators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export function makeDecorator(annotationCls, chainFn: (fn: Function) => void = n
246246
isFunction(this) && this.annotations instanceof Array ? this.annotations : [];
247247
chainAnnotation.push(annotationInstance);
248248
var TypeDecorator: TypeDecorator = <TypeDecorator>function TypeDecorator(cls) {
249-
var annotations = Reflect.getMetadata('annotations', cls);
249+
var annotations = Reflect.getOwnMetadata('annotations', cls);
250250
annotations = annotations || [];
251251
annotations.push(annotationInstance);
252252
Reflect.defineMetadata('annotations', annotations, cls);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import * as dirAnn from 'angular2/src/core/annotations_impl/annotations';
77
class SomeDirective {
88
}
99

10+
@Directive({selector: 'someChildDirective'})
11+
class SomeChildDirective extends SomeDirective {
12+
}
13+
1014
class SomeDirectiveWithoutAnnotation {}
1115

1216
export function main() {
@@ -24,5 +28,10 @@ export function main() {
2428
expect(() => { reader.resolve(SomeDirectiveWithoutAnnotation); })
2529
.toThrowError('No Directive annotation found on SomeDirectiveWithoutAnnotation');
2630
});
31+
32+
it('should not read parent class Directive annotations', function() {
33+
var directiveMetadata = reader.resolve(SomeChildDirective);
34+
expect(directiveMetadata).toEqual(new dirAnn.Directive({selector: 'someChildDirective'}));
35+
});
2736
});
2837
}

modules/angular2/test/util/decorators_spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ class TerminalAnnotation {
2323
terminal = true;
2424
}
2525

26+
class DecoratedParent {}
27+
class DecoratedChild extends DecoratedParent {}
28+
2629
export function main() {
2730
var Reflect = global.Reflect;
2831

2932
var TerminalDecorator = makeDecorator(TerminalAnnotation);
3033
var TestDecorator = makeDecorator(TestAnnotation, (fn: any) => fn.Terminal = TerminalDecorator);
31-
var TestParamDecorator = makeParamDecorator(TestAnnotation);
3234

3335
describe('decorators', () => {
34-
it('shoulld invoke as decorator', () => {
35-
function Type(){};
36+
it('should invoke as decorator', () => {
37+
function Type() {}
3638
TestDecorator({marker: 'WORKS'})(Type);
3739
var annotations = Reflect.getMetadata('annotations', Type);
3840
expect(annotations[0].arg.marker).toEqual('WORKS');
@@ -53,6 +55,15 @@ export function main() {
5355
expect(chain.annotations[1] instanceof TerminalAnnotation).toEqual(true);
5456
});
5557

58+
it('should not apply decorators from the prototype chain', function() {
59+
TestDecorator({marker: 'parent'})(DecoratedParent);
60+
TestDecorator({marker: 'child'})(DecoratedChild);
61+
62+
var annotations = Reflect.getOwnMetadata('annotations', DecoratedChild);
63+
expect(annotations.length).toBe(1);
64+
expect(annotations[0].arg.marker).toEqual('child');
65+
});
66+
5667
describe('Class', () => {
5768
it('should create a class', () => {
5869
var i0, i1;

0 commit comments

Comments
 (0)
X Tutup