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

10% Statements 4/40
0% Branches 0/14
0% Functions 0/7
10% Lines 4/40

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 15710x   10x                 10x                 10x                                                                                                                                                                                                                                                                                
import { ConstructorType, assert, getPrototype } from '@aspectjs/common/utils';
import { AnnotationContext } from '../../annotation-context';
import { AnnotationRef } from '../../annotation-ref';
import {
  Annotation,
  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 { AnnotationsSelector } from './selector';
 
export interface AnnotationsByTypeSelectionOptions {
  /**
   * Search for the annotation in parent classes.
   */
  searchParents: boolean;
}
 
export class AnnotationsByRefSelector<
  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;
 
  constructor(selection: AnnotationsByRefSelector<T, S, X>);
  constructor(
    targetFactory: AnnotationTargetFactory,
    annotationSet: _AnnotationContextSet,
    annotationsRefs: Set<AnnotationRef> | undefined,
    decoratorTypes: T[],
    type?: ConstructorType<X> | X,
    propertyKey?: string | symbol,
  );
  constructor(
    targetFactory: AnnotationTargetFactory | AnnotationsByRefSelector<T, S, X>,
    annotationSet?: _AnnotationContextSet,
    annotationsRefs?: Set<AnnotationRef> | undefined,
    decoratorTypes?: T[],
    type?: ConstructorType<X> | X,
    propertyKey?: string | symbol,
  ) {
    if (targetFactory instanceof AnnotationsByRefSelector) {
      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;
    } else {
      this.targetFactory = targetFactory;
      this.annotationSet = annotationSet!;
      this.annotationsRefs = annotationsRefs;
      this.decoratorTypes = decoratorTypes!;
      this.type = type ? getPrototype(type).constructor : undefined;
      this.propertyKey = propertyKey;
    }
  }
 
  annotations<S2 extends AnnotationStub>(
    annotation: Annotation<AnnotationKind, S2>,
  ): AnnotationsSelector<T, S2, X>;
  annotations(
    ...annotations: (
      | AnnotationRef
      | Annotation<AnnotationKind, AnnotationStub>
    )[]
  ): AnnotationsSelector<T, S, X>;
  annotations(
    ...annotations: (
      | Annotation<AnnotationKind, AnnotationStub>
      | AnnotationRef
    )[]
  ): AnnotationsSelector<T, S, X> {
    let annotationRefs = this.annotationsRefs;
    Iif (annotations.length) {
      if (!annotationRefs) {
        annotationRefs = new Set(annotations.map(AnnotationRef.of));
      } else {
        annotationRefs = new Set(
          annotations
            .map(AnnotationRef.of)
            .filter((a) => annotationRefs!.has(a)),
        );
      }
    }
 
    return new AnnotationsSelector(
      this.targetFactory,
      this.annotationSet,
      annotationRefs,
      this.decoratorTypes,
      this.type,
      this.propertyKey,
    );
  }
 
  find(
    options?: AnnotationsByTypeSelectionOptions,
  ): AnnotationContext<T, S, X>[] {
    Iif (!this.type) {
      assert(!options?.searchParents);
      assert(!!this.annotationSet);
      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];
 
    return classTargets
      .map((target) => target.ref)
      .flatMap((ref) =>
        this.annotationSet.getAnnotations(
          this.decoratorTypes,
          this.annotationsRefs,
          ref,
          this.propertyKey,
        ),
      ) as AnnotationContext<T, S, X>[];
  }
}
 
function getAncestors<T extends AnnotationKind>(
  target: AnnotationTarget<T, any>,
): Array<AnnotationTarget<T, any>> {
  Iif (!target.parentClass) {
    return [];
  }
  return [
    target.parentClass as AnnotationTarget<T, any>,
    ...getAncestors<T>(target.parentClass as AnnotationTarget<T, any>),
  ];
}