refactor(core): add a name to all decorators and other fixes
This commit is contained in:
parent
cc2873a94d
commit
42d442dcd5
|
@ -47,7 +47,7 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
|
|||
angularImportLocations() {
|
||||
return {
|
||||
coreDecorators: '@angular/core/src/metadata',
|
||||
diDecorators: '@angular/core/src/di/decorators',
|
||||
diDecorators: '@angular/core/src/di/metadata',
|
||||
diMetadata: '@angular/core/src/di/metadata',
|
||||
diOpaqueToken: '@angular/core/src/di/opaque_token',
|
||||
animationMetadata: '@angular/core/src/animation/metadata',
|
||||
|
|
|
@ -125,7 +125,7 @@ describe('reflector_host', () => {
|
|||
let {coreDecorators, diDecorators, diMetadata, animationMetadata, provider} =
|
||||
reflectorNestedGenDir.angularImportLocations();
|
||||
expect(coreDecorators).toEqual('@angular/core/src/metadata');
|
||||
expect(diDecorators).toEqual('@angular/core/src/di/decorators');
|
||||
expect(diDecorators).toEqual('@angular/core/src/di/metadata');
|
||||
expect(diMetadata).toEqual('@angular/core/src/di/metadata');
|
||||
expect(animationMetadata).toEqual('@angular/core/src/animation/metadata');
|
||||
expect(provider).toEqual('@angular/core/src/di/provider');
|
||||
|
|
|
@ -444,7 +444,7 @@ class MockReflectorHost implements StaticReflectorHost {
|
|||
angularImportLocations() {
|
||||
return {
|
||||
coreDecorators: 'angular2/src/core/metadata',
|
||||
diDecorators: 'angular2/src/core/di/decorators',
|
||||
diDecorators: 'angular2/src/core/di/metadata',
|
||||
diMetadata: 'angular2/src/core/di/metadata',
|
||||
diOpaqueToken: 'angular2/src/core/di/opaque_token',
|
||||
animationMetadata: 'angular2/src/core/animation/metadata',
|
||||
|
|
|
@ -74,7 +74,7 @@ export interface Inject { token: any; }
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Inject: InjectDecorator = makeParamDecorator([['token', undefined]]);
|
||||
export const Inject: InjectDecorator = makeParamDecorator('Inject', [['token', undefined]]);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -122,7 +122,7 @@ export interface Optional {}
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Optional: OptionalDecorator = makeParamDecorator([]);
|
||||
export const Optional: OptionalDecorator = makeParamDecorator('Optional', []);
|
||||
|
||||
/**
|
||||
* Type of the Injectable decorator / constructor function.
|
||||
|
@ -179,7 +179,7 @@ export interface Injectable {}
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Injectable: InjectableDecorator = makeParamDecorator([]);
|
||||
export const Injectable: InjectableDecorator = makeParamDecorator('Injectable', []);
|
||||
|
||||
/**
|
||||
* Type of the Self decorator / constructor function.
|
||||
|
@ -232,7 +232,7 @@ export interface Self {}
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Self: SelfDecorator = makeParamDecorator([]);
|
||||
export const Self: SelfDecorator = makeParamDecorator('Self', []);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -284,7 +284,7 @@ export interface SkipSelf {}
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const SkipSelf: SkipSelfDecorator = makeParamDecorator([]);
|
||||
export const SkipSelf: SkipSelfDecorator = makeParamDecorator('SkipSelf', []);
|
||||
|
||||
/**
|
||||
* Type of the Host decorator / constructor function.
|
||||
|
@ -362,4 +362,4 @@ export interface Host {}
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Host: HostDecorator = makeParamDecorator([]);
|
||||
export const Host: HostDecorator = makeParamDecorator('Host', []);
|
||||
|
|
|
@ -122,7 +122,8 @@ export interface Attribute { attributeName?: string; }
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Attribute: AttributeDecorator = makeParamDecorator([['attributeName', undefined]]);
|
||||
export const Attribute: AttributeDecorator =
|
||||
makeParamDecorator('Attribute', [['attributeName', undefined]]);
|
||||
|
||||
/**
|
||||
* Type of the Query metadata.
|
||||
|
@ -192,6 +193,7 @@ export type ContentChildren = Query;
|
|||
* @Annotation
|
||||
*/
|
||||
export const ContentChildren: ContentChildrenDecorator = makePropDecorator(
|
||||
'ContentChildren',
|
||||
[
|
||||
['selector', undefined],
|
||||
{first: false, isViewQuery: false, descendants: false, read: undefined}
|
||||
|
@ -251,6 +253,7 @@ export type ContentChild = Query;
|
|||
* @Annotation
|
||||
*/
|
||||
export const ContentChild: ContentChildDecorator = makePropDecorator(
|
||||
'ContentChild',
|
||||
[
|
||||
['selector', undefined], {
|
||||
first: true,
|
||||
|
@ -362,6 +365,7 @@ export type ViewChildren = Query;
|
|||
* @Annotation
|
||||
*/
|
||||
export const ViewChildren: ViewChildrenDecorator = makePropDecorator(
|
||||
'ViewChildren',
|
||||
[
|
||||
['selector', undefined], {
|
||||
first: false,
|
||||
|
@ -467,6 +471,7 @@ export type ViewChild = Query;
|
|||
* @Annotation
|
||||
*/
|
||||
export const ViewChild: ViewChildDecorator = makePropDecorator(
|
||||
'ViewChild',
|
||||
[
|
||||
['selector', undefined], {
|
||||
first: true,
|
||||
|
|
|
@ -770,7 +770,7 @@ export interface Directive {
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Directive: DirectiveDecorator = <DirectiveDecorator>makeDecorator({
|
||||
export const Directive: DirectiveDecorator = <DirectiveDecorator>makeDecorator('Directive', {
|
||||
selector: undefined,
|
||||
inputs: undefined,
|
||||
outputs: undefined,
|
||||
|
@ -1051,7 +1051,7 @@ export interface Component extends Directive {
|
|||
* @Annotation
|
||||
*/
|
||||
export const Component: ComponentDecorator = <ComponentDecorator>makeDecorator(
|
||||
{
|
||||
'Component', {
|
||||
selector: undefined,
|
||||
inputs: undefined,
|
||||
outputs: undefined,
|
||||
|
@ -1110,7 +1110,7 @@ export interface Pipe {
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Pipe: PipeDecorator = <PipeDecorator>makeDecorator({
|
||||
export const Pipe: PipeDecorator = <PipeDecorator>makeDecorator('Pipe', {
|
||||
name: undefined,
|
||||
pure: true,
|
||||
});
|
||||
|
@ -1184,7 +1184,8 @@ export interface Input {
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Input: InputDecorator = makePropDecorator([['bindingPropertyName', undefined]]);
|
||||
export const Input: InputDecorator =
|
||||
makePropDecorator('Input', [['bindingPropertyName', undefined]]);
|
||||
|
||||
/**
|
||||
* Type of the Output decorator / constructor function.
|
||||
|
@ -1249,7 +1250,8 @@ export interface Output { bindingPropertyName?: string; }
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const Output: OutputDecorator = makePropDecorator([['bindingPropertyName', undefined]]);
|
||||
export const Output: OutputDecorator =
|
||||
makePropDecorator('Output', [['bindingPropertyName', undefined]]);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -1310,7 +1312,7 @@ export interface HostBinding { hostPropertyName?: string; }
|
|||
* @Annotation
|
||||
*/
|
||||
export const HostBinding: HostBindingDecorator =
|
||||
makePropDecorator([['hostPropertyName', undefined]]);
|
||||
makePropDecorator('HostBinding', [['hostPropertyName', undefined]]);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -1374,4 +1376,4 @@ export interface HostListener {
|
|||
* @Annotation
|
||||
*/
|
||||
export const HostListener: HostListenerDecorator =
|
||||
makePropDecorator([['eventName', undefined], ['args', []]]);
|
||||
makePropDecorator('HostListener', [['eventName', undefined], ['args', []]]);
|
||||
|
|
|
@ -191,7 +191,7 @@ export interface NgModule {
|
|||
* @stable
|
||||
* @Annotation
|
||||
*/
|
||||
export const NgModule: NgModuleDecorator = <NgModuleDecorator>makeDecorator({
|
||||
export const NgModule: NgModuleDecorator = <NgModuleDecorator>makeDecorator('NgModule', {
|
||||
providers: undefined,
|
||||
declarations: undefined,
|
||||
imports: undefined,
|
||||
|
|
|
@ -182,11 +182,9 @@ function convertTsickleDecoratorIntoMetadata(decoratorInvocations: any[]): any[]
|
|||
return [];
|
||||
}
|
||||
return decoratorInvocations.map(decoratorInvocation => {
|
||||
var decoratorType = decoratorInvocation.type;
|
||||
var annotationCls = decoratorType.annotationCls;
|
||||
var annotationArgs = decoratorInvocation.args ? decoratorInvocation.args : [];
|
||||
var annotation = Object.create(annotationCls.prototype);
|
||||
annotationCls.apply(annotation, annotationArgs);
|
||||
return annotation;
|
||||
const decoratorType = decoratorInvocation.type;
|
||||
const annotationCls = decoratorType.annotationCls;
|
||||
const annotationArgs = decoratorInvocation.args ? decoratorInvocation.args : [];
|
||||
return new annotationCls(...annotationArgs);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -252,19 +252,20 @@ export function Class(clsDef: ClassDefinition): Type<any> {
|
|||
var Reflect = global.Reflect;
|
||||
|
||||
export function makeDecorator(
|
||||
props: {[key: string]: any}, parentClass?: any,
|
||||
name: string, props: {[key: string]: any}, parentClass?: any,
|
||||
chainFn: (fn: Function) => void = null): (...args: any[]) => (cls: any) => any {
|
||||
const annotationCls = makeMetadataClass([props], parentClass);
|
||||
const metaCtor = makeMetadataCtor([props]);
|
||||
|
||||
function DecoratorFactory(objOrType: any): (cls: any) => any {
|
||||
if (!(Reflect && Reflect.getMetadata)) {
|
||||
throw 'reflect-metadata shim is required when using class decorators';
|
||||
}
|
||||
|
||||
const annotationInstance = new (<any>annotationCls)(objOrType);
|
||||
if (this instanceof annotationCls) {
|
||||
return annotationInstance;
|
||||
if (this instanceof DecoratorFactory) {
|
||||
metaCtor.call(this, objOrType);
|
||||
return this;
|
||||
} else {
|
||||
const annotationInstance = new (<any>DecoratorFactory)(objOrType);
|
||||
const chainAnnotation =
|
||||
isFunction(this) && this.annotations instanceof Array ? this.annotations : [];
|
||||
chainAnnotation.push(annotationInstance);
|
||||
|
@ -280,13 +281,15 @@ export function makeDecorator(
|
|||
return TypeDecorator;
|
||||
}
|
||||
}
|
||||
DecoratorFactory.prototype = annotationCls.prototype;
|
||||
(<any>DecoratorFactory).annotationCls = annotationCls;
|
||||
if (parentClass) {
|
||||
DecoratorFactory.prototype = Object.create(parentClass.prototype);
|
||||
}
|
||||
DecoratorFactory.prototype.toString = () => `@${name}`;
|
||||
(<any>DecoratorFactory).annotationCls = DecoratorFactory;
|
||||
return DecoratorFactory;
|
||||
}
|
||||
|
||||
function makeMetadataClass(
|
||||
props: ([string, any] | {[key: string]: any})[], parentClass?: any): any {
|
||||
function makeMetadataCtor(props: ([string, any] | {[key: string]: any})[]): any {
|
||||
function ctor(...args: any[]) {
|
||||
props.forEach((prop, i) => {
|
||||
const argVal = args[i];
|
||||
|
@ -302,24 +305,18 @@ function makeMetadataClass(
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (parentClass) {
|
||||
ctor.prototype = Object.create(parentClass.prototype);
|
||||
}
|
||||
|
||||
return ctor;
|
||||
}
|
||||
|
||||
export function makeParamDecorator(
|
||||
props: ([string, any] | {[key: string]: any})[], parentClass?: any): any {
|
||||
const annotationCls = makeMetadataClass(props, parentClass);
|
||||
name: string, props: ([string, any] | {[key: string]: any})[], parentClass?: any): any {
|
||||
const metaCtor = makeMetadataCtor(props);
|
||||
function ParamDecoratorFactory(...args: any[]): any {
|
||||
let annotationInstance = Object.create(annotationCls.prototype);
|
||||
annotationCls.apply(annotationInstance, args);
|
||||
|
||||
if (this instanceof annotationCls) {
|
||||
return annotationInstance;
|
||||
if (this instanceof ParamDecoratorFactory) {
|
||||
metaCtor.apply(this, args);
|
||||
return this;
|
||||
}
|
||||
const annotationInstance = new (<any>ParamDecoratorFactory)(...args);
|
||||
|
||||
(<any>ParamDecorator).annotation = annotationInstance;
|
||||
return ParamDecorator;
|
||||
|
@ -341,21 +338,23 @@ export function makeParamDecorator(
|
|||
return cls;
|
||||
}
|
||||
}
|
||||
ParamDecoratorFactory.prototype = annotationCls.prototype;
|
||||
(<any>ParamDecoratorFactory).annotationCls = annotationCls;
|
||||
if (parentClass) {
|
||||
ParamDecoratorFactory.prototype = Object.create(parentClass.prototype);
|
||||
}
|
||||
ParamDecoratorFactory.prototype.toString = () => `@${name}`;
|
||||
(<any>ParamDecoratorFactory).annotationCls = ParamDecoratorFactory;
|
||||
return ParamDecoratorFactory;
|
||||
}
|
||||
|
||||
export function makePropDecorator(
|
||||
props: ([string, any] | {[key: string]: any})[], parentClass?: any): any {
|
||||
const annotationCls = makeMetadataClass(props, parentClass);
|
||||
name: string, props: ([string, any] | {[key: string]: any})[], parentClass?: any): any {
|
||||
const metaCtor = makeMetadataCtor(props);
|
||||
function PropDecoratorFactory(...args: any[]): any {
|
||||
var decoratorInstance = Object.create(annotationCls.prototype);
|
||||
annotationCls.apply(decoratorInstance, args);
|
||||
|
||||
if (this instanceof annotationCls) {
|
||||
return decoratorInstance;
|
||||
if (this instanceof PropDecoratorFactory) {
|
||||
metaCtor.apply(this, args);
|
||||
return this;
|
||||
} else {
|
||||
var decoratorInstance = new (<any>PropDecoratorFactory)(...args);
|
||||
return function PropDecorator(target: any, name: string) {
|
||||
const meta = Reflect.getOwnMetadata('propMetadata', target.constructor) || {};
|
||||
meta[name] = meta[name] || [];
|
||||
|
@ -364,7 +363,10 @@ export function makePropDecorator(
|
|||
};
|
||||
}
|
||||
}
|
||||
PropDecoratorFactory.prototype = annotationCls.prototype;
|
||||
(<any>PropDecoratorFactory).annotationCls = annotationCls;
|
||||
if (parentClass) {
|
||||
PropDecoratorFactory.prototype = Object.create(parentClass.prototype);
|
||||
}
|
||||
PropDecoratorFactory.prototype.toString = () => `@${name}`;
|
||||
(<any>PropDecoratorFactory).annotationCls = PropDecoratorFactory;
|
||||
return PropDecoratorFactory;
|
||||
}
|
||||
|
|
|
@ -32,9 +32,11 @@ export function propDecorator(value: any /** TODO #9100 */) {
|
|||
}
|
||||
|
||||
/** @Annotation */ export const ClassDecorator =
|
||||
<ClassDecoratorFactory>makeDecorator({value: undefined});
|
||||
/** @Annotation */ export const ParamDecorator = makeParamDecorator([['value', undefined]]);
|
||||
/** @Annotation */ export const PropDecorator = makePropDecorator([['value', undefined]]);
|
||||
<ClassDecoratorFactory>makeDecorator('ClassDecorator', {value: undefined});
|
||||
/** @Annotation */ export const ParamDecorator =
|
||||
makeParamDecorator('ParamDecorator', [['value', undefined]]);
|
||||
/** @Annotation */ export const PropDecorator =
|
||||
makePropDecorator('PropDecorator', [['value', undefined]]);
|
||||
|
||||
// used only in Dart
|
||||
export class HasGetterAndSetterDecorators {}
|
||||
|
|
|
@ -19,9 +19,9 @@ class DecoratedChild extends DecoratedParent {}
|
|||
export function main() {
|
||||
var Reflect = global.Reflect;
|
||||
|
||||
var TerminalDecorator = makeDecorator({terminal: true});
|
||||
var TestDecorator =
|
||||
makeDecorator({marker: undefined}, Object, (fn: any) => fn.Terminal = TerminalDecorator);
|
||||
var TerminalDecorator = makeDecorator('TerminalDecorator', {terminal: true});
|
||||
var TestDecorator = makeDecorator(
|
||||
'TestDecorator', {marker: undefined}, Object, (fn: any) => fn.Terminal = TerminalDecorator);
|
||||
|
||||
describe('decorators', () => {
|
||||
it('should invoke as decorator', () => {
|
||||
|
|
|
@ -11,7 +11,5 @@ export function getTypeOf(instance: any /** TODO #9100 */) {
|
|||
}
|
||||
|
||||
export function instantiateType(type: Function, params: any[] = []) {
|
||||
var instance = Object.create(type.prototype);
|
||||
instance.constructor.apply(instance, params);
|
||||
return instance;
|
||||
return new (<any>type)(...params);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue