index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. /**
  5. * Safari ~11 has an issue where variable declarations in a For statement throw if they shadow parameters.
  6. * This is fixed by renaming any declarations in the left/init part of a For* statement so they don't shadow.
  7. * @see https://bugs.webkit.org/show_bug.cgi?id=171041
  8. *
  9. * @example
  10. * e => { for (let e of []) e } // throws
  11. * e => { for (let _e of []) _e } // works
  12. */
  13. function handle(declaration) {
  14. if (!declaration.isVariableDeclaration()) return;
  15. const fn = declaration.getFunctionParent();
  16. const {
  17. name
  18. } = declaration.node.declarations[0].id; // check if there is a shadowed binding coming from a parameter
  19. if (fn && fn.scope.hasOwnBinding(name) && fn.scope.getOwnBinding(name).kind === "param") {
  20. declaration.scope.rename(name);
  21. }
  22. }
  23. var _default = () => ({
  24. name: "transform-safari-for-shadowing",
  25. visitor: {
  26. ForXStatement(path) {
  27. handle(path.get("left"));
  28. },
  29. ForStatement(path) {
  30. handle(path.get("init"));
  31. }
  32. }
  33. });
  34. exports.default = _default;
  35. module.exports = exports.default;