All files / common/src/annotation/context/registry selector.ts

97.61% Statements 41/42
83.33% Branches 10/12
100% Functions 9/9
97.61% Lines 41/42

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 15910x                                 10x                                                               60x 2x 2x 2x 2x 2x 2x 2x 2x   58x 58x 58x 58x 58x 58x 58x         56x 21x   21x               35x   35x 35x           35x       35x 44x   44x               35x 33x     2x   2x                 44x 35x   9x           10x             2x 2x   2x         2x 8x                          
import { ConstructorType, assert, getPrototype } from '@aspectjs/common/utils';
import { AnnotationContext } from '../../annotation-context';
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 { _AnnotationTargetImpl } from '../../target/annotation-target.impl';
import { _AnnotationContextSet } from './annotation-context-set';
import { BoundAnnotationContext } from './bound-annotation-context';
 
export interface AnnotationSelectorOptions {
  /**
   * Search for the annotation in parent classes.
   */
  searchParents: boolean;
}
 
export class AnnotationsSelector<
  T extends AnnotationKind = AnnotationKind,
  S extends AnnotationStub = AnnotationStub,
  X = unknown,
> {
  private readonly targetFactory: AnnotationTargetFactory;
  private readonly annotationSet: _AnnotationContextSet;
  private readonly annotationsRefs: Set<AnnotationRef> | undefined;
  private readonly decoratorTypes: T[];
  private readonly type?: ConstructorType<X>;
  private readonly propertyKey?: string | symbol;
  private readonly parameterIndex?: number;
 
  constructor(selector: AnnotationsSelector<T, S, X>);
  constructor(
    targetFactory: AnnotationTargetFactory,
    annotationSet: _AnnotationContextSet,
    annotationsRefs: Set<AnnotationRef> | undefined,
    decoratorTypes: T[],
    type?: ConstructorType<X> | X,
    propertyKey?: string | symbol,
    parameterIndex?: number,
  );
  constructor(
    targetFactory: AnnotationTargetFactory | AnnotationsSelector<T, S, X>,
    annotationSet?: _AnnotationContextSet,
    annotationsRefs?: Set<AnnotationRef> | undefined,
    decoratorTypes?: T[],
    type?: ConstructorType<X> | X,
    propertyKey?: string | symbol,
    parameterIndex?: number,
  ) {
    if (targetFactory instanceof AnnotationsSelector) {
      const selection = targetFactory;
      this.targetFactory = selection.targetFactory;
      this.annotationSet = selection.annotationSet;
      this.annotationsRefs = selection.annotationsRefs;
      this.decoratorTypes = selection.decoratorTypes;
      this.type = selection.type;
      this.propertyKey = selection.propertyKey;
      this.parameterIndex = selection.parameterIndex;
    } else {
      this.targetFactory = targetFactory;
      this.annotationSet = annotationSet!;
      this.annotationsRefs = annotationsRefs;
      this.decoratorTypes = decoratorTypes!;
      this.type = type ? getPrototype(type).constructor : undefined;
      this.propertyKey = propertyKey;
      this.parameterIndex = parameterIndex;
    }
  }
 
  find(options?: AnnotationSelectorOptions): AnnotationContext<T, S, X>[] {
    if (!this.type) {
      assert(!options?.searchParents);
 
      return this.annotationSet.getAnnotations(
        this.decoratorTypes,
        this.annotationsRefs,
        undefined,
        this.propertyKey,
      ) as AnnotationContext<T, S, X>[];
    }
    // search annotations on given type
    const classTarget = this.targetFactory.of(this.type);
 
    assert(!!classTarget);
    Iif (!classTarget) {
      return [];
    }
 
    // search extends the search to parent types ?
    const classTargets =
      options?.searchParents ?? true
        ? [classTarget, ...getAncestors(classTarget)]
        : [classTarget];
 
    const contexts = classTargets
      .map((target) => target.ref)
      .flatMap((ref) =>
        this.annotationSet.getAnnotations(
          this.decoratorTypes,
          this.annotationsRefs,
          ref,
          this.propertyKey,
        ),
      ) as AnnotationContext<T, S, X>[];
 
    if (typeof this.parameterIndex === 'undefined') {
      return contexts;
    }
 
    return contexts.filter(
      (c) =>
        (c.target as AnnotationTarget<AnnotationKind.PARAMETER>)
          .parameterIndex === this.parameterIndex,
    );
  }
}
 
function getAncestors<T extends AnnotationKind>(
  target: AnnotationTarget<T, any>,
): Array<AnnotationTarget<T, any>> {
  if (!target.parentClass) {
    return [];
  }
  return [
    target.parentClass as AnnotationTarget<T, any>,
    ...getAncestors<T>(target.parentClass as AnnotationTarget<T, any>),
  ];
}
 
export class BoundAnnotationsSelector<
  T extends AnnotationKind = AnnotationKind,
  S extends AnnotationStub = AnnotationStub,
  X = unknown,
> extends AnnotationsSelector<T, S, X> {
  constructor(
    selector: AnnotationsSelector<T, S, X>,
    private instance: unknown,
    private args?: unknown[],
  ) {
    super(selector);
  }
  override find(
    options?: AnnotationSelectorOptions,
  ): BoundAnnotationContext<T, S, X>[] {
    return super.find(options).map((ctxt) => {
      return Object.setPrototypeOf(
        {
          ...ctxt,
          target: (ctxt.target as _AnnotationTargetImpl)._bind(
            this.instance,
            this.args,
          ),
        },
        Object.getPrototypeOf(ctxt),
      );
    });
  }
}