-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathindex.js
116 lines (94 loc) · 3.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"use strict";
module.exports = function({ types: t }) {
function liftDeclaration(path, body, kind) {
if (body[0] && body[0].isVariableDeclaration({ kind: kind })) {
if (body[0].node.declarations.length > 1) {
return;
}
if (body[1] && body[1].isVariableDeclaration({ kind: kind })) {
return;
}
const firstNode = body[0].node.declarations[0];
if (!t.isIdentifier(firstNode.id) || !firstNode.init) {
return;
}
const init = path.get("init");
if (!init.isVariableDeclaration({ kind: kind })) {
return;
}
init.pushContainer("declarations", t.variableDeclarator(firstNode.id));
body[0].replaceWith(
t.assignmentExpression(
"=",
t.clone(firstNode.id),
t.clone(firstNode.init)
)
);
}
}
return {
name: "transform-merge-sibling-variables",
visitor: {
ForStatement(path) {
// Lift declarations to the loop initializer
let body = path.get("body");
body = body.isBlockStatement() ? body.get("body") : [body];
liftDeclaration(path, body, "var");
liftDeclaration(path, body, "let");
},
VariableDeclaration: {
enter: [
// concat variables of the same kind with their siblings
function(path) {
if (!path.inList) {
return;
}
const { node } = path;
let sibling = path.getSibling(path.key + 1);
let declarations = [];
while (sibling.isVariableDeclaration({ kind: node.kind })) {
declarations = declarations.concat(sibling.node.declarations);
sibling.remove();
sibling = path.getSibling(path.key + 1);
}
if (declarations.length > 0) {
path.replaceWith(
t.variableDeclaration(node.kind, [
...node.declarations,
...declarations
])
);
}
},
// concat `var` declarations next to for loops with it's initialisers.
// block-scoped `let` and `const` are not moved because the for loop
// is a different block scope.
function(path) {
if (!path.inList) {
return;
}
const { node } = path;
if (node.kind !== "var") {
return;
}
const next = path.getSibling(path.key + 1);
if (!next.isForStatement()) {
return;
}
const init = next.get("init");
if (!init.isVariableDeclaration({ kind: node.kind })) {
return;
}
const declarations = node.declarations.concat(
init.node.declarations
);
// temporary workaround to forces babel recalculate scope,
// references and binding until babel/babel#4818 resolved
path.remove();
init.replaceWith(t.variableDeclaration("var", declarations));
}
]
}
}
};
};