(source: { [key: string]: any }, name: string, replacementFactory: (...args: any[]) => any)
| 19 | */ |
| 20 | |
| 21 | export function fill(source: { [key: string]: any }, name: string, replacementFactory: (...args: any[]) => any): void { |
| 22 | if (!(name in source)) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | // explicitly casting to unknown because we don't know the type of the method initially at all |
| 27 | const original = source[name] as unknown; |
| 28 | |
| 29 | if (typeof original !== 'function') { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | const wrapped = replacementFactory(original) as WrappedFunction; |
| 34 | |
| 35 | // Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work |
| 36 | // otherwise it'll throw "TypeError: Object.defineProperties called on non-object" |
| 37 | if (typeof wrapped === 'function') { |
| 38 | markFunctionWrapped(wrapped, original); |
| 39 | } |
| 40 | |
| 41 | try { |
| 42 | source[name] = wrapped; |
| 43 | } catch { |
| 44 | DEBUG_BUILD && debug.log(`Failed to replace method "${name}" in object`, source); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Defines a non-enumerable property on the given object. |
no test coverage detected