6dd028a668dc37856829f499f4fb8e03811e31c0798169ab7505f387445d0fa3cd0c2e2154dc5e4719b7331307bae2023a5356a9aa5da3a3d8b644f0fb0b6a 1012 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * Safari ~11 has an issue where variable declarations in a For statement throw if they shadow parameters.
  3. * This is fixed by renaming any declarations in the left/init part of a For* statement so they don't shadow.
  4. * @see https://bugs.webkit.org/show_bug.cgi?id=171041
  5. *
  6. * @example
  7. * e => { for (let e of []) e } // throws
  8. * e => { for (let _e of []) _e } // works
  9. */
  10. function handle(declaration) {
  11. if (!declaration.isVariableDeclaration()) return;
  12. const fn = declaration.getFunctionParent();
  13. const { name } = declaration.node.declarations[0].id;
  14. // check if there is a shadowed binding coming from a parameter
  15. if (
  16. fn &&
  17. fn.scope.hasOwnBinding(name) &&
  18. fn.scope.getOwnBinding(name).kind === "param"
  19. ) {
  20. declaration.scope.rename(name);
  21. }
  22. }
  23. export 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. });