All files / common/src/annotation/target annotation-target.ts

66.66% Statements 2/3
100% Branches 0/0
50% Functions 1/2
66.66% Lines 2/3

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                      10x 816x                                                                                                                                                                                                                  
import { MethodPropertyDescriptor, Prototype } from '@aspectjs/common/utils';
import type {
  Annotation,
  AnnotationKind,
  AnnotationStub,
} from '../annotation.types';
import { AnnotationsSelector } from '../context/registry/selector';
 
/**
 * @internal
 */
export class AnnotationTargetRef {
  constructor(public readonly value: string) {}
  toString() {
    return this.value;
  }
}
 
/**
 * @internal
 */
export interface BaseAnnotationTarget<
  T extends AnnotationKind = AnnotationKind,
  X = unknown,
> {
  readonly kind: T;
  readonly proto: Prototype<X>;
  readonly name: string;
  readonly label: string;
  readonly ref: AnnotationTargetRef;
  readonly declaringClass: ClassAnnotationTarget<X>;
  readonly parentClass: ClassAnnotationTarget<unknown> | undefined;
  readonly static: boolean;
  asDecoratorArgs(): any[];
 
  eval(): unknown;
 
  defineMetadata(key: string, value: any): void;
  getMetadata<T>(key: string, defaultvalue: () => T): T;
  getMetadata<T>(key: string, defaultvalue?: () => T): T | undefined;
 
  annotations<
    T extends AnnotationKind,
    S extends AnnotationStub = AnnotationStub,
  >(
    annotation: S,
  ): AnnotationsSelector<T, S>;
  annotations(
    ...annotation: Annotation[]
  ): AnnotationsSelector<AnnotationKind, AnnotationStub>;
}
 
/**
 * Target for an annotated class.
 * @param X the type of the class
 */
export interface ClassAnnotationTarget<X = unknown>
  extends BaseAnnotationTarget<AnnotationKind.CLASS, X> {
  readonly parentClass: ClassAnnotationTarget<unknown> | undefined;
  readonly static: boolean;
}
 
/**
 * Target for an annotated property.
 * @param X the type of the property's class
 */
export interface PropertyAnnotationTarget<X = unknown>
  extends BaseAnnotationTarget<AnnotationKind.PROPERTY, X> {
  readonly propertyKey: string | symbol;
  readonly declaringClass: ClassAnnotationTarget<X>;
  readonly descriptor?: TypedPropertyDescriptor<unknown>;
  readonly static: boolean;
}
 
/**
 * Target for an annotated method.
 * @param X the type of the method's class
 */
export interface MethodAnnotationTarget<X = unknown>
  extends BaseAnnotationTarget<AnnotationKind.METHOD, X> {
  readonly propertyKey: string | symbol;
  readonly descriptor: MethodPropertyDescriptor;
  readonly declaringClass: ClassAnnotationTarget<X>;
  readonly static: boolean;
}
 
/**
 * Target for an annotated parameter.
 * @param X the type of the parameter's class
 */
export interface ParameterAnnotationTarget<X = unknown>
  extends BaseAnnotationTarget<AnnotationKind.PARAMETER, X> {
  readonly propertyKey: string | symbol;
  readonly parameterIndex: number;
  readonly descriptor: MethodPropertyDescriptor;
  readonly declaringMethod: MethodAnnotationTarget<X>;
  readonly declaringClass: ClassAnnotationTarget<X>;
  readonly static: boolean;
}
 
/**
 * Represents the symbol (Class, Method, Parameter, Property) annotated by an annotation.
 * @param T The kind of annotation
 * @param X The type of the class that target belongs to.
 */
export type AnnotationTarget<
  T extends AnnotationKind = AnnotationKind,
  X = unknown,
> = T extends AnnotationKind.CLASS
  ? ClassAnnotationTarget<X>
  : T extends AnnotationKind.PARAMETER
  ? ParameterAnnotationTarget<X>
  : T extends AnnotationKind.METHOD
  ? MethodAnnotationTarget<X>
  : T extends AnnotationKind.PROPERTY
  ? PropertyAnnotationTarget<X>
  : never;