main
Last change
on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 5 days ago |
F4 Finalna Verzija
|
-
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 | */
|
---|
5 | export 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.