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

67.34% Statements 33/49
63.15% Branches 12/19
50% Functions 6/12
68.75% Lines 33/48

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 19110x                 10x 10x           10x         10x 10x   10x   10x                           263x               263x                     263x 263x 263x                                   470x 470x 470x   470x 470x   470x       470x         470x         263x                               933x 670x     263x     263x       263x     1x       1x           1x     1x                                                                       263x 263x                    
import {
  ConstructorType,
  MethodPropertyDescriptor,
  Prototype,
  assert,
  defineMetadata,
  getMetadata,
  getPrototype,
} from '@aspectjs/common/utils';
import { AnnotationKind } from '../../annotation.types';
import {
  AnnotationTargetRef,
  ClassAnnotationTarget,
  MethodAnnotationTarget,
  ParameterAnnotationTarget,
} from '../annotation-target';
import {
  BOUND_INSTANCE_SYMBOL,
  BOUND_VALUE_SYMBOL,
  _AnnotationTargetImpl,
} from '../annotation-target.impl';
import { _ClassAnnotationTargetImpl } from './class-annotation-target.impl';
import { _MethodAnnotationTargetImpl } from './method-annotation-target.impl';
 
let _globalTargetId = 0;
 
export class _ParameterAnnotationTargetImpl<X>
  extends _AnnotationTargetImpl<AnnotationKind.PARAMETER, X>
  implements ParameterAnnotationTarget<X>
{
  readonly propertyKey: string | symbol;
  readonly descriptor: MethodPropertyDescriptor;
  readonly parameterIndex: number;
  protected declare [BOUND_INSTANCE_SYMBOL]?: X;
  protected declare [BOUND_VALUE_SYMBOL]?: (...args: unknown[]) => unknown;
 
  private _declaringClassTarget?: ClassAnnotationTarget<X>;
  private _methodTarget?: MethodAnnotationTarget<X>;
 
  constructor(
    private readonly decoree: Prototype<X> | ConstructorType<X>,
    proto: Prototype<X>,
    propertyKey: string | symbol,
    descriptor: MethodPropertyDescriptor,
    parameterIndex: number,
    ref: AnnotationTargetRef,
    isStatic: boolean,
  ) {
    super(
      AnnotationKind.PARAMETER,
      proto,
      argsNames(proto[propertyKey as any])[parameterIndex!] ??
        `#${parameterIndex!}`,
      `${isStatic ? 'static ' : ''}argument ${proto.constructor.name}.${String(
        propertyKey,
      )}[${parameterIndex}]`,
      ref,
      isStatic,
    );
    this.propertyKey = propertyKey;
    this.descriptor = descriptor;
    this.parameterIndex = parameterIndex;
  }
 
  override defineMetadata(key: string, value: any): void {
    defineMetadata(key, value, this.proto, this.propertyKey);
  }
  override getMetadata<T>(
    key: string,
    defaultvalue?: (() => T) | undefined,
  ): T {
    return getMetadata(key, this.proto, this.propertyKey, defaultvalue);
  }
 
  static of<X>(
    decoree: Prototype<X> | ConstructorType<X>,
    propertyKey: string | symbol,
    parameterIndex: number,
  ) {
    assert(typeof decoree === 'object' || typeof decoree === 'function');
    assert(typeof propertyKey === 'string' || typeof propertyKey === 'symbol');
    assert(typeof parameterIndex === 'number');
 
    const proto = getPrototype(decoree);
    const isStatic = typeof decoree === 'function';
 
    const ref = `c[${proto.constructor.name}].${isStatic ? 's ' : ''}m[${String(
      propertyKey,
    )}].a[${parameterIndex}]`;
 
    const descriptor = Object.getOwnPropertyDescriptor(
      decoree,
      propertyKey,
    ) as MethodPropertyDescriptor;
 
    return getMetadata(
      `@ajs:tgrf`,
      decoree,
      ref,
      () =>
        new _ParameterAnnotationTargetImpl(
          decoree,
          proto,
          propertyKey,
          descriptor,
          parameterIndex,
          new AnnotationTargetRef(`${ref}#${_globalTargetId++}`),
          isStatic,
        ),
    );
  }
 
  asDecoratorArgs() {
    return [this.proto, this.propertyKey, this.parameterIndex];
  }
  get declaringClass() {
    if (this._declaringClassTarget) {
      return this._declaringClassTarget;
    }
 
    const classTarget = _ClassAnnotationTargetImpl.of<X>(
      this.proto.constructor,
    );
    this._declaringClassTarget = this[BOUND_INSTANCE_SYMBOL]
      ? classTarget._bind(this[BOUND_INSTANCE_SYMBOL])
      : classTarget;
 
    return this._declaringClassTarget;
  }
  get declaringMethod(): MethodAnnotationTarget<X> {
    Iif (this._methodTarget) {
      return this._methodTarget;
    }
 
    const parentTarget = _MethodAnnotationTargetImpl.of(
      this.decoree,
      this.propertyKey,
      this.descriptor,
    );
 
    this._methodTarget = this[BOUND_INSTANCE_SYMBOL]
      ? parentTarget._bind(this[BOUND_INSTANCE_SYMBOL])
      : parentTarget;
    return this._methodTarget;
  }
  override get parentClass() {
    return this.declaringClass.parentClass;
  }
 
  override _bind(instance: X, args: unknown[]): ParameterAnnotationTarget<X> {
    Iif (this.static) {
      return this;
    }
    Iif (this[BOUND_INSTANCE_SYMBOL] === instance) {
      return this;
    }
 
    const bound = new _ParameterAnnotationTargetImpl(
      this.decoree,
      this.proto,
      this.propertyKey,
      this.descriptor,
      this.parameterIndex,
      this.ref,
      this.static,
    );
 
    bound[BOUND_INSTANCE_SYMBOL] = instance;
    bound[BOUND_VALUE_SYMBOL] = bound.proto[bound.propertyKey as any];
 
    Iif (args) {
      bound.eval = () => args[bound.parameterIndex];
    }
 
    return bound;
  }
}
 
function argsNames(func: unknown | ((...args: unknown[]) => unknown)) {
  assert(typeof func === 'function');
  return (func + '')
    .replace(/[/][/].*$/gm, '') // strip single-line comments
    .replace(/\s+/g, '') // strip white space
    .replace(/[/][*][^/*]*[*][/]/g, '') // strip multi-line comments
    .split('){', 1)[0]!
    .replace(/^[^(]*[(]/, '') // extract the parameters
    .replace(/=[^,]+/g, '') // strip any ES6 defaults
    .split(',')
    .filter(Boolean); // split & filter [""]
}