feat(core): support the decorator data that tsickle produces
This commit is contained in:
@ -121,6 +121,17 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
|||||||
if (isPresent((<any>typeOrFunc).parameters)) {
|
if (isPresent((<any>typeOrFunc).parameters)) {
|
||||||
return (<any>typeOrFunc).parameters;
|
return (<any>typeOrFunc).parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// API of tsickle for lowering decorators to properties on the class.
|
||||||
|
if (isPresent((<any>typeOrFunc).ctorParameters)) {
|
||||||
|
let ctorParameters = (<any>typeOrFunc).ctorParameters;
|
||||||
|
let paramTypes = ctorParameters.map( ctorParam => ctorParam && ctorParam.type );
|
||||||
|
let paramAnnotations = ctorParameters.map( ctorParam =>
|
||||||
|
ctorParam && convertTsickleDecoratorIntoMetadata(ctorParam.decorators) );
|
||||||
|
return this._zipTypesAndAnnotations(paramTypes, paramAnnotations);
|
||||||
|
}
|
||||||
|
|
||||||
|
// API for metadata created by invoking the decorators.
|
||||||
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
|
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
|
||||||
var paramAnnotations = this._reflect.getMetadata('parameters', typeOrFunc);
|
var paramAnnotations = this._reflect.getMetadata('parameters', typeOrFunc);
|
||||||
var paramTypes = this._reflect.getMetadata('design:paramtypes', typeOrFunc);
|
var paramTypes = this._reflect.getMetadata('design:paramtypes', typeOrFunc);
|
||||||
@ -143,6 +154,13 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
|||||||
}
|
}
|
||||||
return annotations;
|
return annotations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// API of tsickle for lowering decorators to properties on the class.
|
||||||
|
if (isPresent((<any>typeOrFunc).decorators)) {
|
||||||
|
return convertTsickleDecoratorIntoMetadata((<any>typeOrFunc).decorators);
|
||||||
|
}
|
||||||
|
|
||||||
|
// API for metadata created by invoking the decorators.
|
||||||
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
|
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
|
||||||
var annotations = this._reflect.getMetadata('annotations', typeOrFunc);
|
var annotations = this._reflect.getMetadata('annotations', typeOrFunc);
|
||||||
if (isPresent(annotations)) return annotations;
|
if (isPresent(annotations)) return annotations;
|
||||||
@ -159,6 +177,18 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
|||||||
}
|
}
|
||||||
return propMetadata;
|
return propMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// API of tsickle for lowering decorators to properties on the class.
|
||||||
|
if (isPresent((<any>typeOrFunc).propDecorators)) {
|
||||||
|
let propDecorators = (<any>typeOrFunc).propDecorators;
|
||||||
|
let propMetadata = <{[key: string]: any[]}>{};
|
||||||
|
Object.keys(propDecorators).forEach( prop => {
|
||||||
|
propMetadata[prop] = convertTsickleDecoratorIntoMetadata(propDecorators[prop]);
|
||||||
|
});
|
||||||
|
return propMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API for metadata created by invoking the decorators.
|
||||||
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
|
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
|
||||||
var propMetadata = this._reflect.getMetadata('propMetadata', typeOrFunc);
|
var propMetadata = this._reflect.getMetadata('propMetadata', typeOrFunc);
|
||||||
if (isPresent(propMetadata)) return propMetadata;
|
if (isPresent(propMetadata)) return propMetadata;
|
||||||
@ -185,3 +215,17 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
|||||||
// There is not a concept of import uri in Js, but this is useful in developing Dart applications.
|
// There is not a concept of import uri in Js, but this is useful in developing Dart applications.
|
||||||
importUri(type: Type): string { return `./${stringify(type)}`; }
|
importUri(type: Type): string { return `./${stringify(type)}`; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function convertTsickleDecoratorIntoMetadata(decoratorInvocations: any[]): any[] {
|
||||||
|
if (!decoratorInvocations) {
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -270,6 +270,7 @@ export function makeDecorator(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
DecoratorFactory.prototype = Object.create(annotationCls.prototype);
|
DecoratorFactory.prototype = Object.create(annotationCls.prototype);
|
||||||
|
(<any>DecoratorFactory).annotationCls = annotationCls;
|
||||||
return DecoratorFactory;
|
return DecoratorFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,15 +305,16 @@ export function makeParamDecorator(annotationCls): any {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ParamDecoratorFactory.prototype = Object.create(annotationCls.prototype);
|
ParamDecoratorFactory.prototype = Object.create(annotationCls.prototype);
|
||||||
|
(<any>ParamDecoratorFactory).annotationCls = annotationCls;
|
||||||
return ParamDecoratorFactory;
|
return ParamDecoratorFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function makePropDecorator(decoratorCls): any {
|
export function makePropDecorator(annotationCls): any {
|
||||||
function PropDecoratorFactory(...args): any {
|
function PropDecoratorFactory(...args): any {
|
||||||
var decoratorInstance = Object.create(decoratorCls.prototype);
|
var decoratorInstance = Object.create(annotationCls.prototype);
|
||||||
decoratorCls.apply(decoratorInstance, args);
|
annotationCls.apply(decoratorInstance, args);
|
||||||
|
|
||||||
if (this instanceof decoratorCls) {
|
if (this instanceof annotationCls) {
|
||||||
return decoratorInstance;
|
return decoratorInstance;
|
||||||
} else {
|
} else {
|
||||||
return function PropDecorator(target: any, name: string) {
|
return function PropDecorator(target: any, name: string) {
|
||||||
@ -324,6 +326,7 @@ export function makePropDecorator(decoratorCls): any {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PropDecoratorFactory.prototype = Object.create(decoratorCls.prototype);
|
PropDecoratorFactory.prototype = Object.create(annotationCls.prototype);
|
||||||
|
(<any>PropDecoratorFactory).annotationCls = annotationCls;
|
||||||
return PropDecoratorFactory;
|
return PropDecoratorFactory;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user