Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395/** Copies one function's identity onto another: own properties, prototype, and a `toString` that wraps the source body. Inlined from [`mimic-function`](https://github.com/sindresorhus/mimic-function) by Sindre Sorhus (MIT; notice preserved in `LICENSES/MIT.txt`) at version 5.0.1 so the fork keeps zero runtime dependencies on the memoization path. Call shape follows repository lint (one destructured object parameter), and the wrapper's `name` descriptor is derived from the wrapper itself rather than read from `Function.prototype.toString.name` (observably identical, without referencing a built-in method unbound). @module */
import { PropertyDescriptorMissingError, } from './errors.ts';
//region Captured descriptors
/** Descriptor of the `toString` property on `Function.prototype`, captured at module load like upstream `mimic-function`, so later patches to `toString` cannot change the replacement's property flags. */const TOSTRING_DESCRIPTOR = Object.getOwnPropertyDescriptor( Function.prototype, 'toString',);
//endregion Captured descriptors
//region Descriptor helpers
/** Tests whether copying `fromDescriptor` onto `to` is legal. `Object.defineProperty()` throws when the target property already exists, is not configurable, and either one of its descriptor fields changes or its non-writable value would be replaced; this predicate reports exactly those legal copies. @param toDescriptor - Own descriptor of the copy target, absent when the property is new. @param fromDescriptor - Own descriptor of the source property. @returns `true` when `Object.defineProperty` would accept the copy. @example ```ts canCopyProperty({ toDescriptor: undefined, fromDescriptor: Object.getOwnPropertyDescriptor(fn, 'name',), },); // => true ``` */export function canCopyProperty({ toDescriptor, fromDescriptor,}: { // oxlint-disable-next-line no-restricted-syntax/no-nullish-union -- mirrors `Object.getOwnPropertyDescriptor`'s documented `PropertyDescriptor | undefined` return for a missing target property readonly toDescriptor: PropertyDescriptor | undefined; readonly fromDescriptor: PropertyDescriptor;},): boolean { return (toDescriptor === undefined) || (toDescriptor.configurable === true) || ((toDescriptor.writable === fromDescriptor.writable) && ((toDescriptor.enumerable === fromDescriptor.enumerable) && ((toDescriptor.configurable === fromDescriptor.configurable) && ((toDescriptor.writable === true) || (toDescriptor.value === fromDescriptor.value)))));}
/** Copies one own property from source to target when legal. `length` and `prototype` are skipped because the target keeps its own parameter count and its non-writable prototype; `arguments` and `caller` are skipped because some runtimes surface them through `Reflect.ownKeys` and they must never be copied. @param to - Function receiving the copied property. @param from - Function the property is copied from. @param property - Own property name or symbol to copy. @param ignoreNonConfigurable - Whether illegal copies are silently skipped instead of thrown. @example ```ts copyProperty({ to: memoized, from: fn, property: 'name', ignoreNonConfigurable: true, },); ``` */export function copyProperty({ to, from, property, ignoreNonConfigurable,}: { readonly to: object; readonly from: object; readonly property: string | symbol; readonly ignoreNonConfigurable: boolean;},): void { if ((property === 'length') || (property === 'prototype')) return;
if ((property === 'arguments') || (property === 'caller')) return;
/** Own descriptor of the copy target, absent when the property is new. */ const toDescriptor = Object.getOwnPropertyDescriptor( to, property, ); /** Own descriptor of the source property, always present because the property came from `Reflect.ownKeys(from)`. */ const fromDescriptor = Object.getOwnPropertyDescriptor( from, property, );
if (fromDescriptor === undefined) throw new PropertyDescriptorMissingError();
/** Whether defining this property would throw on the target. */ const copyIsIllegal = !canCopyProperty({ toDescriptor, fromDescriptor, },);
if (copyIsIllegal && ignoreNonConfigurable) return;
Object.defineProperty( to, property, fromDescriptor, );}
//endregion Descriptor helpers
//region Prototype and toString
/** Points the target's prototype chain at the source's prototype, so methods and `instanceof` checks see the source's lineage. @param to - Function whose prototype chain is aligned. @param from - Function whose prototype is inherited. @example ```ts changePrototype({ to: memoized, from: fn, },); ``` */export function changePrototype({ to, from,}: { readonly to: object; readonly from: object;},): void { /** Prototype the source function itself inherits from. */ const fromPrototype = Reflect.getPrototypeOf(from,);
// mutation-test-disable-next-line conditional -- re-setting an identical prototype is a no-op on every runtime; the guard is a pure fast path if (fromPrototype === Reflect.getPrototypeOf(to,)) return;
Reflect.setPrototypeOf( to, fromPrototype, );}
/** Builds the wrapped `toString` body: a marker naming the wrapper followed by the source function's own `toString` output. @param withName - Wrapper name marker embedded in the wrapped body. @param fromBody - Pre-captured source body text. @returns Wrapped body text. @example ```ts wrappedToString({ withName: 'with memoized() ', fromBody: 'function fetchUser() {}', },); ``` */export function wrappedToString({ withName, fromBody,}: { readonly withName: string; readonly fromBody: string;},): string { return `/* Wrapped ${withName}*/\n${fromBody}`;}
/** Installs a `toString` on the target that reports the wrapped source body. The source body is captured now (not lazily) so the source function can be garbage collected, and the bound wrapper keeps repeated `toString()` calls cheap. The wrapper's `name` becomes `toString` with the standard function-name flags, matching upstream's copy of `Function.prototype.toString.name`. @param to - Function receiving the wrapped `toString`. @param from - Function whose body text is wrapped. @param name - Target name embedded in the wrapper marker. @example ```ts changeToString({ to: memoized, from: fn, name: 'memoized', },); ``` */export function changeToString({ to, from, name,}: { readonly to: object; readonly from: { readonly toString: () => string; }; readonly name: string;},): void { /** Name marker shown in the wrapped `toString` output. */ const withName = (name === '') ? '' : `with ${name.trim()}() `; /** `toString` replacement bound to the marker and the captured source body. */ const newToString = wrappedToString.bind( null, { withName, fromBody: from.toString(), }, );
/** Own name descriptor of the bound wrapper, carrying the standard function-name flags. */ const wrapperNameDescriptor = Object.getOwnPropertyDescriptor( newToString, 'name', );
// mutation-test-disable-next-line conditional -- every function carries a name descriptor, so the guard is unreachable; it exists to fail loud if that invariant breaks if (wrapperNameDescriptor === undefined) throw new PropertyDescriptorMissingError();
Object.defineProperty( newToString, 'name', { ...wrapperNameDescriptor, value: 'toString', }, );
if (TOSTRING_DESCRIPTOR === undefined) throw new PropertyDescriptorMissingError();
/** Replacement descriptor copying `Function.prototype.toString`'s flags; absent flags become `false`, which is what `Object.defineProperty` does with upstream's explicit `undefined` fields. */ const replacementDescriptor: PropertyDescriptor = { value: newToString, // mutation-test-disable-next-line boolean -- `Function.prototype.toString` always carries full data-descriptor flags, so this fallback is unreachable on conforming runtimes writable: TOSTRING_DESCRIPTOR.writable ?? false, // mutation-test-disable-next-line boolean -- same unreachable fallback as `writable` above enumerable: TOSTRING_DESCRIPTOR.enumerable ?? false, // mutation-test-disable-next-line boolean -- same unreachable fallback as `writable` above configurable: TOSTRING_DESCRIPTOR.configurable ?? false, };
Object.defineProperty( to, 'toString', replacementDescriptor, );}
//endregion Prototype and toString
//region Mimic
/** Copies the source function's identity onto the target function. Target keeps its own body and parameter count (`length` is never copied), while name, own properties, prototype, and wrapped `toString` follow the source, so the wrapper is indistinguishable from the wrapped function to reflection-based callers. @param to - Target function to extend. @param from - Source function to copy from. @param ignoreNonConfigurable - Whether illegal property copies are skipped instead of thrown. @returns The extended target function. @example ```ts mimicFunction({ to: memoized, from: fn, ignoreNonConfigurable: true, },); ``` */export function mimicFunction<Target extends { readonly name: string; }>({ to, from, ignoreNonConfigurable = false,}: { readonly to: Target; readonly from: { readonly toString: () => string; }; readonly ignoreNonConfigurable?: boolean;},): Target { /** Target name captured before copying overwrites it; reused by the wrapped `toString` marker. */ const { name, } = to;
for (const property of Reflect.ownKeys(from,)) copyProperty({ to, from, property, ignoreNonConfigurable, },);
changePrototype({ to, from, },); changeToString({ to, from, name, },);
return to;}
//endregion Mimic