@alterior/annotations 3.13.3 → 3.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,700 +1,700 @@
1
- /// <reference types="reflect-metadata" />
2
- import { __decorate, __metadata } from "tslib";
3
- /**
4
- * @alterior/annotations
5
- * A class library for handling Typescript metadata decorators via "annotation" classes
6
- *
7
- * (C) 2017-2019 William Lahti
8
- *
9
- */
10
- import { NotSupportedError } from '@alterior/common';
11
- // These are the properties on a class where annotation metadata is deposited
12
- // when annotation decorators are executed. Note that these are intended to
13
- // be compatible with Angular 6's model
14
- export const ANNOTATIONS_KEY = '__annotations__';
15
- export const CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY = '__parameters__';
16
- export const PROPERTY_ANNOTATIONS_KEY = '__prop__metadata__';
17
- export const METHOD_PARAMETER_ANNOTATIONS_KEY = '__parameter__metadata__';
18
- ;
19
- /**
20
- * Thrown when a caller attempts to decorate an annotation target when the
21
- * annotation does not support that target.
22
- */
23
- export class AnnotationTargetError extends NotSupportedError {
24
- constructor(annotationClass, invalidType, supportedTypes, message) {
25
- super(message || `You cannot decorate a ${invalidType} with annotation ${annotationClass.name}. Valid targets: ${supportedTypes.join(', ')}`);
26
- this._invalidType = invalidType;
27
- this._annotationClass = annotationClass;
28
- this._supportedTypes = supportedTypes;
29
- }
30
- get invalidType() {
31
- return this._invalidType;
32
- }
33
- get supportedTypes() {
34
- return this._supportedTypes;
35
- }
36
- get annotationClass() {
37
- return this._annotationClass;
38
- }
39
- }
40
- /**
41
- * Create a decorator suitable for use along with an Annotation class.
42
- * This is the core of the Annotation.decorator() method.
43
- *
44
- * @param ctor
45
- * @param options
46
- */
47
- function makeDecorator(ctor, options) {
48
- if (!ctor)
49
- throw new Error(`Cannot create decorator: Passed class reference was undefined/null: This can happen due to circular dependencies.`);
50
- let factory = null;
51
- let validTargets = null;
52
- let allowMultiple = false;
53
- if (options) {
54
- if (options.factory)
55
- factory = options.factory;
56
- if (options.validTargets)
57
- validTargets = options.validTargets;
58
- if (options.allowMultiple)
59
- allowMultiple = options.allowMultiple;
60
- }
61
- if (!factory)
62
- factory = (target, ...args) => new ctor(...args);
63
- if (!validTargets)
64
- validTargets = ['class', 'method', 'property', 'parameter'];
65
- return (...decoratorArgs) => {
66
- return (target, ...args) => {
67
- // Note that checking the length is not enough, because for properties
68
- // two arguments are passed, but the property descriptor is `undefined`.
69
- // So we make sure that we have a valid property descriptor (args[1])
70
- if (args.length === 2 && args[1] !== undefined) {
71
- if (typeof args[1] === 'number') {
72
- // Parameter decorator on a method or a constructor (methodName will be undefined)
73
- let methodName = args[0];
74
- let index = args[1];
75
- if (!validTargets.includes('parameter'))
76
- throw new AnnotationTargetError(ctor, 'parameter', validTargets);
77
- if (!allowMultiple) {
78
- let existingParamDecs = Annotations.getParameterAnnotations(target, methodName, true);
79
- let existingParamAnnots = existingParamDecs[index] || [];
80
- if (existingParamAnnots.find(x => x.$metadataName === ctor['$metadataName']))
81
- throw new Error(`Annotation ${ctor.name} can only be applied to an element once.`);
82
- }
83
- if (methodName) {
84
- let annotation = factory({
85
- type: 'parameter',
86
- target,
87
- propertyKey: methodName,
88
- index
89
- }, ...decoratorArgs);
90
- if (!annotation)
91
- return;
92
- annotation.applyToParameter(target, methodName, index);
93
- }
94
- else {
95
- let annotation = factory({
96
- type: 'parameter',
97
- target,
98
- index
99
- }, ...decoratorArgs);
100
- if (!annotation)
101
- return;
102
- annotation.applyToConstructorParameter(target, index);
103
- }
104
- }
105
- else {
106
- // Method decorator
107
- let methodName = args[0];
108
- let descriptor = args[1];
109
- if (!validTargets.includes('method'))
110
- throw new AnnotationTargetError(ctor, 'method', validTargets);
111
- if (!allowMultiple) {
112
- let existingAnnots = Annotations.getMethodAnnotations(target, methodName, true);
113
- if (existingAnnots.find(x => x.$metadataName === ctor['$metadataName']))
114
- throw new Error(`Annotation ${ctor.name} can only be applied to an element once.`);
115
- }
116
- let annotation = factory({
117
- type: 'method',
118
- target,
119
- propertyKey: methodName,
120
- propertyDescriptor: descriptor
121
- }, ...decoratorArgs);
122
- if (!annotation)
123
- return;
124
- annotation.applyToMethod(target, methodName);
125
- }
126
- }
127
- else if (args.length >= 1) {
128
- // Property decorator
129
- let propertyKey = args[0];
130
- if (!validTargets.includes('property'))
131
- throw new AnnotationTargetError(ctor, 'property', validTargets);
132
- if (!allowMultiple) {
133
- let existingAnnots = Annotations.getPropertyAnnotations(target, propertyKey, true);
134
- if (existingAnnots.find(x => x.$metadataName === ctor['$metadataName']))
135
- throw new Error(`Annotation ${ctor.name} can only be applied to an element once.`);
136
- }
137
- let annotation = factory({
138
- type: 'property',
139
- target,
140
- propertyKey
141
- }, ...decoratorArgs);
142
- if (!annotation)
143
- return;
144
- annotation.applyToProperty(target, propertyKey);
145
- }
146
- else if (args.length === 0) {
147
- // Class decorator
148
- if (!validTargets.includes('class'))
149
- throw new AnnotationTargetError(ctor, 'class', validTargets);
150
- if (!allowMultiple) {
151
- let existingAnnots = Annotations.getClassAnnotations(target);
152
- if (existingAnnots.find(x => x.$metadataName === ctor['$metadataName']))
153
- throw new Error(`Annotation ${ctor.name} can only be applied to an element once.`);
154
- }
155
- let annotation = factory({
156
- type: 'class',
157
- target
158
- }, ...decoratorArgs);
159
- if (!annotation)
160
- return;
161
- annotation.applyToClass(target);
162
- }
163
- else {
164
- // Invalid, or future decorator types we don't support yet.
165
- throw new Error(`Encountered unknown decorator invocation with ${args.length + 1} parameters.`);
166
- }
167
- };
168
- };
169
- }
170
- export function MetadataName(name) {
171
- return target => Object.defineProperty(target, '$metadataName', { value: name });
172
- }
173
- /**
174
- * Represents a metadata annotation which can be applied to classes,
175
- * constructor parameters, methods, properties, or method parameters
176
- * via decorators.
177
- *
178
- * Custom annotations are defined as subclasses of this class.
179
- * By convention, all custom annotation classes should have a name
180
- * which ends in "Annotation" such as "NameAnnotation".
181
- *
182
- * To create a new annotation create a subclass of `Annotation`
183
- * with a constructor that takes the parameters you are interested in
184
- * storing, and save the appropriate information onto fields of the
185
- * new instance. For your convenience, Annotation provides a default
186
- * constructor which takes a map object and applies its properties onto
187
- * the current instance, but you may replace it with a constructor that
188
- * takes any arguments you wish.
189
- *
190
- * You may wish to add type safety to the default constructor parameter.
191
- * To do so, override the constructor and define it:
192
- *
193
- ```
194
- class XYZ extends Annotation {
195
- constructor(
196
- options : MyOptions
197
- ) {
198
- super(options);
199
- }
200
- }
201
- ```
202
- *
203
- * Annotations are applied by using decorators.
204
- * When you define a custom annotation, you must also define a
205
- * custom decorator:
206
- *
207
- ```
208
- const Name =
209
- NameAnnotation.decorator();
210
- ```
211
- * You can then use that decorator:
212
- ```
213
- @Name()
214
- class ABC {
215
- // ...
216
- }
217
- ```
218
- *
219
- */
220
- export class Annotation {
221
- constructor(props) {
222
- this.$metadataName = this.constructor['$metadataName'];
223
- if (!this.$metadataName || !this.$metadataName.includes(':')) {
224
- throw new Error(`You must specify a metadata name for this annotation in the form of `
225
- + ` 'mynamespace:myproperty'. You specified: '${this.$metadataName || '<none>'}'`);
226
- }
227
- Object.assign(this, props || {});
228
- }
229
- toString() {
230
- return `@${this.constructor.name}`;
231
- }
232
- static getMetadataName() {
233
- if (!this['$metadataName'])
234
- throw new Error(`Annotation subclass ${this.name} must have @MetadataName()`);
235
- return this['$metadataName'];
236
- }
237
- static decorator(options) {
238
- if (this === Annotation) {
239
- if (!options || !options.factory) {
240
- throw new Error(`When calling Annotation.decorator() to create a mutator, you must specify a factory (or use Mutator.decorator())`);
241
- }
242
- }
243
- return makeDecorator(this, options);
244
- }
245
- /**
246
- * Clone this annotation instance into a new one. This is not a deep copy.
247
- */
248
- clone() {
249
- return Annotations.clone(this);
250
- }
251
- /**
252
- * Apply this annotation to a given target.
253
- * @param target
254
- */
255
- applyToClass(target) {
256
- return Annotations.applyToClass(this, target);
257
- }
258
- /**
259
- * Apply this annotation instance to the given property.
260
- * @param target
261
- * @param name
262
- */
263
- applyToProperty(target, name) {
264
- return Annotations.applyToProperty(this, target, name);
265
- }
266
- /**
267
- * Apply this annotation instance to the given method.
268
- * @param target
269
- * @param name
270
- */
271
- applyToMethod(target, name) {
272
- return Annotations.applyToMethod(this, target, name);
273
- }
274
- /**
275
- * Apply this annotation instance to the given method parameter.
276
- * @param target
277
- * @param name
278
- * @param index
279
- */
280
- applyToParameter(target, name, index) {
281
- return Annotations.applyToParameter(this, target, name, index);
282
- }
283
- /**
284
- * Apply this annotation instance to the given constructor parameter.
285
- * @param target
286
- * @param name
287
- * @param index
288
- */
289
- applyToConstructorParameter(target, index) {
290
- return Annotations.applyToConstructorParameter(this, target, index);
291
- }
292
- /**
293
- * Filter the given list of annotations for the ones which match this annotation class
294
- * based on matching $metadataName.
295
- *
296
- * @param this
297
- * @param annotations
298
- */
299
- static filter(annotations) {
300
- return annotations.filter(x => x.$metadataName === this.getMetadataName());
301
- }
302
- /**
303
- * Get all instances of this annotation class attached to the given class.
304
- * If called on a subclass of Annotation, it returns only annotations that match
305
- * that subclass.
306
- * @param this
307
- * @param type The class to check
308
- */
309
- static getAllForClass(type) {
310
- return Annotations.getClassAnnotations(type)
311
- .filter(x => x.$metadataName === this.getMetadataName());
312
- }
313
- /**
314
- * Get a single instance of this annotation class attached to the given class.
315
- * If called on a subclass of Annotation, it returns only annotations that match
316
- * that subclass.
317
- *
318
- * @param this
319
- * @param type
320
- */
321
- static getForClass(type) {
322
- return this.getAllForClass(type)[0];
323
- }
324
- /**
325
- * Get all instances of this annotation class attached to the given method.
326
- * If called on a subclass of Annotation, it returns only annotations that match
327
- * that subclass.
328
- *
329
- * @param this
330
- * @param type The class where the method is defined
331
- * @param methodName The name of the method to check
332
- */
333
- static getAllForMethod(type, methodName) {
334
- return Annotations.getMethodAnnotations(type, methodName)
335
- .filter(x => x.$metadataName === this.getMetadataName());
336
- }
337
- /**
338
- * Get one instance of this annotation class attached to the given method.
339
- * If called on a subclass of Annotation, it returns only annotations that match
340
- * that subclass.
341
- *
342
- * @param this
343
- * @param type The class where the method is defined
344
- * @param methodName The name of the method to check
345
- */
346
- static getForMethod(type, methodName) {
347
- return this.getAllForMethod(type, methodName)[0];
348
- }
349
- /**
350
- * Get all instances of this annotation class attached to the given property.
351
- * If called on a subclass of Annotation, it returns only annotations that match
352
- * that subclass.
353
- *
354
- * @param this
355
- * @param type The class where the property is defined
356
- * @param propertyName The name of the property to check
357
- */
358
- static getAllForProperty(type, propertyName) {
359
- return Annotations.getPropertyAnnotations(type, propertyName)
360
- .filter(x => x.$metadataName === this.getMetadataName());
361
- }
362
- /**
363
- * Get one instance of this annotation class attached to the given property.
364
- * If called on a subclass of Annotation, it returns only annotations that match
365
- * that subclass.
366
- *
367
- * @param this
368
- * @param type The class where the property is defined
369
- * @param propertyName The name of the property to check
370
- */
371
- static getForProperty(type, propertyName) {
372
- return this.getAllForProperty(type, propertyName)[0];
373
- }
374
- /**
375
- * Get all instances of this annotation class attached to the parameters of the given method.
376
- * If called on a subclass of Annotation, it returns only annotations that match
377
- * that subclass.
378
- *
379
- * @param this
380
- * @param type The class where the method is defined
381
- * @param methodName The name of the method where parameter annotations should be checked for
382
- */
383
- static getAllForParameters(type, methodName) {
384
- return Annotations.getParameterAnnotations(type, methodName)
385
- .map(set => (set || []).filter(x => this === Annotation ? true : (x.$metadataName === this.getMetadataName())));
386
- }
387
- /**
388
- * Get all instances of this annotation class attached to the parameters of the constructor
389
- * for the given class.
390
- * If called on a subclass of Annotation, it returns only annotations that match
391
- * that subclass.
392
- *
393
- * @param this
394
- * @param type The class where constructor parameter annotations should be checked for
395
- */
396
- static getAllForConstructorParameters(type) {
397
- let finalSet = new Array(type.length).fill(undefined);
398
- let annotations = Annotations.getConstructorParameterAnnotations(type)
399
- .map(set => (set || []).filter(x => this === Annotation ? true : (x.$metadataName === this.getMetadataName())));
400
- for (let i = 0, max = annotations.length; i < max; ++i)
401
- finalSet[i] = annotations[i];
402
- return finalSet;
403
- }
404
- }
405
- /**
406
- * A helper class for managing annotations
407
- */
408
- export class Annotations {
409
- /**
410
- * Copy the annotations defined for one class onto another.
411
- * @param from The class to copy annotations from
412
- * @param to The class to copy annotations to
413
- */
414
- static copyClassAnnotations(from, to) {
415
- let annotations = Annotations.getClassAnnotations(from);
416
- annotations.forEach(x => Annotations.applyToClass(x, to));
417
- }
418
- /**
419
- * Apply this annotation to a given target.
420
- * @param target
421
- */
422
- static applyToClass(annotation, target) {
423
- let list = this.getOrCreateListForClass(target);
424
- let clone = this.clone(annotation);
425
- list.push(clone);
426
- if (Reflect.getOwnMetadata) {
427
- let reflectedAnnotations = Reflect.getOwnMetadata('annotations', target) || [];
428
- reflectedAnnotations.push({ toString() { return `${clone.$metadataName}`; }, annotation: clone });
429
- Reflect.defineMetadata('annotations', reflectedAnnotations, target);
430
- }
431
- return clone;
432
- }
433
- /**
434
- * Apply this annotation instance to the given property.
435
- * @param target
436
- * @param name
437
- */
438
- static applyToProperty(annotation, target, name) {
439
- let list = this.getOrCreateListForProperty(target, name);
440
- let clone = this.clone(annotation);
441
- list.push(clone);
442
- if (Reflect.getOwnMetadata) {
443
- let reflectedAnnotations = Reflect.getOwnMetadata('propMetadata', target, name) || [];
444
- reflectedAnnotations.push({ toString() { return `${clone.$metadataName}`; }, annotation: clone });
445
- Reflect.defineMetadata('propMetadata', reflectedAnnotations, target, name);
446
- }
447
- return clone;
448
- }
449
- /**
450
- * Apply this annotation instance to the given method.
451
- * @param target
452
- * @param name
453
- */
454
- static applyToMethod(annotation, target, name) {
455
- let list = this.getOrCreateListForMethod(target, name);
456
- let clone = Annotations.clone(annotation);
457
- list.push(clone);
458
- if (Reflect.getOwnMetadata && target.constructor) {
459
- const meta = Reflect.getOwnMetadata('propMetadata', target.constructor) || {};
460
- meta[name] = (meta.hasOwnProperty(name) && meta[name]) || [];
461
- meta[name].unshift({ toString() { return `${clone.$metadataName}`; }, annotation: clone });
462
- Reflect.defineMetadata('propMetadata', meta, target.constructor);
463
- }
464
- return clone;
465
- }
466
- /**
467
- * Apply this annotation instance to the given method parameter.
468
- * @param target
469
- * @param name
470
- * @param index
471
- */
472
- static applyToParameter(annotation, target, name, index) {
473
- let list = this.getOrCreateListForMethodParameters(target, name);
474
- while (list.length < index)
475
- list.push(null);
476
- let paramList = list[index] || [];
477
- let clone = this.clone(annotation);
478
- paramList.push(clone);
479
- list[index] = paramList;
480
- return clone;
481
- }
482
- /**
483
- * Apply this annotation instance to the given constructor parameter.
484
- * @param target
485
- * @param name
486
- * @param index
487
- */
488
- static applyToConstructorParameter(annotation, target, index) {
489
- let list = this.getOrCreateListForConstructorParameters(target);
490
- while (list.length < index)
491
- list.push(null);
492
- let paramList = list[index] || [];
493
- let clone = this.clone(annotation);
494
- paramList.push(clone);
495
- list[index] = paramList;
496
- if (Reflect.getOwnMetadata) {
497
- let parameterList = Reflect.getOwnMetadata('parameters', target) || [];
498
- while (parameterList.length < index)
499
- parameterList.push(null);
500
- let parameterAnnotes = parameterList[index] || [];
501
- parameterAnnotes.push(clone);
502
- parameterList[index] = parameterAnnotes;
503
- Reflect.defineMetadata('parameters', parameterList, target);
504
- }
505
- return clone;
506
- }
507
- /**
508
- * Clone the given Annotation instance into a new instance. This is not
509
- * a deep copy.
510
- *
511
- * @param annotation
512
- */
513
- static clone(annotation) {
514
- if (!annotation)
515
- return annotation;
516
- return Object.assign(Object.create(Object.getPrototypeOf(annotation)), annotation);
517
- }
518
- /**
519
- * Get all annotations (including from Angular and other compatible
520
- * frameworks).
521
- *
522
- * @param target The target to fetch annotations for
523
- */
524
- static getClassAnnotations(target) {
525
- return (this.getListForClass(target) || [])
526
- .map(x => this.clone(x));
527
- }
528
- /**
529
- * Get all annotations (including from Angular and other compatible
530
- * frameworks).
531
- *
532
- * @param target The target to fetch annotations for
533
- */
534
- static getMethodAnnotations(target, methodName, isStatic = false) {
535
- return (this.getListForMethod(isStatic ? target : target.prototype, methodName) || [])
536
- .map(x => this.clone(x));
537
- }
538
- /**
539
- * Get all annotations (including from Angular and other compatible
540
- * frameworks).
541
- *
542
- * @param target The target to fetch annotations for
543
- */
544
- static getPropertyAnnotations(target, methodName, isStatic = false) {
545
- return (this.getListForProperty(isStatic ? target : target.prototype, methodName) || [])
546
- .map(x => this.clone(x));
547
- }
548
- /**
549
- * Get the annotations defined on the parameters of the given method of the given
550
- * class.
551
- *
552
- * @param type
553
- * @param methodName
554
- * @param isStatic Whether `type` itself (isStatic = true), or `type.prototype` (isStatic = false) should be the target.
555
- * Note that passing true may indicate that the passed `type` is already the prototype of a class.
556
- */
557
- static getParameterAnnotations(type, methodName, isStatic = false) {
558
- return (this.getListForMethodParameters(isStatic ? type : type.prototype, methodName) || [])
559
- .map(set => set ? set.map(anno => this.clone(anno)) : []);
560
- }
561
- /**
562
- * Get the annotations defined on the parameters of the given method of the given
563
- * class.
564
- *
565
- * @param type
566
- * @param methodName
567
- */
568
- static getConstructorParameterAnnotations(type) {
569
- return (this.getListForConstructorParameters(type) || [])
570
- .map(set => set ? set.map(anno => this.clone(anno)) : []);
571
- }
572
- /**
573
- * Get a list of annotations for the given class.
574
- * @param target
575
- */
576
- static getListForClass(target) {
577
- if (!target)
578
- return [];
579
- let combinedSet = [];
580
- let superclass = Object.getPrototypeOf(target);
581
- if (superclass && superclass !== Function)
582
- combinedSet = combinedSet.concat(this.getListForClass(superclass));
583
- if (target.hasOwnProperty(ANNOTATIONS_KEY))
584
- combinedSet = combinedSet.concat(target[ANNOTATIONS_KEY] || []);
585
- return combinedSet;
586
- }
587
- /**
588
- * Get a list of own annotations for the given class, or create that list.
589
- * @param target
590
- */
591
- static getOrCreateListForClass(target) {
592
- if (!target.hasOwnProperty(ANNOTATIONS_KEY))
593
- Object.defineProperty(target, ANNOTATIONS_KEY, { enumerable: false, value: [] });
594
- return target[ANNOTATIONS_KEY];
595
- }
596
- /**
597
- * Gets a map of the annotations defined on all properties of the given class/function. To get the annotations of instance fields,
598
- * make sure to use `Class.prototype`, otherwise static annotations are returned.
599
- */
600
- static getMapForClassProperties(target, mapToPopulate) {
601
- let combinedSet = mapToPopulate || {};
602
- if (!target || target === Function)
603
- return combinedSet;
604
- this.getMapForClassProperties(Object.getPrototypeOf(target), combinedSet);
605
- if (target.hasOwnProperty(PROPERTY_ANNOTATIONS_KEY)) {
606
- let ownMap = target[PROPERTY_ANNOTATIONS_KEY] || {};
607
- for (let key of Object.keys(ownMap))
608
- combinedSet[key] = (combinedSet[key] || []).concat(ownMap[key]);
609
- }
610
- return combinedSet;
611
- }
612
- static getOrCreateMapForClassProperties(target) {
613
- if (!target.hasOwnProperty(PROPERTY_ANNOTATIONS_KEY))
614
- Object.defineProperty(target, PROPERTY_ANNOTATIONS_KEY, { enumerable: false, value: [] });
615
- return target[PROPERTY_ANNOTATIONS_KEY];
616
- }
617
- static getListForProperty(target, propertyKey) {
618
- let map = this.getMapForClassProperties(target);
619
- if (!map)
620
- return null;
621
- return map[propertyKey];
622
- }
623
- static getOrCreateListForProperty(target, propertyKey) {
624
- let map = this.getOrCreateMapForClassProperties(target);
625
- if (!map[propertyKey])
626
- map[propertyKey] = [];
627
- return map[propertyKey];
628
- }
629
- static getOrCreateListForMethod(target, methodName) {
630
- return this.getOrCreateListForProperty(target, methodName);
631
- }
632
- static getListForMethod(target, methodName) {
633
- return this.getListForProperty(target, methodName);
634
- }
635
- /**
636
- * Get a map of the annotations defined on all parameters of all methods of the given class/function.
637
- * To get instance methods, make sure to pass `Class.prototype`, otherwise the results are for static fields.
638
- */
639
- static getMapForMethodParameters(target, mapToPopulate) {
640
- let combinedMap = mapToPopulate || {};
641
- if (!target || target === Function)
642
- return combinedMap;
643
- // superclass/prototype
644
- this.getMapForMethodParameters(Object.getPrototypeOf(target), combinedMap);
645
- if (target.hasOwnProperty(METHOD_PARAMETER_ANNOTATIONS_KEY)) {
646
- let ownMap = target[METHOD_PARAMETER_ANNOTATIONS_KEY] || {};
647
- for (let methodName of Object.keys(ownMap)) {
648
- let parameters = ownMap[methodName];
649
- let combinedMethodMap = combinedMap[methodName] || [];
650
- for (let i = 0, max = parameters.length; i < max; ++i) {
651
- combinedMethodMap[i] = (combinedMethodMap[i] || []).concat(parameters[i] || []);
652
- }
653
- combinedMap[methodName] = combinedMethodMap;
654
- }
655
- }
656
- return combinedMap;
657
- }
658
- static getOrCreateMapForMethodParameters(target) {
659
- if (!target.hasOwnProperty(METHOD_PARAMETER_ANNOTATIONS_KEY))
660
- Object.defineProperty(target, METHOD_PARAMETER_ANNOTATIONS_KEY, { enumerable: false, value: {} });
661
- return target[METHOD_PARAMETER_ANNOTATIONS_KEY];
662
- }
663
- static getListForMethodParameters(target, methodName) {
664
- let map = this.getMapForMethodParameters(target);
665
- if (!map)
666
- return null;
667
- return map[methodName];
668
- }
669
- static getOrCreateListForMethodParameters(target, methodName) {
670
- let map = this.getOrCreateMapForMethodParameters(target);
671
- if (!map[methodName])
672
- map[methodName] = [];
673
- return map[methodName];
674
- }
675
- static getOrCreateListForConstructorParameters(target) {
676
- if (!target[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY])
677
- Object.defineProperty(target, CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY, { enumerable: false, value: [] });
678
- return target[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY];
679
- }
680
- static getListForConstructorParameters(target) {
681
- return target[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY];
682
- }
683
- }
684
- /**
685
- * An annotation for attaching a label to a programmatic element.
686
- * Can be queried with LabelAnnotation.getForClass() for example.
687
- */
688
- let LabelAnnotation = class LabelAnnotation extends Annotation {
689
- constructor(text) {
690
- super();
691
- this.text = text;
692
- }
693
- };
694
- LabelAnnotation = __decorate([
695
- MetadataName('alterior:Label'),
696
- __metadata("design:paramtypes", [String])
697
- ], LabelAnnotation);
698
- export { LabelAnnotation };
699
- export const Label = LabelAnnotation.decorator();
1
+ /// <reference types="reflect-metadata" />
2
+ import { __decorate, __metadata } from "tslib";
3
+ /**
4
+ * @alterior/annotations
5
+ * A class library for handling Typescript metadata decorators via "annotation" classes
6
+ *
7
+ * (C) 2017-2019 William Lahti
8
+ *
9
+ */
10
+ import { NotSupportedError } from '@alterior/common';
11
+ // These are the properties on a class where annotation metadata is deposited
12
+ // when annotation decorators are executed. Note that these are intended to
13
+ // be compatible with Angular 6's model
14
+ export const ANNOTATIONS_KEY = '__annotations__';
15
+ export const CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY = '__parameters__';
16
+ export const PROPERTY_ANNOTATIONS_KEY = '__prop__metadata__';
17
+ export const METHOD_PARAMETER_ANNOTATIONS_KEY = '__parameter__metadata__';
18
+ ;
19
+ /**
20
+ * Thrown when a caller attempts to decorate an annotation target when the
21
+ * annotation does not support that target.
22
+ */
23
+ export class AnnotationTargetError extends NotSupportedError {
24
+ constructor(annotationClass, invalidType, supportedTypes, message) {
25
+ super(message || `You cannot decorate a ${invalidType} with annotation ${annotationClass.name}. Valid targets: ${supportedTypes.join(', ')}`);
26
+ this._invalidType = invalidType;
27
+ this._annotationClass = annotationClass;
28
+ this._supportedTypes = supportedTypes;
29
+ }
30
+ get invalidType() {
31
+ return this._invalidType;
32
+ }
33
+ get supportedTypes() {
34
+ return this._supportedTypes;
35
+ }
36
+ get annotationClass() {
37
+ return this._annotationClass;
38
+ }
39
+ }
40
+ /**
41
+ * Create a decorator suitable for use along with an Annotation class.
42
+ * This is the core of the Annotation.decorator() method.
43
+ *
44
+ * @param ctor
45
+ * @param options
46
+ */
47
+ function makeDecorator(ctor, options) {
48
+ if (!ctor)
49
+ throw new Error(`Cannot create decorator: Passed class reference was undefined/null: This can happen due to circular dependencies.`);
50
+ let factory = null;
51
+ let validTargets = null;
52
+ let allowMultiple = false;
53
+ if (options) {
54
+ if (options.factory)
55
+ factory = options.factory;
56
+ if (options.validTargets)
57
+ validTargets = options.validTargets;
58
+ if (options.allowMultiple)
59
+ allowMultiple = options.allowMultiple;
60
+ }
61
+ if (!factory)
62
+ factory = (target, ...args) => new ctor(...args);
63
+ if (!validTargets)
64
+ validTargets = ['class', 'method', 'property', 'parameter'];
65
+ return (...decoratorArgs) => {
66
+ return (target, ...args) => {
67
+ // Note that checking the length is not enough, because for properties
68
+ // two arguments are passed, but the property descriptor is `undefined`.
69
+ // So we make sure that we have a valid property descriptor (args[1])
70
+ if (args.length === 2 && args[1] !== undefined) {
71
+ if (typeof args[1] === 'number') {
72
+ // Parameter decorator on a method or a constructor (methodName will be undefined)
73
+ let methodName = args[0];
74
+ let index = args[1];
75
+ if (!validTargets.includes('parameter'))
76
+ throw new AnnotationTargetError(ctor, 'parameter', validTargets);
77
+ if (!allowMultiple) {
78
+ let existingParamDecs = Annotations.getParameterAnnotations(target, methodName, true);
79
+ let existingParamAnnots = existingParamDecs[index] || [];
80
+ if (existingParamAnnots.find(x => x.$metadataName === ctor['$metadataName']))
81
+ throw new Error(`Annotation ${ctor.name} can only be applied to an element once.`);
82
+ }
83
+ if (methodName) {
84
+ let annotation = factory({
85
+ type: 'parameter',
86
+ target,
87
+ propertyKey: methodName,
88
+ index
89
+ }, ...decoratorArgs);
90
+ if (!annotation)
91
+ return;
92
+ annotation.applyToParameter(target, methodName, index);
93
+ }
94
+ else {
95
+ let annotation = factory({
96
+ type: 'parameter',
97
+ target,
98
+ index
99
+ }, ...decoratorArgs);
100
+ if (!annotation)
101
+ return;
102
+ annotation.applyToConstructorParameter(target, index);
103
+ }
104
+ }
105
+ else {
106
+ // Method decorator
107
+ let methodName = args[0];
108
+ let descriptor = args[1];
109
+ if (!validTargets.includes('method'))
110
+ throw new AnnotationTargetError(ctor, 'method', validTargets);
111
+ if (!allowMultiple) {
112
+ let existingAnnots = Annotations.getMethodAnnotations(target, methodName, true);
113
+ if (existingAnnots.find(x => x.$metadataName === ctor['$metadataName']))
114
+ throw new Error(`Annotation ${ctor.name} can only be applied to an element once.`);
115
+ }
116
+ let annotation = factory({
117
+ type: 'method',
118
+ target,
119
+ propertyKey: methodName,
120
+ propertyDescriptor: descriptor
121
+ }, ...decoratorArgs);
122
+ if (!annotation)
123
+ return;
124
+ annotation.applyToMethod(target, methodName);
125
+ }
126
+ }
127
+ else if (args.length >= 1) {
128
+ // Property decorator
129
+ let propertyKey = args[0];
130
+ if (!validTargets.includes('property'))
131
+ throw new AnnotationTargetError(ctor, 'property', validTargets);
132
+ if (!allowMultiple) {
133
+ let existingAnnots = Annotations.getPropertyAnnotations(target, propertyKey, true);
134
+ if (existingAnnots.find(x => x.$metadataName === ctor['$metadataName']))
135
+ throw new Error(`Annotation ${ctor.name} can only be applied to an element once.`);
136
+ }
137
+ let annotation = factory({
138
+ type: 'property',
139
+ target,
140
+ propertyKey
141
+ }, ...decoratorArgs);
142
+ if (!annotation)
143
+ return;
144
+ annotation.applyToProperty(target, propertyKey);
145
+ }
146
+ else if (args.length === 0) {
147
+ // Class decorator
148
+ if (!validTargets.includes('class'))
149
+ throw new AnnotationTargetError(ctor, 'class', validTargets);
150
+ if (!allowMultiple) {
151
+ let existingAnnots = Annotations.getClassAnnotations(target);
152
+ if (existingAnnots.find(x => x.$metadataName === ctor['$metadataName']))
153
+ throw new Error(`Annotation ${ctor.name} can only be applied to an element once.`);
154
+ }
155
+ let annotation = factory({
156
+ type: 'class',
157
+ target
158
+ }, ...decoratorArgs);
159
+ if (!annotation)
160
+ return;
161
+ annotation.applyToClass(target);
162
+ }
163
+ else {
164
+ // Invalid, or future decorator types we don't support yet.
165
+ throw new Error(`Encountered unknown decorator invocation with ${args.length + 1} parameters.`);
166
+ }
167
+ };
168
+ };
169
+ }
170
+ export function MetadataName(name) {
171
+ return target => Object.defineProperty(target, '$metadataName', { value: name });
172
+ }
173
+ /**
174
+ * Represents a metadata annotation which can be applied to classes,
175
+ * constructor parameters, methods, properties, or method parameters
176
+ * via decorators.
177
+ *
178
+ * Custom annotations are defined as subclasses of this class.
179
+ * By convention, all custom annotation classes should have a name
180
+ * which ends in "Annotation" such as "NameAnnotation".
181
+ *
182
+ * To create a new annotation create a subclass of `Annotation`
183
+ * with a constructor that takes the parameters you are interested in
184
+ * storing, and save the appropriate information onto fields of the
185
+ * new instance. For your convenience, Annotation provides a default
186
+ * constructor which takes a map object and applies its properties onto
187
+ * the current instance, but you may replace it with a constructor that
188
+ * takes any arguments you wish.
189
+ *
190
+ * You may wish to add type safety to the default constructor parameter.
191
+ * To do so, override the constructor and define it:
192
+ *
193
+ ```
194
+ class XYZ extends Annotation {
195
+ constructor(
196
+ options : MyOptions
197
+ ) {
198
+ super(options);
199
+ }
200
+ }
201
+ ```
202
+ *
203
+ * Annotations are applied by using decorators.
204
+ * When you define a custom annotation, you must also define a
205
+ * custom decorator:
206
+ *
207
+ ```
208
+ const Name =
209
+ NameAnnotation.decorator();
210
+ ```
211
+ * You can then use that decorator:
212
+ ```
213
+ @Name()
214
+ class ABC {
215
+ // ...
216
+ }
217
+ ```
218
+ *
219
+ */
220
+ export class Annotation {
221
+ constructor(props) {
222
+ this.$metadataName = this.constructor['$metadataName'];
223
+ if (!this.$metadataName || !this.$metadataName.includes(':')) {
224
+ throw new Error(`You must specify a metadata name for this annotation in the form of `
225
+ + ` 'mynamespace:myproperty'. You specified: '${this.$metadataName || '<none>'}'`);
226
+ }
227
+ Object.assign(this, props || {});
228
+ }
229
+ toString() {
230
+ return `@${this.constructor.name}`;
231
+ }
232
+ static getMetadataName() {
233
+ if (!this['$metadataName'])
234
+ throw new Error(`Annotation subclass ${this.name} must have @MetadataName()`);
235
+ return this['$metadataName'];
236
+ }
237
+ static decorator(options) {
238
+ if (this === Annotation) {
239
+ if (!options || !options.factory) {
240
+ throw new Error(`When calling Annotation.decorator() to create a mutator, you must specify a factory (or use Mutator.decorator())`);
241
+ }
242
+ }
243
+ return makeDecorator(this, options);
244
+ }
245
+ /**
246
+ * Clone this annotation instance into a new one. This is not a deep copy.
247
+ */
248
+ clone() {
249
+ return Annotations.clone(this);
250
+ }
251
+ /**
252
+ * Apply this annotation to a given target.
253
+ * @param target
254
+ */
255
+ applyToClass(target) {
256
+ return Annotations.applyToClass(this, target);
257
+ }
258
+ /**
259
+ * Apply this annotation instance to the given property.
260
+ * @param target
261
+ * @param name
262
+ */
263
+ applyToProperty(target, name) {
264
+ return Annotations.applyToProperty(this, target, name);
265
+ }
266
+ /**
267
+ * Apply this annotation instance to the given method.
268
+ * @param target
269
+ * @param name
270
+ */
271
+ applyToMethod(target, name) {
272
+ return Annotations.applyToMethod(this, target, name);
273
+ }
274
+ /**
275
+ * Apply this annotation instance to the given method parameter.
276
+ * @param target
277
+ * @param name
278
+ * @param index
279
+ */
280
+ applyToParameter(target, name, index) {
281
+ return Annotations.applyToParameter(this, target, name, index);
282
+ }
283
+ /**
284
+ * Apply this annotation instance to the given constructor parameter.
285
+ * @param target
286
+ * @param name
287
+ * @param index
288
+ */
289
+ applyToConstructorParameter(target, index) {
290
+ return Annotations.applyToConstructorParameter(this, target, index);
291
+ }
292
+ /**
293
+ * Filter the given list of annotations for the ones which match this annotation class
294
+ * based on matching $metadataName.
295
+ *
296
+ * @param this
297
+ * @param annotations
298
+ */
299
+ static filter(annotations) {
300
+ return annotations.filter(x => x.$metadataName === this.getMetadataName());
301
+ }
302
+ /**
303
+ * Get all instances of this annotation class attached to the given class.
304
+ * If called on a subclass of Annotation, it returns only annotations that match
305
+ * that subclass.
306
+ * @param this
307
+ * @param type The class to check
308
+ */
309
+ static getAllForClass(type) {
310
+ return Annotations.getClassAnnotations(type)
311
+ .filter(x => x.$metadataName === this.getMetadataName());
312
+ }
313
+ /**
314
+ * Get a single instance of this annotation class attached to the given class.
315
+ * If called on a subclass of Annotation, it returns only annotations that match
316
+ * that subclass.
317
+ *
318
+ * @param this
319
+ * @param type
320
+ */
321
+ static getForClass(type) {
322
+ return this.getAllForClass(type)[0];
323
+ }
324
+ /**
325
+ * Get all instances of this annotation class attached to the given method.
326
+ * If called on a subclass of Annotation, it returns only annotations that match
327
+ * that subclass.
328
+ *
329
+ * @param this
330
+ * @param type The class where the method is defined
331
+ * @param methodName The name of the method to check
332
+ */
333
+ static getAllForMethod(type, methodName) {
334
+ return Annotations.getMethodAnnotations(type, methodName)
335
+ .filter(x => x.$metadataName === this.getMetadataName());
336
+ }
337
+ /**
338
+ * Get one instance of this annotation class attached to the given method.
339
+ * If called on a subclass of Annotation, it returns only annotations that match
340
+ * that subclass.
341
+ *
342
+ * @param this
343
+ * @param type The class where the method is defined
344
+ * @param methodName The name of the method to check
345
+ */
346
+ static getForMethod(type, methodName) {
347
+ return this.getAllForMethod(type, methodName)[0];
348
+ }
349
+ /**
350
+ * Get all instances of this annotation class attached to the given property.
351
+ * If called on a subclass of Annotation, it returns only annotations that match
352
+ * that subclass.
353
+ *
354
+ * @param this
355
+ * @param type The class where the property is defined
356
+ * @param propertyName The name of the property to check
357
+ */
358
+ static getAllForProperty(type, propertyName) {
359
+ return Annotations.getPropertyAnnotations(type, propertyName)
360
+ .filter(x => x.$metadataName === this.getMetadataName());
361
+ }
362
+ /**
363
+ * Get one instance of this annotation class attached to the given property.
364
+ * If called on a subclass of Annotation, it returns only annotations that match
365
+ * that subclass.
366
+ *
367
+ * @param this
368
+ * @param type The class where the property is defined
369
+ * @param propertyName The name of the property to check
370
+ */
371
+ static getForProperty(type, propertyName) {
372
+ return this.getAllForProperty(type, propertyName)[0];
373
+ }
374
+ /**
375
+ * Get all instances of this annotation class attached to the parameters of the given method.
376
+ * If called on a subclass of Annotation, it returns only annotations that match
377
+ * that subclass.
378
+ *
379
+ * @param this
380
+ * @param type The class where the method is defined
381
+ * @param methodName The name of the method where parameter annotations should be checked for
382
+ */
383
+ static getAllForParameters(type, methodName) {
384
+ return Annotations.getParameterAnnotations(type, methodName)
385
+ .map(set => (set || []).filter(x => this === Annotation ? true : (x.$metadataName === this.getMetadataName())));
386
+ }
387
+ /**
388
+ * Get all instances of this annotation class attached to the parameters of the constructor
389
+ * for the given class.
390
+ * If called on a subclass of Annotation, it returns only annotations that match
391
+ * that subclass.
392
+ *
393
+ * @param this
394
+ * @param type The class where constructor parameter annotations should be checked for
395
+ */
396
+ static getAllForConstructorParameters(type) {
397
+ let finalSet = new Array(type.length).fill(undefined);
398
+ let annotations = Annotations.getConstructorParameterAnnotations(type)
399
+ .map(set => (set || []).filter(x => this === Annotation ? true : (x.$metadataName === this.getMetadataName())));
400
+ for (let i = 0, max = annotations.length; i < max; ++i)
401
+ finalSet[i] = annotations[i];
402
+ return finalSet;
403
+ }
404
+ }
405
+ /**
406
+ * A helper class for managing annotations
407
+ */
408
+ export class Annotations {
409
+ /**
410
+ * Copy the annotations defined for one class onto another.
411
+ * @param from The class to copy annotations from
412
+ * @param to The class to copy annotations to
413
+ */
414
+ static copyClassAnnotations(from, to) {
415
+ let annotations = Annotations.getClassAnnotations(from);
416
+ annotations.forEach(x => Annotations.applyToClass(x, to));
417
+ }
418
+ /**
419
+ * Apply this annotation to a given target.
420
+ * @param target
421
+ */
422
+ static applyToClass(annotation, target) {
423
+ let list = this.getOrCreateListForClass(target);
424
+ let clone = this.clone(annotation);
425
+ list.push(clone);
426
+ if (Reflect.getOwnMetadata) {
427
+ let reflectedAnnotations = Reflect.getOwnMetadata('annotations', target) || [];
428
+ reflectedAnnotations.push({ toString() { return `${clone.$metadataName}`; }, annotation: clone });
429
+ Reflect.defineMetadata('annotations', reflectedAnnotations, target);
430
+ }
431
+ return clone;
432
+ }
433
+ /**
434
+ * Apply this annotation instance to the given property.
435
+ * @param target
436
+ * @param name
437
+ */
438
+ static applyToProperty(annotation, target, name) {
439
+ let list = this.getOrCreateListForProperty(target, name);
440
+ let clone = this.clone(annotation);
441
+ list.push(clone);
442
+ if (Reflect.getOwnMetadata) {
443
+ let reflectedAnnotations = Reflect.getOwnMetadata('propMetadata', target, name) || [];
444
+ reflectedAnnotations.push({ toString() { return `${clone.$metadataName}`; }, annotation: clone });
445
+ Reflect.defineMetadata('propMetadata', reflectedAnnotations, target, name);
446
+ }
447
+ return clone;
448
+ }
449
+ /**
450
+ * Apply this annotation instance to the given method.
451
+ * @param target
452
+ * @param name
453
+ */
454
+ static applyToMethod(annotation, target, name) {
455
+ let list = this.getOrCreateListForMethod(target, name);
456
+ let clone = Annotations.clone(annotation);
457
+ list.push(clone);
458
+ if (Reflect.getOwnMetadata && target.constructor) {
459
+ const meta = Reflect.getOwnMetadata('propMetadata', target.constructor) || {};
460
+ meta[name] = (meta.hasOwnProperty(name) && meta[name]) || [];
461
+ meta[name].unshift({ toString() { return `${clone.$metadataName}`; }, annotation: clone });
462
+ Reflect.defineMetadata('propMetadata', meta, target.constructor);
463
+ }
464
+ return clone;
465
+ }
466
+ /**
467
+ * Apply this annotation instance to the given method parameter.
468
+ * @param target
469
+ * @param name
470
+ * @param index
471
+ */
472
+ static applyToParameter(annotation, target, name, index) {
473
+ let list = this.getOrCreateListForMethodParameters(target, name);
474
+ while (list.length < index)
475
+ list.push(null);
476
+ let paramList = list[index] || [];
477
+ let clone = this.clone(annotation);
478
+ paramList.push(clone);
479
+ list[index] = paramList;
480
+ return clone;
481
+ }
482
+ /**
483
+ * Apply this annotation instance to the given constructor parameter.
484
+ * @param target
485
+ * @param name
486
+ * @param index
487
+ */
488
+ static applyToConstructorParameter(annotation, target, index) {
489
+ let list = this.getOrCreateListForConstructorParameters(target);
490
+ while (list.length < index)
491
+ list.push(null);
492
+ let paramList = list[index] || [];
493
+ let clone = this.clone(annotation);
494
+ paramList.push(clone);
495
+ list[index] = paramList;
496
+ if (Reflect.getOwnMetadata) {
497
+ let parameterList = Reflect.getOwnMetadata('parameters', target) || [];
498
+ while (parameterList.length < index)
499
+ parameterList.push(null);
500
+ let parameterAnnotes = parameterList[index] || [];
501
+ parameterAnnotes.push(clone);
502
+ parameterList[index] = parameterAnnotes;
503
+ Reflect.defineMetadata('parameters', parameterList, target);
504
+ }
505
+ return clone;
506
+ }
507
+ /**
508
+ * Clone the given Annotation instance into a new instance. This is not
509
+ * a deep copy.
510
+ *
511
+ * @param annotation
512
+ */
513
+ static clone(annotation) {
514
+ if (!annotation)
515
+ return annotation;
516
+ return Object.assign(Object.create(Object.getPrototypeOf(annotation)), annotation);
517
+ }
518
+ /**
519
+ * Get all annotations (including from Angular and other compatible
520
+ * frameworks).
521
+ *
522
+ * @param target The target to fetch annotations for
523
+ */
524
+ static getClassAnnotations(target) {
525
+ return (this.getListForClass(target) || [])
526
+ .map(x => this.clone(x));
527
+ }
528
+ /**
529
+ * Get all annotations (including from Angular and other compatible
530
+ * frameworks).
531
+ *
532
+ * @param target The target to fetch annotations for
533
+ */
534
+ static getMethodAnnotations(target, methodName, isStatic = false) {
535
+ return (this.getListForMethod(isStatic ? target : target.prototype, methodName) || [])
536
+ .map(x => this.clone(x));
537
+ }
538
+ /**
539
+ * Get all annotations (including from Angular and other compatible
540
+ * frameworks).
541
+ *
542
+ * @param target The target to fetch annotations for
543
+ */
544
+ static getPropertyAnnotations(target, methodName, isStatic = false) {
545
+ return (this.getListForProperty(isStatic ? target : target.prototype, methodName) || [])
546
+ .map(x => this.clone(x));
547
+ }
548
+ /**
549
+ * Get the annotations defined on the parameters of the given method of the given
550
+ * class.
551
+ *
552
+ * @param type
553
+ * @param methodName
554
+ * @param isStatic Whether `type` itself (isStatic = true), or `type.prototype` (isStatic = false) should be the target.
555
+ * Note that passing true may indicate that the passed `type` is already the prototype of a class.
556
+ */
557
+ static getParameterAnnotations(type, methodName, isStatic = false) {
558
+ return (this.getListForMethodParameters(isStatic ? type : type.prototype, methodName) || [])
559
+ .map(set => set ? set.map(anno => this.clone(anno)) : []);
560
+ }
561
+ /**
562
+ * Get the annotations defined on the parameters of the given method of the given
563
+ * class.
564
+ *
565
+ * @param type
566
+ * @param methodName
567
+ */
568
+ static getConstructorParameterAnnotations(type) {
569
+ return (this.getListForConstructorParameters(type) || [])
570
+ .map(set => set ? set.map(anno => this.clone(anno)) : []);
571
+ }
572
+ /**
573
+ * Get a list of annotations for the given class.
574
+ * @param target
575
+ */
576
+ static getListForClass(target) {
577
+ if (!target)
578
+ return [];
579
+ let combinedSet = [];
580
+ let superclass = Object.getPrototypeOf(target);
581
+ if (superclass && superclass !== Function)
582
+ combinedSet = combinedSet.concat(this.getListForClass(superclass));
583
+ if (target.hasOwnProperty(ANNOTATIONS_KEY))
584
+ combinedSet = combinedSet.concat(target[ANNOTATIONS_KEY] || []);
585
+ return combinedSet;
586
+ }
587
+ /**
588
+ * Get a list of own annotations for the given class, or create that list.
589
+ * @param target
590
+ */
591
+ static getOrCreateListForClass(target) {
592
+ if (!target.hasOwnProperty(ANNOTATIONS_KEY))
593
+ Object.defineProperty(target, ANNOTATIONS_KEY, { enumerable: false, value: [] });
594
+ return target[ANNOTATIONS_KEY];
595
+ }
596
+ /**
597
+ * Gets a map of the annotations defined on all properties of the given class/function. To get the annotations of instance fields,
598
+ * make sure to use `Class.prototype`, otherwise static annotations are returned.
599
+ */
600
+ static getMapForClassProperties(target, mapToPopulate) {
601
+ let combinedSet = mapToPopulate || {};
602
+ if (!target || target === Function)
603
+ return combinedSet;
604
+ this.getMapForClassProperties(Object.getPrototypeOf(target), combinedSet);
605
+ if (target.hasOwnProperty(PROPERTY_ANNOTATIONS_KEY)) {
606
+ let ownMap = target[PROPERTY_ANNOTATIONS_KEY] || {};
607
+ for (let key of Object.keys(ownMap))
608
+ combinedSet[key] = (combinedSet[key] || []).concat(ownMap[key]);
609
+ }
610
+ return combinedSet;
611
+ }
612
+ static getOrCreateMapForClassProperties(target) {
613
+ if (!target.hasOwnProperty(PROPERTY_ANNOTATIONS_KEY))
614
+ Object.defineProperty(target, PROPERTY_ANNOTATIONS_KEY, { enumerable: false, value: [] });
615
+ return target[PROPERTY_ANNOTATIONS_KEY];
616
+ }
617
+ static getListForProperty(target, propertyKey) {
618
+ let map = this.getMapForClassProperties(target);
619
+ if (!map)
620
+ return null;
621
+ return map[propertyKey];
622
+ }
623
+ static getOrCreateListForProperty(target, propertyKey) {
624
+ let map = this.getOrCreateMapForClassProperties(target);
625
+ if (!map[propertyKey])
626
+ map[propertyKey] = [];
627
+ return map[propertyKey];
628
+ }
629
+ static getOrCreateListForMethod(target, methodName) {
630
+ return this.getOrCreateListForProperty(target, methodName);
631
+ }
632
+ static getListForMethod(target, methodName) {
633
+ return this.getListForProperty(target, methodName);
634
+ }
635
+ /**
636
+ * Get a map of the annotations defined on all parameters of all methods of the given class/function.
637
+ * To get instance methods, make sure to pass `Class.prototype`, otherwise the results are for static fields.
638
+ */
639
+ static getMapForMethodParameters(target, mapToPopulate) {
640
+ let combinedMap = mapToPopulate || {};
641
+ if (!target || target === Function)
642
+ return combinedMap;
643
+ // superclass/prototype
644
+ this.getMapForMethodParameters(Object.getPrototypeOf(target), combinedMap);
645
+ if (target.hasOwnProperty(METHOD_PARAMETER_ANNOTATIONS_KEY)) {
646
+ let ownMap = target[METHOD_PARAMETER_ANNOTATIONS_KEY] || {};
647
+ for (let methodName of Object.keys(ownMap)) {
648
+ let parameters = ownMap[methodName];
649
+ let combinedMethodMap = combinedMap[methodName] || [];
650
+ for (let i = 0, max = parameters.length; i < max; ++i) {
651
+ combinedMethodMap[i] = (combinedMethodMap[i] || []).concat(parameters[i] || []);
652
+ }
653
+ combinedMap[methodName] = combinedMethodMap;
654
+ }
655
+ }
656
+ return combinedMap;
657
+ }
658
+ static getOrCreateMapForMethodParameters(target) {
659
+ if (!target.hasOwnProperty(METHOD_PARAMETER_ANNOTATIONS_KEY))
660
+ Object.defineProperty(target, METHOD_PARAMETER_ANNOTATIONS_KEY, { enumerable: false, value: {} });
661
+ return target[METHOD_PARAMETER_ANNOTATIONS_KEY];
662
+ }
663
+ static getListForMethodParameters(target, methodName) {
664
+ let map = this.getMapForMethodParameters(target);
665
+ if (!map)
666
+ return null;
667
+ return map[methodName];
668
+ }
669
+ static getOrCreateListForMethodParameters(target, methodName) {
670
+ let map = this.getOrCreateMapForMethodParameters(target);
671
+ if (!map[methodName])
672
+ map[methodName] = [];
673
+ return map[methodName];
674
+ }
675
+ static getOrCreateListForConstructorParameters(target) {
676
+ if (!target[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY])
677
+ Object.defineProperty(target, CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY, { enumerable: false, value: [] });
678
+ return target[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY];
679
+ }
680
+ static getListForConstructorParameters(target) {
681
+ return target[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY];
682
+ }
683
+ }
684
+ /**
685
+ * An annotation for attaching a label to a programmatic element.
686
+ * Can be queried with LabelAnnotation.getForClass() for example.
687
+ */
688
+ let LabelAnnotation = class LabelAnnotation extends Annotation {
689
+ constructor(text) {
690
+ super();
691
+ this.text = text;
692
+ }
693
+ };
694
+ LabelAnnotation = __decorate([
695
+ MetadataName('alterior:Label'),
696
+ __metadata("design:paramtypes", [String])
697
+ ], LabelAnnotation);
698
+ export { LabelAnnotation };
699
+ export const Label = LabelAnnotation.decorator();
700
700
  //# sourceMappingURL=annotations.js.map