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