fd5dc42e483f61cc49c836c0677dceba8f47a0d17f167ba0259a2f27fac7c228997aac716abc1c5a26acac5d5825144f02c6dbd80c9e1a63b1a0d8be6ad534 1.3 KB

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