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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
"use strict";
const REPLACED = Symbol("replaced");
const h = require("./helpers");
module.exports = t => {
function mergeNestedIfs(path) {
const consequent = path.get("consequent");
const alternate = path.get("alternate"); // not nested if
if (!consequent.isIfStatement()) return; // there are no alternate nodes in both the if statements (nested)
if (alternate.node || consequent.get("alternate").node) return;
const test = path.get("test");
test.replaceWith(t.logicalExpression("&&", test.node, consequent.get("test").node));
consequent.replaceWith(t.clone(consequent.get("consequent").node));
} // No alternate, make into a guarded expression
function toGuardedExpression(path) {
const node = path.node;
if (node.consequent && !node.alternate && node.consequent.type === "ExpressionStatement") {
let op = "&&";
if (t.isUnaryExpression(node.test, {
operator: "!"
})) {
node.test = node.test.argument;
op = "||";
}
path.replaceWith(t.expressionStatement(t.logicalExpression(op, node.test, node.consequent.expression)));
return REPLACED;
}
} // both consequent and alternate are expressions, turn into ternary
function toTernary(path) {
const node = path.node;
if (t.isExpressionStatement(node.consequent) && t.isExpressionStatement(node.alternate)) {
path.replaceWith(t.conditionalExpression(node.test, node.consequent.expression, node.alternate.expression));
return REPLACED;
}
} // consequent and alternate are return -- conditional.
function toConditional(path) {
const node = path.node;
if (t.isReturnStatement(node.consequent) && t.isReturnStatement(node.alternate)) {
if (!node.consequent.argument && !node.alternate.argument) {
path.replaceWith(t.expressionStatement(node.test));
return REPLACED;
}
path.replaceWith(t.returnStatement(t.conditionalExpression(node.test, node.consequent.argument || h.VOID_0(t), node.alternate.argument || h.VOID_0(t))));
return REPLACED;
}
} // There is nothing after this If block. And one or both
// of the consequent and alternate are either expression statment
// or return statements.
function toReturn(path) {
const node = path.node;
if (!path.getSibling(path.key + 1).node && path.parentPath && path.parentPath.parentPath && path.parentPath.parentPath.isFunction()) {
// Only the consequent is a return, void the alternate.
if (t.isReturnStatement(node.consequent) && t.isExpressionStatement(node.alternate)) {
if (!node.consequent.argument) {
path.replaceWith(t.expressionStatement(t.logicalExpression("||", node.test, node.alternate.expression)));
return REPLACED;
}
path.replaceWith(t.returnStatement(t.conditionalExpression(node.test, node.consequent.argument || h.VOID_0(t), t.unaryExpression("void", node.alternate.expression, true))));
return REPLACED;
} // Only the alternate is a return, void the consequent.
if (t.isReturnStatement(node.alternate) && t.isExpressionStatement(node.consequent)) {
if (!node.alternate.argument) {
path.replaceWith(t.expressionStatement(t.logicalExpression("&&", node.test, node.consequent.expression)));
return REPLACED;
}
path.replaceWith(t.returnStatement(t.conditionalExpression(node.test, t.unaryExpression("void", node.consequent.expression, true), node.alternate.argument || h.VOID_0(t))));
return REPLACED;
}
if (t.isReturnStatement(node.consequent) && !node.alternate) {
if (!node.consequent.argument) {
path.replaceWith(t.expressionStatement(node.test));
return REPLACED;
} // This would only be worth it if the previous statement was an if
// because then we may merge to create a conditional.
if (path.getSibling(path.key - 1).isIfStatement()) {
path.replaceWith(t.returnStatement(t.conditionalExpression(node.test, node.consequent.argument || h.VOID_0(t), h.VOID_0(t))));
return REPLACED;
}
}
if (t.isReturnStatement(node.alternate) && !node.consequent) {
if (!node.alternate.argument) {
path.replaceWith(t.expressionStatement(node.test));
return REPLACED;
} // Same as above.
if (path.getSibling(path.key - 1).isIfStatement()) {
path.replaceWith(t.returnStatement(t.conditionalExpression(node.test, node.alternate.argument || h.VOID_0(t), h.VOID_0(t))));
return REPLACED;
}
}
}
let next = path.getSibling(path.key + 1); // If the next satatement(s) is an if statement and we can simplify that
// to potentailly be an expression (or a return) then this will make it
// easier merge.
if (next.isIfStatement()) {
next.pushContext(path.context);
next.visit();
next.popContext();
next = path.getSibling(path.key + 1);
} // Some other visitor might have deleted our node. OUR NODE ;_;
if (!path.node) {
return;
} // No alternate but the next statement is a return
// also turn into a return conditional
if (t.isReturnStatement(node.consequent) && !node.alternate && next.isReturnStatement()) {
const nextArg = next.node.argument || h.VOID_0(t);
next.remove();
path.replaceWith(t.returnStatement(t.conditionalExpression(node.test, node.consequent.argument || h.VOID_0(t), nextArg)));
return REPLACED;
} // Next is the last expression, turn into a return while void'ing the exprs
if (path.parentPath && path.parentPath.parentPath && path.parentPath.parentPath.isFunction() && !path.getSibling(path.key + 2).node && t.isReturnStatement(node.consequent) && !node.alternate && next.isExpressionStatement()) {
const nextExpr = next.node.expression;
next.remove();
if (node.consequent.argument) {
path.replaceWith(t.returnStatement(t.conditionalExpression(node.test, node.consequent.argument, t.unaryExpression("void", nextExpr, true))));
return REPLACED;
}
path.replaceWith(t.logicalExpression("||", node.test, nextExpr));
return REPLACED;
}
} // Remove else for if-return
function removeUnnecessaryElse(path) {
const node = path.node;
const consequent = path.get("consequent");
const alternate = path.get("alternate");
if (consequent.node && alternate.node && (consequent.isReturnStatement() || consequent.isBlockStatement() && t.isReturnStatement(consequent.node.body[consequent.node.body.length - 1])) && ( // don't hoist declarations
// TODO: validate declarations after fixing scope issues
alternate.isBlockStatement() ? !alternate.get("body").some(stmt => stmt.isVariableDeclaration({
kind: "let"
}) || stmt.isVariableDeclaration({
kind: "const"
})) : true)) {
path.insertAfter(alternate.isBlockStatement() ? alternate.node.body.map(el => t.clone(el)) : t.clone(alternate.node));
node.alternate = null;
return REPLACED;
}
}
function runTransforms(path) {
// ordered
const transforms = [toGuardedExpression, toTernary, toConditional, toReturn, removeUnnecessaryElse]; // run each of the replacement till we replace something
// which is identified by the Symbol(REPLACED) that each of the
// functions return when they replace something
for (var _i = 0; _i < transforms.length; _i++) {
const transform = transforms[_i];
if (transform(path) === REPLACED) {
break;
}
}
} // If the consequent is if and the altenrate is not then
// switch them out. That way we know we don't have to print
// a block.x
function switchConsequent(path) {
const node = path.node;
if (!node.alternate) {
return;
}
if (!t.isIfStatement(node.consequent)) {
return;
}
if (t.isIfStatement(node.alternate)) {
return;
}
node.test = t.unaryExpression("!", node.test, true);
var _ref = [node.consequent, node.alternate];
node.alternate = _ref[0];
node.consequent = _ref[1];
} // Make if statements with conditional returns in the body into
// an if statement that guards the rest of the block.
function conditionalReturnToGuards(path) {
const node = path.node;
if (!path.inList || !path.get("consequent").isBlockStatement() || node.alternate) {
return;
}
let ret;
let test;
const exprs = [];
const statements = node.consequent.body;
for (let i = 0, statement; statement = statements[i]; i++) {
if (t.isExpressionStatement(statement)) {
exprs.push(statement.expression);
} else if (t.isIfStatement(statement)) {
if (i < statements.length - 1) {
// This isn't the last statement. Bail.
return;
}
if (statement.alternate) {
return;
}
if (!t.isReturnStatement(statement.consequent)) {
return;
}
ret = statement.consequent;
test = statement.test;
} else {
return;
}
}
if (!test || !ret) {
return;
}
exprs.push(test);
const expr = exprs.length === 1 ? exprs[0] : t.sequenceExpression(exprs);
const replacement = t.logicalExpression("&&", node.test, expr);
path.replaceWith(t.ifStatement(replacement, ret, null));
}
return {
mergeNestedIfs,
simplify: runTransforms,
switchConsequent,
conditionalReturnToGuards
};
};
|