index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = _default;
  4. /**
  5. * Fixes block-shadowed let/const bindings in Safari 10/11.
  6. * https://kangax.github.io/compat-table/es6/#test-let_scope_shadow_resolution
  7. */
  8. function _default({
  9. types: t
  10. }) {
  11. return {
  12. name: "transform-safari-block-shadowing",
  13. visitor: {
  14. VariableDeclarator(path) {
  15. // the issue only affects let and const bindings:
  16. const kind = path.parent.kind;
  17. if (kind !== "let" && kind !== "const") return; // ignore non-block-scoped bindings:
  18. const block = path.scope.block;
  19. if (t.isFunction(block) || t.isProgram(block)) return;
  20. const bindings = t.getOuterBindingIdentifiers(path.node.id);
  21. for (const name of Object.keys(bindings)) {
  22. let scope = path.scope; // ignore parent bindings (note: impossible due to let/const?)
  23. if (!scope.hasOwnBinding(name)) continue; // check if shadowed within the nearest function/program boundary
  24. while (scope = scope.parent) {
  25. if (scope.hasOwnBinding(name)) {
  26. path.scope.rename(name);
  27. break;
  28. }
  29. if (t.isFunction(scope.block) || t.isProgram(scope.block)) {
  30. break;
  31. }
  32. }
  33. }
  34. }
  35. }
  36. };
  37. }
  38. module.exports = exports.default;