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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 73x 1093x 1093x 1093x 1088x 5x 1093x 5x 1088x 18x 1093x 1093x 1093x 1075x 1093x 1706x 1706x 1706x 1706x 1706x 1712x 3414x 3414x 3414x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3414x 1706x 1706x 1706x 1706x 1093x 1093x 1093x 1092x 1706x 1092x 1092x 1092x 1092x 1092x 10x 1706x 1290x 416x | import { AnnotationRef } from '../annotation-ref';
import {
Annotation,
AnnotationKind,
AnyDecorator,
Decorator,
} from '../annotation.types';
import { DecoratorHookRegistry } from './decorator-hook.registry';
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-empty-function */
import {
_copyPropsAndMeta,
assert,
getPrototype,
} from '@aspectjs/common/utils';
import { ReflectContext } from '../../reflect/reflect.context';
import { reflectContext } from '../../reflect/reflect.context.global';
import { ReflectError } from '../../reflect/reflect.error';
import { AnnotationContext } from '../annotation-context';
import type { AnnotationStub } from '../annotation.types';
import { AnnotationRegistry } from '../registry/annotation.registry';
import { AnnotationTargetFactory } from '../target/annotation-target.factory';
let anonymousAnnotationId = 0;
/**
* Options given to decoree.annotations {@link AnnotationFactory} to create a new annotation.
* @typeParam T the type of annotation to create
* @typeParam S the signature of the annotation to create. It defines the name of the annotation and the set of accepted parameters.
*/
export interface AnnotationCreateOptions<T extends AnnotationKind> {
/**
* The name of the annotation to create.
*/
name: string;
/**
* The type of annotation to create.
*/
kind: T;
}
/**
* Factory to create an {@link Annotation}.
*/
export class AnnotationFactory {
/**
* Create a new AnnotationFactory with the given `groupId`.
* You generaly have to choose a `groupId` that identifies your project or is unique to your organisation.
* The `groupId` will be used as a part of the signature for the created annotations.
* @param groupId The groupId of this factory.
*/
constructor(
/**
* The group of this factory.
* All annotations created by this factory will belong to this group.
*/
public readonly groupId: string,
) {}
/**
* Create with the a n annotation.
* @param options The options for the annotation to create.
* @param annotationStub The signature of the annotation to create.
* @typeParam T the type of annotation to create
* @typeParam S the signature of the annotation to create. It defines the name of the annotation and the set of accepted parameters.
*/
create<T extends AnnotationKind, S extends AnnotationStub<T>>(
options?: AnnotationCreateOptions<T>,
annotationStub?: S,
): Annotation<T, S>;
/**
* Create a new annotation with the given `name` and `type`.
* If no annotation type is given, the annotation could be used above classes, methods, properties, attributes and parameters.
*
* @param name The name of the annotation to create.
* @typeParam S the signature of the annotation to create. It defines the name of the annotation and the set of accepted parameters.
* @example
* ```ts
* const LogErrors = new AnnotationFactory('demo').create('LogErrors');
* ```
*/
create<S extends AnnotationStub>(name: string): Annotation<any, S>;
/**
* Create a new annotation wwith the given type and signature. The created annotation accepts the same parameters as with the the provid function.
* If no annotation type is given, the annotation could be used above classes, methods, properties, attributes and parameters.
*
* @param type The type of annotation to create.
* @param name The name of the annotation to create.
* @param annotationStub The signature of the annotation to create.
* @typeParam T the type of annotation to create
* @typeParam S the signature of the annotation to create. It defines the name of the annotation and the set of accepted parameters.
* @example
* ```ts
* const LogErrors = new AnnotationFactory('demo').create(
* AnnotationKind.METHOD,
* 'Log',
* function Log(
* level: 'info' | 'warn' | 'error' | 'debug' = 'error',
* ) {});
* ```
*/
create<T extends AnnotationKind, S extends AnnotationStub<T>>(
type: T,
name?: string,
annotationStub?: S,
): Annotation<T, S>;
/**
* Create a new annotation with the given signature.
* The created annotation has the same name as the given function, and accepts the same parameters.
* If no annotation type is given, the annotation could be used above classes, methods, properties, attributes and parameters.
*
* @param name The name of the annotation to create.
* @param annotationStub The signature of the annotation to create.
* @typeParam S the signature of the annotation to create. It defines the name of the annotation and the set of accepted parameters.
* @example
* ```ts
* const LogErrors = new AnnotationFactory('demo').create(
* 'Log',
* function Log(
* level: 'info' | 'warn' | 'error' | 'debug' = 'error',
* ) {});
* ```
*/
create<S extends AnnotationStub>(
name?: string,
annotationStub?: S,
): Annotation<any, S>;
create<T extends AnnotationKind, S extends AnnotationStub<T>>(
init?: string | AnnotationCreateOptions<T> | AnnotationKind,
nameOrAnnotationStub?: S | string,
annotationStub?: S,
): Annotation<T, S> {
const _opts: Partial<AnnotationCreateOptions<T>> =
typeof init === 'object' ? init : {};
Iif (typeof init === 'object') {
annotationStub = nameOrAnnotationStub as S;
} else if (typeof init === 'string') {
_opts.name = init;
} else Iif (typeof init === typeof AnnotationKind) {
_opts.kind = init as T;
}
if (typeof nameOrAnnotationStub === 'string') {
_opts.name = nameOrAnnotationStub;
} else if (typeof nameOrAnnotationStub === 'function') {
annotationStub = nameOrAnnotationStub;
}
const groupId = this.groupId;
Iif (!_opts.name) {
_opts.name = `anonymousAnnotation#${anonymousAnnotationId++}`;
}
if (!annotationStub) {
annotationStub = function () {} as S;
}
// create the annotation (ie: decorator factory)
return this._createAnnotation(groupId, _opts.name, annotationStub);
}
// Turn an annotation into an ES decorator
private _createDecorator<
T extends AnnotationKind,
S extends AnnotationStub<T>,
>(
annotation: Annotation<T, S>,
annotationStub: S,
annotationArgs: any[],
): Decorator {
/* eslint-disable-next-line @typescript-eslint/no-this-alias */
const _this = this;
return function (
this: any,
...targetArgs: any[]
): Function | PropertyDescriptor | void {
const reflect = reflectContext();
const annotationContext = _this._createAnnotationContext<T, S>(
reflect,
annotation as any,
annotationArgs,
targetArgs,
);
return [...reflect.get(DecoratorHookRegistry).values()]
.sort(
(c1, c2) =>
(c1.order ?? Number.MAX_SAFE_INTEGER) -
(c2.order ?? Number.MAX_SAFE_INTEGER - 1),
)
.reduce(
(decoree, { name, createDecorator: decorator }) => {
try {
const newDecoree = (decorator as any)
.apply(this, [reflect, annotationContext, annotationStub])
?.apply(this, targetArgs);
if (newDecoree) {
if (decoree) {
const type = annotationContext.target.kind;
if (type === AnnotationKind.CLASS) {
const proto = getPrototype(
annotationContext.target.proto.constructor,
);
proto.constructor = newDecoree;
newDecoree.prototype = proto;
targetArgs[0] = newDecoree;
assert(
typeof newDecoree === 'function' &&
typeof decoree === 'function',
);
_copyPropsAndMeta(newDecoree, decoree); // copy static props
} else IEif (newDecoree.value) {
_copyPropsAndMeta(newDecoree.value, decoree.value); // copy static props
}
}
decoree = newDecoree;
}
return decoree;
} catch (e) {
Iif (!(e instanceof ReflectError)) {
console.error(
`Error applying annotation hook ${name}: ${
(e as Error).message
}`,
);
}
throw e;
}
},
noopDecorator.apply(this, targetArgs as any) as any,
);
};
}
private _createAnnotationContext<
T extends AnnotationKind,
S extends AnnotationStub<T>,
>(
reflect: ReflectContext,
annotation: Annotation<T, S>,
annotationArgs: unknown[],
targetArgs: any[],
) {
const targetFactory = reflect.get(AnnotationTargetFactory);
/* eslint-disable-next-line prefer-spread */
const target = targetFactory.of.apply(targetFactory, targetArgs as any);
assert(!!target);
return new AnnotationContext(annotation, annotationArgs, target);
}
private _createAnnotation<
T extends AnnotationKind,
S extends AnnotationStub<T>,
>(groupId: string, name: string, stub: S): Annotation<T, S> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const _factory = this;
const annotationRef = AnnotationRef.of(groupId, name);
reflectContext().get(AnnotationRegistry).register(annotationRef);
const annotation = function (...annotationArgs: any[]): Decorator {
return _factory._createDecorator(annotation, stub, annotationArgs);
} as any as Annotation<T, S>;
// copy static properties
Object.defineProperties(annotation, Object.getOwnPropertyDescriptors(stub));
Object.defineProperties(
annotation,
Object.getOwnPropertyDescriptors(annotationRef),
);
Object.defineProperty(annotation, 'ref', {
value: annotationRef,
});
annotation.toString = () => `@${annotation.name}`;
return annotation;
}
}
const noopDecorator: AnyDecorator = (<TFunction extends Function>(
target: TFunction,
propertyKey: string | symbol,
_parameterIndex: number,
): any => {
if (propertyKey !== undefined) {
return Object.getOwnPropertyDescriptor(target, propertyKey);
}
return target;
}) as any;
|