Skip to content

Commit b6fd811

Browse files
committed
feat(core): support the decorator data that tsickle produces
1 parent 3ae856a commit b6fd811

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

modules/@angular/core/src/reflection/reflection_capabilities.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
121121
if (isPresent((<any>typeOrFunc).parameters)) {
122122
return (<any>typeOrFunc).parameters;
123123
}
124+
125+
// API of tsickle for lowering decorators to properties on the class.
126+
if (isPresent((<any>typeOrFunc).ctorParameters)) {
127+
let ctorParameters = (<any>typeOrFunc).ctorParameters;
128+
let paramTypes = ctorParameters.map( ctorParam => ctorParam && ctorParam.type );
129+
let paramAnnotations = ctorParameters.map( ctorParam =>
130+
ctorParam && convertTsickleDecoratorIntoMetadata(ctorParam.decorators) );
131+
return this._zipTypesAndAnnotations(paramTypes, paramAnnotations);
132+
}
133+
134+
// API for metadata created by invoking the decorators.
124135
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
125136
var paramAnnotations = this._reflect.getMetadata('parameters', typeOrFunc);
126137
var paramTypes = this._reflect.getMetadata('design:paramtypes', typeOrFunc);
@@ -143,6 +154,13 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
143154
}
144155
return annotations;
145156
}
157+
158+
// API of tsickle for lowering decorators to properties on the class.
159+
if (isPresent((<any>typeOrFunc).decorators)) {
160+
return convertTsickleDecoratorIntoMetadata((<any>typeOrFunc).decorators);
161+
}
162+
163+
// API for metadata created by invoking the decorators.
146164
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
147165
var annotations = this._reflect.getMetadata('annotations', typeOrFunc);
148166
if (isPresent(annotations)) return annotations;
@@ -159,6 +177,18 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
159177
}
160178
return propMetadata;
161179
}
180+
181+
// API of tsickle for lowering decorators to properties on the class.
182+
if (isPresent((<any>typeOrFunc).propDecorators)) {
183+
let propDecorators = (<any>typeOrFunc).propDecorators;
184+
let propMetadata = <{[key: string]: any[]}>{};
185+
Object.keys(propDecorators).forEach( prop => {
186+
propMetadata[prop] = convertTsickleDecoratorIntoMetadata(propDecorators[prop]);
187+
});
188+
return propMetadata;
189+
}
190+
191+
// API for metadata created by invoking the decorators.
162192
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
163193
var propMetadata = this._reflect.getMetadata('propMetadata', typeOrFunc);
164194
if (isPresent(propMetadata)) return propMetadata;
@@ -185,3 +215,17 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
185215
// There is not a concept of import uri in Js, but this is useful in developing Dart applications.
186216
importUri(type: Type): string { return `./${stringify(type)}`; }
187217
}
218+
219+
function convertTsickleDecoratorIntoMetadata(decoratorInvocations: any[]): any[] {
220+
if (!decoratorInvocations) {
221+
return [];
222+
}
223+
return decoratorInvocations.map( decoratorInvocation => {
224+
var decoratorType = decoratorInvocation.type;
225+
var annotationCls = decoratorType.annotationCls;
226+
var annotationArgs = decoratorInvocation.args ? decoratorInvocation.args : [];
227+
var annotation = Object.create(annotationCls.prototype);
228+
annotationCls.apply(annotation, annotationArgs);
229+
return annotation;
230+
});
231+
}

modules/@angular/core/src/util/decorators.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ export function makeDecorator(
270270
}
271271
}
272272
DecoratorFactory.prototype = Object.create(annotationCls.prototype);
273+
(<any>DecoratorFactory).annotationCls = annotationCls;
273274
return DecoratorFactory;
274275
}
275276

@@ -304,15 +305,16 @@ export function makeParamDecorator(annotationCls): any {
304305
}
305306
}
306307
ParamDecoratorFactory.prototype = Object.create(annotationCls.prototype);
308+
(<any>ParamDecoratorFactory).annotationCls = annotationCls;
307309
return ParamDecoratorFactory;
308310
}
309311

310-
export function makePropDecorator(decoratorCls): any {
312+
export function makePropDecorator(annotationCls): any {
311313
function PropDecoratorFactory(...args): any {
312-
var decoratorInstance = Object.create(decoratorCls.prototype);
313-
decoratorCls.apply(decoratorInstance, args);
314+
var decoratorInstance = Object.create(annotationCls.prototype);
315+
annotationCls.apply(decoratorInstance, args);
314316

315-
if (this instanceof decoratorCls) {
317+
if (this instanceof annotationCls) {
316318
return decoratorInstance;
317319
} else {
318320
return function PropDecorator(target: any, name: string) {
@@ -324,6 +326,7 @@ export function makePropDecorator(decoratorCls): any {
324326
};
325327
}
326328
}
327-
PropDecoratorFactory.prototype = Object.create(decoratorCls.prototype);
329+
PropDecoratorFactory.prototype = Object.create(annotationCls.prototype);
330+
(<any>PropDecoratorFactory).annotationCls = annotationCls;
328331
return PropDecoratorFactory;
329332
}

0 commit comments

Comments
 (0)