Skip to content

Commit

Permalink
chore: Patch mimic-fn for enablements compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Mar 14, 2024
1 parent f171f91 commit 7969ac9
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/bundle-source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"./package.json": "./package.json"
},
"scripts": {
"postinstall": "patch-package",
"build": "exit 0",
"build:types": "tsc --build tsconfig.build.json",
"clean:types": "git clean -f '*.d.ts*'",
Expand Down
136 changes: 136 additions & 0 deletions packages/bundle-source/patches/mimic-fn+4.0.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
diff --git a/node_modules/mimic-fn/index.js b/node_modules/mimic-fn/index.js
index bc9ef7d..a075a8e 100644
--- a/node_modules/mimic-fn/index.js
+++ b/node_modules/mimic-fn/index.js
@@ -1,71 +1,89 @@
const copyProperty = (to, from, property, ignoreNonConfigurable) => {
- // `Function#length` should reflect the parameters of `to` not `from` since we keep its body.
- // `Function#prototype` is non-writable and non-configurable so can never be modified.
- if (property === 'length' || property === 'prototype') {
- return;
- }
+ // `Function#length` should reflect the parameters of `to` not `from` since we keep its body.
+ // `Function#prototype` is non-writable and non-configurable so can never be modified.
+ if (property === 'length' || property === 'prototype') {
+ return;
+ }

- // `Function#arguments` and `Function#caller` should not be copied. They were reported to be present in `Reflect.ownKeys` for some devices in React Native (#41), so we explicitly ignore them here.
- if (property === 'arguments' || property === 'caller') {
- return;
- }
+ // `Function#arguments` and `Function#caller` should not be copied. They were reported to be present in `Reflect.ownKeys` for some devices in React Native (#41), so we explicitly ignore them here.
+ if (property === 'arguments' || property === 'caller') {
+ return;
+ }

- const toDescriptor = Object.getOwnPropertyDescriptor(to, property);
- const fromDescriptor = Object.getOwnPropertyDescriptor(from, property);
+ const toDescriptor = Object.getOwnPropertyDescriptor(to, property);
+ const fromDescriptor = Object.getOwnPropertyDescriptor(from, property);

- if (!canCopyProperty(toDescriptor, fromDescriptor) && ignoreNonConfigurable) {
- return;
- }
+ if (!canCopyProperty(toDescriptor, fromDescriptor) && ignoreNonConfigurable) {
+ return;
+ }

- Object.defineProperty(to, property, fromDescriptor);
+ Object.defineProperty(to, property, fromDescriptor);
};

// `Object.defineProperty()` throws if the property exists, is not configurable and either:
// - one its descriptors is changed
// - it is non-writable and its value is changed
const canCopyProperty = function (toDescriptor, fromDescriptor) {
- return toDescriptor === undefined || toDescriptor.configurable || (
- toDescriptor.writable === fromDescriptor.writable &&
- toDescriptor.enumerable === fromDescriptor.enumerable &&
- toDescriptor.configurable === fromDescriptor.configurable &&
- (toDescriptor.writable || toDescriptor.value === fromDescriptor.value)
- );
+ return (
+ toDescriptor === undefined ||
+ toDescriptor.configurable ||
+ (toDescriptor.writable === fromDescriptor.writable &&
+ toDescriptor.enumerable === fromDescriptor.enumerable &&
+ toDescriptor.configurable === fromDescriptor.configurable &&
+ (toDescriptor.writable || toDescriptor.value === fromDescriptor.value))
+ );
};

const changePrototype = (to, from) => {
- const fromPrototype = Object.getPrototypeOf(from);
- if (fromPrototype === Object.getPrototypeOf(to)) {
- return;
- }
+ const fromPrototype = Object.getPrototypeOf(from);
+ if (fromPrototype === Object.getPrototypeOf(to)) {
+ return;
+ }

- Object.setPrototypeOf(to, fromPrototype);
+ Object.setPrototypeOf(to, fromPrototype);
};

-const wrappedToString = (withName, fromBody) => `/* Wrapped ${withName}*/\n${fromBody}`;
+const wrappedToString = (withName, fromBody) =>
+ `/* Wrapped ${withName}*/\n${fromBody}`;

-const toStringDescriptor = Object.getOwnPropertyDescriptor(Function.prototype, 'toString');
-const toStringName = Object.getOwnPropertyDescriptor(Function.prototype.toString, 'name');
+const toStringDescriptor = Object.getOwnPropertyDescriptor(
+ Function.prototype,
+ 'toString',
+);
+const toStringName = Object.getOwnPropertyDescriptor(
+ Function.prototype.toString,
+ 'name',
+);

// We call `from.toString()` early (not lazily) to ensure `from` can be garbage collected.
// We use `bind()` instead of a closure for the same reason.
// Calling `from.toString()` early also allows caching it in case `to.toString()` is called several times.
const changeToString = (to, from, name) => {
- const withName = name === '' ? '' : `with ${name.trim()}() `;
- const newToString = wrappedToString.bind(null, withName, from.toString());
- // Ensure `to.toString.toString` is non-enumerable and has the same `same`
- Object.defineProperty(newToString, 'name', toStringName);
- Object.defineProperty(to, 'toString', {...toStringDescriptor, value: newToString});
+ const withName = name === '' ? '' : `with ${name.trim()}() `;
+ const newToString = wrappedToString.bind(null, withName, from.toString());
+ // Ensure `to.toString.toString` is non-enumerable and has the same `same`
+ Object.defineProperty(newToString, 'name', toStringName);
+ const { writable, configurable } = toStringDescriptor;
+ Object.defineProperty(to, 'toString', {
+ value: newToString,
+ writable,
+ configurable,
+ });
};

-export default function mimicFunction(to, from, {ignoreNonConfigurable = false} = {}) {
- const {name} = to;
+export default function mimicFunction(
+ to,
+ from,
+ { ignoreNonConfigurable = false } = {},
+) {
+ const { name } = to;

- for (const property of Reflect.ownKeys(from)) {
- copyProperty(to, from, property, ignoreNonConfigurable);
- }
+ for (const property of Reflect.ownKeys(from)) {
+ copyProperty(to, from, property, ignoreNonConfigurable);
+ }

- changePrototype(to, from);
- changeToString(to, from, name);
+ changePrototype(to, from);
+ changeToString(to, from, name);

- return to;
+ return to;
}

0 comments on commit 7969ac9

Please sign in to comment.