Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 10x 10x 10x 10x 10x 10x 10x 10x 10x 1791x 1791x 1791x 1791x 1791x 1791x 18x 18x 1791x 12x 1791x 1791x 10x 1791x 1791x 470x 1321x 854x 419x 435x 435x 435x 12x 467x 1791x | import {
ConstructorType,
Prototype,
getPrototype,
isClassInstance,
isFunction,
isNumber,
isObject,
isUndefined,
} from '@aspectjs/common/utils';
import { BoundAnnotationTarget } from './bound-annotation-target';
import { AnnotationKind } from '../annotation.types';
import { AnnotationTarget } from './annotation-target';
import { _AnnotationTargetImpl } from './annotation-target.impl';
import { _findPropertyDescriptor } from './annotation-target.utils';
import { _ClassAnnotationTargetImpl } from './impl/class-annotation-target.impl';
import { _MethodAnnotationTargetImpl } from './impl/method-annotation-target.impl';
import { _ParameterAnnotationTargetImpl } from './impl/parameter-annotation-target.impl';
import { _PropertyAnnotationTargetImpl } from './impl/property-annotation-target.impl';
const _TARGET_GENERATORS: {
[t in AnnotationKind]: (...args: any[]) => AnnotationTarget<t>;
} = {
[AnnotationKind.CLASS]: _ClassAnnotationTargetImpl.of,
[AnnotationKind.PROPERTY]: _PropertyAnnotationTargetImpl.of,
[AnnotationKind.METHOD]: _MethodAnnotationTargetImpl.of,
[AnnotationKind.PARAMETER]: _ParameterAnnotationTargetImpl.of,
};
/**
* Create {@link AnnotationTarget}
*/
export class AnnotationTargetFactory {
of<X = unknown>(
target: ConstructorType<X>,
): AnnotationTarget<AnnotationKind.CLASS, X>;
of<X = unknown>(target: X): BoundAnnotationTarget<AnnotationKind.CLASS, X>;
of<X = unknown>(
target: ConstructorType<X>,
propertyKey: keyof X,
): AnnotationTarget<AnnotationKind.PROPERTY, X>;
of<X = unknown>(
target: X,
propertyKey: keyof X,
): BoundAnnotationTarget<AnnotationKind.PROPERTY, X>;
of<X = unknown>(
target: ConstructorType<X>,
propertyKey: keyof X,
descriptor: PropertyDescriptor,
): AnnotationTarget<AnnotationKind.METHOD, X>;
of<X = unknown>(
target: X,
propertyKey: keyof X,
descriptor: PropertyDescriptor,
): BoundAnnotationTarget<AnnotationKind.METHOD, X>;
of<X = unknown>(
target: ConstructorType<X>,
propertyKey: keyof X,
parameterIndex: number,
): AnnotationTarget<AnnotationKind.PARAMETER, X>;
of<T extends AnnotationKind = AnnotationKind, X = unknown>(
...args: unknown[]
): AnnotationTarget<T, X> {
// ClassAnnotation = <TFunction extends Function>(target: TFunction) => TFunction | void;
// PropertyAnnotation = (target: Object, propertyKey: string | symbol) => void;
// MethodAnnotation = <A>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<A>) => TypedPropertyDescriptor<A> | void;
// ParameterAnnotation = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;
const target = args[0] as ConstructorType<X> | Prototype<X>;
let classInstance: X | undefined;
const propertyKey: string | symbol | undefined = isUndefined(args[1])
? undefined
: (args[1] as string | symbol);
const parameterIndex: number | undefined = isNumber(args[2])
? args[2]
: undefined;
const descriptor: PropertyDescriptor | undefined = isObject(args[2])
? args[2]
: undefined;
const type = inferTypeFromArgs(
target,
propertyKey,
parameterIndex,
descriptor,
) as T;
if (isClassInstance(target)) {
// target is a class instance. Decoree should be the class ctor for type=CLASS, or the class proto for type=METHOD/PROPERTY/PARAMETER
args[0] =
type === AnnotationKind.CLASS
? getPrototype(target).constructor
: getPrototype(target);
classInstance = target as X;
}
if (!args[2] && type === AnnotationKind.METHOD) {
// Get the property descriptor if missing from args
args[2] = _findPropertyDescriptor(args[0] as Prototype<X>, propertyKey!);
}
const annotationTarget = _TARGET_GENERATORS[type](...args);
return classInstance
? (annotationTarget as unknown as _AnnotationTargetImpl<T, X>)._bind(
classInstance as X,
)
: (annotationTarget as AnnotationTarget<T, X>);
}
}
export function inferTypeFromArgs(...args: unknown[]): AnnotationKind;
export function inferTypeFromArgs<X = unknown>(
decoree: ConstructorType<X> | Prototype<X>,
propertyKey?: string | symbol,
parameterIndex?: number,
descriptor?: PropertyDescriptor,
): AnnotationKind;
export function inferTypeFromArgs<X = unknown>(...args: any[]): AnnotationKind {
const [decoree, propertyKey, parameterIndex, descriptor]: [
ConstructorType<X> | Prototype<X>,
string | symbol,
number,
PropertyDescriptor,
] = args as any;
let kind: AnnotationKind;
if (isNumber(parameterIndex)) {
kind = AnnotationKind.PARAMETER;
} else if (!isUndefined(propertyKey)) {
if (isObject(descriptor) && isFunction(descriptor.value)) {
kind = AnnotationKind.METHOD;
} else {
kind = AnnotationKind.PROPERTY;
// if called of(x, "method"), while method is a method, replace propertyKey with descriptor
const descriptor = _findPropertyDescriptor(decoree, propertyKey!);
if (typeof descriptor?.value === 'function') {
kind = AnnotationKind.METHOD;
}
}
} else {
kind = AnnotationKind.CLASS;
}
return kind;
}
|