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 | 132x 132x 186x 132x 186x 186x 177x 9x 3x 6x 5x 1x 177x 1x 176x 184x 132x | // TC39 decorators reach the class through `context.metadata` (`Symbol.metadata`), which engines
// do not ship yet; it must exist before the first decorated class is evaluated.
(Symbol as { metadata?: symbol }).metadata ??= Symbol.for('Symbol.metadata');
/** What a decorator call decorates, independent of the decorator signature in use. */
export type DecoratorTarget = Readonly<{
/** Object that stores the metadata: the class (legacy) or its `context.metadata` bag (TC39). */
owner: object;
/** Decorated member name; `undefined` for a class. */
key: string | undefined;
/** Whether the member is static. */
isStatic: boolean;
}>;
type Tc39Context = Readonly<{
kind: string;
name?: string | symbol;
static?: boolean;
metadata?: object;
}>;
const isTc39Context = (value: unknown): value is Tc39Context =>
typeof value === 'object' &&
value !== null &&
typeof (value as { kind?: unknown }).kind === 'string';
const NO_METADATA =
'Decorators need Symbol.metadata: import postgrelize before the decorated class is defined.';
/**
* Tells the two decorator calling conventions apart: TC39 standard `(value, context)` and legacy
* `experimentalDecorators` `(constructor)` / `(target, key, descriptor?)`.
*
* @internal
*/
export class DecoratorSignature {
/**
* @param args - Arguments the decorator was called with.
* @returns The decorated class or member.
* @throws Error when the arguments match neither convention.
*/
resolve(args: readonly unknown[]): DecoratorTarget {
const [first, second] = args;
if (isTc39Context(second)) {
return this.#standard(second);
}
if (typeof first === 'function') {
return { owner: first, key: this.#name(second), isStatic: second !== undefined };
}
if (typeof first === 'object' && first !== null && second !== undefined) {
return { owner: first.constructor, key: this.#name(second), isStatic: false };
}
throw new Error('Unsupported decorator usage.');
}
#standard(context: Tc39Context): DecoratorTarget {
if (context.metadata === undefined) {
throw new Error(NO_METADATA);
}
return {
owner: context.metadata,
key: this.#name(context.name),
isStatic: context.static === true,
};
}
#name(key: unknown): string | undefined {
return typeof key === 'symbol' ? key.description : (key as string | undefined);
}
}
/** @internal Shared signature adapter. */
export const signature = new DecoratorSignature();
|