Urara-Blog/node_modules/.pnpm-store/v3/files/f6/fd5dc42e483f61cc49c836c0677dceba8f47a0d17f167ba0259a2f27fac7c228997aac716abc1c5a26acac5d5825144f02c6dbd80c9e1a63b1a0d8be6ad534
2022-08-14 01:14:53 +08:00

31 lines
1.3 KiB
Text

/**
* Edge 16 & 17 do not infer function.name from variable assignment.
* All other `function.name` behavior works fine, so we can skip most of @babel/transform-function-name.
* @see https://kangax.github.io/compat-table/es6/#test-function_name_property_variables_(function)
*
* Note: contrary to various Github issues, Edge 16+ *does* correctly infer the name of Arrow Functions.
* The variable declarator name inference issue only affects function expressions, so that's all we fix here.
*
* A Note on Minification: Terser undoes this transform *by default* unless `keep_fnames` is set to true.
* There is by design - if Function.name is critical to your application, you must configure
* your minifier to preserve function names.
*/
export default ({ types: t }) => ({
name: "transform-edge-function-name",
visitor: {
FunctionExpression: {
exit(path) {
if (!path.node.id && t.isIdentifier(path.parent.id)) {
const id = t.cloneNode(path.parent.id);
const binding = path.scope.getBinding(id.name);
// if the binding gets reassigned anywhere, rename it
if (binding?.constantViolations.length) {
path.scope.rename(id.name);
}
path.node.id = id;
}
},
},
},
});