fix(decorators): stop directives inheriting parent class decorators.
Fixes #2291
This commit is contained in:
parent
9c19eb906b
commit
f7d7789915
@ -246,7 +246,7 @@ export function makeDecorator(annotationCls, chainFn: (fn: Function) => void = n
|
|||||||
isFunction(this) && this.annotations instanceof Array ? this.annotations : [];
|
isFunction(this) && this.annotations instanceof Array ? this.annotations : [];
|
||||||
chainAnnotation.push(annotationInstance);
|
chainAnnotation.push(annotationInstance);
|
||||||
var TypeDecorator: TypeDecorator = <TypeDecorator>function TypeDecorator(cls) {
|
var TypeDecorator: TypeDecorator = <TypeDecorator>function TypeDecorator(cls) {
|
||||||
var annotations = Reflect.getMetadata('annotations', cls);
|
var annotations = Reflect.getOwnMetadata('annotations', cls);
|
||||||
annotations = annotations || [];
|
annotations = annotations || [];
|
||||||
annotations.push(annotationInstance);
|
annotations.push(annotationInstance);
|
||||||
Reflect.defineMetadata('annotations', annotations, cls);
|
Reflect.defineMetadata('annotations', annotations, cls);
|
||||||
|
@ -7,6 +7,10 @@ import * as dirAnn from 'angular2/src/core/annotations_impl/annotations';
|
|||||||
class SomeDirective {
|
class SomeDirective {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Directive({selector: 'someChildDirective'})
|
||||||
|
class SomeChildDirective extends SomeDirective {
|
||||||
|
}
|
||||||
|
|
||||||
class SomeDirectiveWithoutAnnotation {}
|
class SomeDirectiveWithoutAnnotation {}
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
@ -24,5 +28,10 @@ export function main() {
|
|||||||
expect(() => { reader.resolve(SomeDirectiveWithoutAnnotation); })
|
expect(() => { reader.resolve(SomeDirectiveWithoutAnnotation); })
|
||||||
.toThrowError('No Directive annotation found on 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'}));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -23,16 +23,18 @@ class TerminalAnnotation {
|
|||||||
terminal = true;
|
terminal = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DecoratedParent {}
|
||||||
|
class DecoratedChild extends DecoratedParent {}
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
var Reflect = global.Reflect;
|
var Reflect = global.Reflect;
|
||||||
|
|
||||||
var TerminalDecorator = makeDecorator(TerminalAnnotation);
|
var TerminalDecorator = makeDecorator(TerminalAnnotation);
|
||||||
var TestDecorator = makeDecorator(TestAnnotation, (fn: any) => fn.Terminal = TerminalDecorator);
|
var TestDecorator = makeDecorator(TestAnnotation, (fn: any) => fn.Terminal = TerminalDecorator);
|
||||||
var TestParamDecorator = makeParamDecorator(TestAnnotation);
|
|
||||||
|
|
||||||
describe('decorators', () => {
|
describe('decorators', () => {
|
||||||
it('shoulld invoke as decorator', () => {
|
it('should invoke as decorator', () => {
|
||||||
function Type(){};
|
function Type() {}
|
||||||
TestDecorator({marker: 'WORKS'})(Type);
|
TestDecorator({marker: 'WORKS'})(Type);
|
||||||
var annotations = Reflect.getMetadata('annotations', Type);
|
var annotations = Reflect.getMetadata('annotations', Type);
|
||||||
expect(annotations[0].arg.marker).toEqual('WORKS');
|
expect(annotations[0].arg.marker).toEqual('WORKS');
|
||||||
@ -53,6 +55,15 @@ export function main() {
|
|||||||
expect(chain.annotations[1] instanceof TerminalAnnotation).toEqual(true);
|
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', () => {
|
describe('Class', () => {
|
||||||
it('should create a class', () => {
|
it('should create a class', () => {
|
||||||
var i0, i1;
|
var i0, i1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user