/
index.js
104 lines (84 loc) · 2.61 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
"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;
}
let firstNode = body[0].node.declarations[0];
if (!t.isIdentifier(firstNode.id) || !firstNode.init) {
return;
}
let init = path.get("init");
if (!init.isVariableDeclaration({ kind: kind })) {
return;
}
init.node.declarations = init.node.declarations.concat(
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;
}
let { node } = path;
while (true) {
let sibling = path.getSibling(path.key + 1);
if (!sibling.isVariableDeclaration({ kind: node.kind })) {
break;
}
node.declarations = node.declarations.concat(sibling.node.declarations);
sibling.remove();
}
},
// 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;
}
let next = path.getSibling(path.key + 1);
if (!next.isForStatement()) {
return;
}
let init = next.get("init");
if (!init.isVariableDeclaration({ kind: node.kind })) {
return;
}
init.node.declarations = node.declarations.concat(
init.node.declarations
);
path.remove();
},
],
},
},
};
};