Skip to content

Commit

Permalink
fix(decorators): stop directives inheriting parent class decorators.
Browse files Browse the repository at this point in the history
  • Loading branch information
jelbourn committed Jul 29, 2015
1 parent c5f8c95 commit 1688394
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion modules/angular2/src/util/decorators.ts
Expand Up @@ -246,7 +246,7 @@ export function makeDecorator(annotationCls, chainFn: (fn: Function) => void = n
isFunction(this) && this.annotations instanceof Array ? this.annotations : [];
chainAnnotation.push(annotationInstance);
var TypeDecorator: TypeDecorator = <TypeDecorator>function TypeDecorator(cls) {
var annotations = Reflect.getMetadata('annotations', cls);
var annotations = Reflect.getOwnMetadata('annotations', cls);
annotations = annotations || [];
annotations.push(annotationInstance);
Reflect.defineMetadata('annotations', annotations, cls);
Expand Down
Expand Up @@ -7,6 +7,10 @@ import * as dirAnn from 'angular2/src/core/annotations_impl/annotations';
class SomeDirective {
}

@Directive({selector: 'someChildDirective'})
class SomeChildDirective extends SomeDirective {
}

class SomeDirectiveWithoutAnnotation {}

export function main() {
Expand All @@ -24,5 +28,10 @@ export function main() {
expect(() => { reader.resolve(SomeDirectiveWithoutAnnotation); })
.toThrowError('No Directive annotation found on SomeDirectiveWithoutAnnotation');
});

it('should not read parent class Directive annotations', function() {
var directiveMetadata = reader.resolve(SomeChildDirective);
expect(directiveMetadata).toEqual(new dirAnn.Directive({selector: 'someChildDirective'}));
});
});
}
17 changes: 14 additions & 3 deletions modules/angular2/test/util/decorators_spec.ts
Expand Up @@ -23,16 +23,18 @@ class TerminalAnnotation {
terminal = true;
}

class DecoratedParent {}
class DecoratedChild extends DecoratedParent {}

export function main() {
var Reflect = global.Reflect;

var TerminalDecorator = makeDecorator(TerminalAnnotation);
var TestDecorator = makeDecorator(TestAnnotation, (fn: any) => fn.Terminal = TerminalDecorator);
var TestParamDecorator = makeParamDecorator(TestAnnotation);

describe('decorators', () => {
it('shoulld invoke as decorator', () => {
function Type(){};
it('should invoke as decorator', () => {
function Type() {}
TestDecorator({marker: 'WORKS'})(Type);
var annotations = Reflect.getMetadata('annotations', Type);
expect(annotations[0].arg.marker).toEqual('WORKS');
Expand All @@ -53,6 +55,15 @@ export function main() {
expect(chain.annotations[1] instanceof TerminalAnnotation).toEqual(true);
});

it('should not apply decorators from the prototype chain', function() {
TestDecorator({marker: 'parent'})(DecoratedParent);
TestDecorator({marker: 'child'})(DecoratedChild);

var annotations = Reflect.getOwnMetadata('annotations', DecoratedChild);
expect(annotations.length).toBe(1);
expect(annotations[0].arg.marker).toEqual('child');
});

describe('Class', () => {
it('should create a class', () => {
var i0, i1;
Expand Down

0 comments on commit 1688394

Please sign in to comment.