All files / common/src/annotation/context/registry by-target-selector.ts

80.95% Statements 17/21
60% Branches 3/5
80% Functions 8/10
80.95% Lines 17/21

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 14810x               10x         10x         10x       61x 61x 61x                   18x                                       4x             11x                                   13x             10x               2x                                   58x                 58x 2x     56x                                            
import {
  ConstructorType,
  assert,
  getPrototype,
  isClassInstance,
  isObject,
} from '@aspectjs/common/utils';
import { AnnotationRef } from '../../annotation-ref';
import { AnnotationKind, AnnotationStub } from '../../annotation.types';
import { AnnotationTarget } from '../../target/annotation-target';
import { AnnotationTargetFactory } from '../../target/annotation-target.factory';
import { _AnnotationContextSet } from './annotation-context-set';
import { AnnotationSelectionFilter } from './selection-filter';
import { AnnotationsSelector, BoundAnnotationsSelector } from './selector';
 
/**
 * Selects annotations based on their type or target.
 */
export class AnnotationByTargetSelector<
  S extends AnnotationStub = AnnotationStub,
> {
  constructor(
    private readonly targetFactory: AnnotationTargetFactory,
    private readonly annotationSet: _AnnotationContextSet,
    private readonly annotationsRefs: Set<AnnotationRef> | undefined,
  ) {}
 
  all<X = unknown>(
    type?: ConstructorType<X>,
  ): AnnotationsSelector<AnnotationKind, S, X>;
  all<X = unknown>(type?: X): BoundAnnotationsSelector<AnnotationKind, S, X>;
  all<X = unknown>(
    type?: X | ConstructorType<X>,
  ): AnnotationsSelector<AnnotationKind, S, X> {
    return this.createSelector(
      [
        AnnotationKind.CLASS,
        AnnotationKind.METHOD,
        AnnotationKind.PROPERTY,
        AnnotationKind.PARAMETER,
      ],
      type,
    );
  }
 
  onClass<X = unknown>(
    type?: ConstructorType<X>,
  ): AnnotationsSelector<AnnotationKind.CLASS, S, X>;
  onClass<X = unknown>(
    type?: X,
  ): BoundAnnotationsSelector<AnnotationKind.CLASS, S, X>;
  onClass<X = unknown>(
    type?: ConstructorType<X> | X,
  ): AnnotationsSelector<AnnotationKind.CLASS, S, X> {
    return this.createSelector([AnnotationKind.CLASS], type);
  }
 
  onMethod<X = any, K extends keyof X = keyof X>(
    type?: ConstructorType<X> | X,
    propertyKey?: K,
  ): AnnotationsSelector<AnnotationKind.METHOD, S, X> {
    return this.createSelector(
      [AnnotationKind.METHOD],
      type ? getPrototype(type).constructor : undefined,
      propertyKey,
    );
  }
  onProperty<X, K extends keyof X = keyof X>(
    type?: ConstructorType<X>,
    propertyKey?: K,
  ): AnnotationsSelector<AnnotationKind.PROPERTY, S, X>;
  onProperty<X, K extends keyof X = keyof X>(
    type?: X,
    propertyKey?: K,
  ): BoundAnnotationsSelector<AnnotationKind.PROPERTY, S, X>;
  onProperty<X, K extends keyof X = keyof X>(
    type?: ConstructorType<X> | X,
    propertyKey?: K,
  ): AnnotationsSelector<AnnotationKind.PROPERTY, S, X> {
    return this.createSelector([AnnotationKind.PROPERTY], type, propertyKey);
  }
 
  onParameters<X, K extends keyof X = keyof X>(
    type?: ConstructorType<X> | X,
    propertyKey?: K,
  ): AnnotationsSelector<AnnotationKind.PARAMETER, S, X> {
    return this.createSelector([AnnotationKind.PARAMETER], type, propertyKey);
  }
 
  onParameter<X, K extends keyof X = keyof X>(
    type: ConstructorType<X> | X,
    propertyKey: K,
    parameterIndex: number,
  ): AnnotationsSelector<AnnotationKind.PARAMETER, S, X> {
    return this.createSelector(
      [AnnotationKind.PARAMETER],
      type,
      propertyKey,
      parameterIndex,
    );
  }
 
  private createSelector<
    T extends AnnotationKind = AnnotationKind,
    X = unknown,
    K extends keyof X = any,
  >(
    annotationTypes: T[],
    targetType?: X | ConstructorType<X>,
    propertyKey?: K,
    parameterIndex?: number,
  ): AnnotationsSelector<T, S, X> {
    const selection = new AnnotationsSelector<T, S, X>(
      this.targetFactory,
      this.annotationSet,
      this.annotationsRefs,
      annotationTypes,
      targetType,
      propertyKey as string | symbol,
      parameterIndex,
    );
    if (isClassInstance(targetType)) {
      return new BoundAnnotationsSelector<T, S, X>(selection, targetType);
    }
 
    return selection;
  }
 
  on<X>(
    filter: AnnotationSelectionFilter,
  ): AnnotationsSelector<AnnotationKind, S, X> {
    const { target, types } = filter;
    assert(isObject(target));
 
    return new AnnotationsSelector<AnnotationKind, S, X>(
      this.targetFactory,
      this.annotationSet,
      this.annotationsRefs,
      types ??
        (Object.values(AnnotationKind).filter(
          (x: any) => !isNaN(x),
        ) as AnnotationKind[]),
      target.proto?.constructor,
      (target as AnnotationTarget<AnnotationKind.METHOD>).propertyKey as any,
    );
  }
}