source: trip-planner-front/node_modules/@babel/preset-modules/src/plugins/transform-safari-block-shadowing/index.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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