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

82.85% Statements 29/35
76.92% Branches 10/13
55.55% Functions 5/9
82.85% Lines 29/35

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 14010x                 10x 10x         10x         10x   10x   10x                                   214x                   214x 214x                                   432x 432x 432x   432x 432x   432x       432x         208x                             824x 615x     209x     209x       209x             6x     6x     6x               6x 6x 6x      
import {
  ConstructorType,
  MethodPropertyDescriptor,
  Prototype,
  assert,
  defineMetadata,
  getMetadata,
  getPrototype,
} from '@aspectjs/common/utils';
import { AnnotationKind } from '../../annotation.types';
import {
  AnnotationTargetRef,
  ClassAnnotationTarget,
  MethodAnnotationTarget,
} from '../annotation-target';
import {
  BOUND_INSTANCE_SYMBOL,
  BOUND_VALUE_SYMBOL,
  _AnnotationTargetImpl,
} from '../annotation-target.impl';
import { _ClassAnnotationTargetImpl } from './class-annotation-target.impl';
 
let _globalTargetId = 0;
 
export class _MethodAnnotationTargetImpl<X>
  extends _AnnotationTargetImpl<AnnotationKind.METHOD, X>
  implements MethodAnnotationTarget<X>
{
  readonly propertyKey: string | symbol;
  readonly descriptor: MethodPropertyDescriptor;
  protected declare [BOUND_INSTANCE_SYMBOL]?: X;
  protected declare [BOUND_VALUE_SYMBOL]?: (...args: unknown[]) => unknown;
 
  private _declaringClassTarget?: ClassAnnotationTarget<X>;
 
  private constructor(
    proto: Prototype<X>,
    propertyKey: string | symbol,
    descriptor: MethodPropertyDescriptor,
    ref: AnnotationTargetRef,
    isStatic: boolean,
  ) {
    super(
      AnnotationKind.METHOD,
      proto,
      String(propertyKey),
      `${isStatic ? 'static ' : ''}method ${proto.constructor.name}.${String(
        propertyKey,
      )}`,
      ref,
      isStatic,
    );
    this.propertyKey = propertyKey;
    this.descriptor = descriptor;
  }
 
  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,
    descriptor: MethodPropertyDescriptor,
  ) {
    assert(typeof decoree === 'object' || typeof decoree === 'function');
    assert(typeof propertyKey === 'string' || typeof propertyKey === 'symbol');
    assert(typeof descriptor === 'object');
 
    const proto = getPrototype(decoree);
    const isStatic = typeof decoree === 'function';
 
    const ref = `c[${proto.constructor.name}].${isStatic ? 's ' : ''}m[${String(
      propertyKey,
    )}]`;
 
    return getMetadata(
      `@ajs:tgrf`,
      decoree,
      ref,
      () =>
        new _MethodAnnotationTargetImpl(
          proto,
          propertyKey,
          descriptor,
          new AnnotationTargetRef(`${ref}#${_globalTargetId++}`),
          isStatic,
        ),
    );
  }
 
  asDecoratorArgs() {
    return [this.proto, this.propertyKey, this.descriptor];
  }
 
  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;
  }
  override get parentClass() {
    return this.declaringClass.parentClass;
  }
 
  override _bind(instance: X): MethodAnnotationTarget<X> {
    Iif (this.static) {
      return this;
    }
    Iif (this[BOUND_INSTANCE_SYMBOL] === instance) {
      return this;
    }
    const bound = new _MethodAnnotationTargetImpl(
      this.proto,
      this.propertyKey,
      this.descriptor,
      this.ref,
      this.static,
    );
 
    bound[BOUND_INSTANCE_SYMBOL] = instance;
    bound[BOUND_VALUE_SYMBOL] = bound.proto[bound.propertyKey as any];
    return bound;
  }
}