/
index.js
225 lines (196 loc) · 5.41 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"use strict";
const COLLAPSERS = [
require("./object-collapser"),
require("./array-collapser"),
require("./array-property-collapser"),
require("./set-collapser")
].map(Collapser => {
return new Collapser();
});
function getFunctionParent(path, scopeParent) {
const parent = path.findParent(p => p.isFunction());
// don"t traverse higher than the function the var is defined in.
return parent === scopeParent ? null : parent;
}
function getFunctionReferences(path, scopeParent, references = new Set()) {
for (
let func = getFunctionParent(path, scopeParent);
func;
func = getFunctionParent(func, scopeParent)
) {
const id = func.node.id;
const binding = id && func.scope.getBinding(id.name);
if (!binding) {
continue;
}
binding.referencePaths.forEach(path => {
if (!references.has(path)) {
references.add(path);
getFunctionReferences(path, scopeParent, references);
}
});
}
return references;
}
function getIdAndFunctionReferences(name, parent) {
// Returns false if there's an error. Otherwise returns a list of references.
const binding = parent.scope.getBinding(name);
if (!binding) {
return false;
}
const references = binding.referencePaths.reduce((references, ref) => {
references.add(ref);
getFunctionReferences(ref, parent, references);
return references;
}, new Set());
return Array.from(references);
}
function validateTopLevel(path) {
// Ensures the structure is of the form (roughly):
// {
// ...
// var foo = expr;
// ...
// }
// returns null if not of this form
// otherwise returns [foo as string, ?rval, index of the variable declaration]
const declarations = path.get("declarations");
if (declarations.length !== 1) {
return;
}
const declaration = declarations[0];
const id = declaration.get("id"),
init = declaration.get("init");
if (!id.isIdentifier()) {
return;
}
const parent = path.parentPath;
if (!parent.isBlockParent() || !parent.isScopable()) {
return;
}
const body = parent.get("body");
if (!Array.isArray(body)) {
return;
}
const startIndex = body.indexOf(path);
if (startIndex === -1) {
return;
}
return [id.node.name, init, startIndex];
}
function collectExpressions(path, isExprTypeValid) {
// input: ExprStatement => 'a | SequenceExpression
// SequenceExpression => 'a list
// Validates 'a is of the right type
// returns null if found inconsistency, else returns Array<"a>
if (path.isExpressionStatement()) {
const exprs = collectExpressions(path.get("expression"), isExprTypeValid);
return exprs !== null ? exprs : null;
}
if (path.isSequenceExpression()) {
const exprs = path
.get("expressions")
.map(p => collectExpressions(p, isExprTypeValid));
if (exprs.some(e => e === null)) {
return null;
} else {
return exprs.reduce((s, n) => s.concat(n), []); // === Array.flatten
}
}
if (isExprTypeValid(path)) {
return [path];
}
return null;
}
function getContiguousStatementsAndExpressions(
body,
start,
end,
isExprTypeValid,
checkExpr
) {
const statements = [];
let allExprs = [];
for (let i = start; i < end; i++) {
const exprs = collectExpressions(body[i], isExprTypeValid);
if (exprs === null || !exprs.every(e => checkExpr(e))) {
break;
}
statements.push(body[i]);
allExprs = allExprs.concat(exprs);
}
return [statements, allExprs];
}
function getReferenceChecker(references) {
// returns a function s.t. given an expr, returns true iff expr is an ancestor of a reference
return expr => references.some(r => r === expr || r.isDescendant(expr));
}
function tryUseCollapser(t, collapser, varDecl, topLevel, checkReference) {
// Returns true iff successfully used the collapser. Otherwise returns undefined.
const [name, init, startIndex] = topLevel;
const body = varDecl.parentPath.get("body");
if (!collapser.isInitTypeValid(init)) {
return;
}
const [statements, exprs] = getContiguousStatementsAndExpressions(
body,
startIndex + 1,
body.length,
collapser.isExpressionTypeValid,
collapser.getExpressionChecker(name, checkReference)
);
if (statements.length === 0) {
return;
}
const assignments = exprs.map(e => collapser.extractAssignment(e));
const oldInit = init.node;
const newInit = t.cloneDeep(oldInit);
if (
!assignments.every(assignment =>
collapser.addSuccessfully(t, assignment, newInit)
)
) {
return;
}
// some collapses may increase the size
if (
!collapser.isSizeSmaller({
newInit,
oldInit,
varDecl,
assignments,
statements
})
) {
return;
}
init.replaceWith(newInit);
statements.forEach(s => s.remove());
return true;
}
module.exports = function({ types: t }) {
return {
name: "transform-inline-consecutive-adds",
visitor: {
VariableDeclaration(varDecl) {
const topLevel = validateTopLevel(varDecl);
if (!topLevel) {
return;
}
const [name] = topLevel;
const references = getIdAndFunctionReferences(name, varDecl.parentPath);
if (references === false) {
return;
}
const checkReference = getReferenceChecker(references);
if (
COLLAPSERS.some(c =>
tryUseCollapser(t, c, varDecl, topLevel, checkReference)
)
) {
return;
}
}
}
};
};