| 1 |
import type { FunctionExpression } from "@babel/types"; |
| 2 |
import type { NodePath } from "@babel/traverse"; |
| 3 |
|
| 4 |
/** |
| 5 |
* Check whether a function expression can be affected by |
| 6 |
* https://bugs.webkit.org/show_bug.cgi?id=220517 |
| 7 |
* @param path The function expression NodePath |
| 8 |
* @returns the name of function id if it should be transformed, otherwise returns false |
| 9 |
*/ |
| 10 |
export function shouldTransform( |
| 11 |
path: NodePath<FunctionExpression>, |
| 12 |
): string | false { |
| 13 |
const { node } = path; |
| 14 |
const functionId = node.id; |
| 15 |
if (!functionId) return false; |
| 16 |
|
| 17 |
const name = functionId.name; |
| 18 |
// On collision, `getOwnBinding` returns the param binding |
| 19 |
// with the id binding be registered as constant violation |
| 20 |
const paramNameBinding = path.scope.getOwnBinding(name); |
| 21 |
if (paramNameBinding === undefined) { |
| 22 |
// Case 1: the function id is injected by babel-helper-name-function, which |
| 23 |
// assigns `NOT_LOCAL_BINDING` to the `functionId` and thus not registering id |
| 24 |
// in scope tracking |
| 25 |
// Case 2: the function id is injected by a third party plugin which does not update the |
| 26 |
// scope info |
| 27 |
return false; |
| 28 |
} |
| 29 |
if (paramNameBinding.kind !== "param") { |
| 30 |
// the function id does not reproduce in params |
| 31 |
return false; |
| 32 |
} |
| 33 |
|
| 34 |
if (paramNameBinding.identifier === paramNameBinding.path.node) { |
| 35 |
// the param binding is a simple parameter |
| 36 |
// e.g. (function a(a) {}) |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
return name; |
| 41 |
} |