-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmovedDeclarations.ts
239 lines (199 loc) · 7.49 KB
/
movedDeclarations.ts
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
import { NodePath } from "@babel/traverse";
import { Order } from "../../order";
import { PluginArg, PluginObject } from "../plugin";
import { NodeSymbol, PREDICTABLE } from "../../constants";
import * as t from "@babel/types";
import { isStaticValue } from "../../utils/static-utils";
import {
getPatternIdentifierNames,
isStrictMode,
prepend,
} from "../../utils/ast-utils";
import Template from "../../templates/template";
/**
* Moved Declarations moves variables in two ways:
*
* 1) Move variables to top of the current block
* 2) Move variables as unused function parameters
*/
export default ({ Plugin }: PluginArg): PluginObject => {
const me = Plugin(Order.MovedDeclarations, {
changeData: {
variableDeclarations: 0,
functionParameters: 0,
},
});
function isFunctionEligibleForParameterPacking(
functionPath: NodePath<t.Function>,
proposedParameterName: string
) {
// Getter/setter functions must have zero or one formal parameter
// We cannot add extra parameters to them
if (functionPath.isObjectMethod() || functionPath.isClassMethod()) {
if (functionPath.node.kind !== "method") {
return false;
}
}
// Rest params check
if (functionPath.get("params").find((p) => p.isRestElement())) return false;
// Max 1,000 parameters
if (functionPath.get("params").length > 1_000) return false;
// Check for duplicate parameter names
var bindingIdentifiers = getPatternIdentifierNames(
functionPath.get("params")
);
// Duplicate parameter name not allowed
if (bindingIdentifiers.has(proposedParameterName)) return false;
return true;
}
return {
visitor: {
FunctionDeclaration: {
exit(path) {
var functionPath = path.findParent((path) =>
path.isFunction()
) as NodePath<t.Function>;
if (!functionPath || !(functionPath.node as NodeSymbol)[PREDICTABLE])
return;
// Must be direct child of the function
if (path.parentPath !== functionPath.get("body")) return;
const functionName = path.node.id.name;
// Must be eligible for parameter packing
if (
!isFunctionEligibleForParameterPacking(functionPath, functionName)
)
return;
var strictMode = isStrictMode(functionPath);
// Default parameters are not allowed when 'use strict' is declared
if (strictMode) return;
var functionExpression = path.node as t.Node as t.FunctionExpression;
functionExpression.type = "FunctionExpression";
functionExpression.id = null;
var identifier = t.identifier(functionName);
functionPath.node.params.push(identifier);
var paramPath = functionPath.get("params").at(-1);
// Update binding to point to new path
const binding = functionPath.scope.getBinding(functionName);
if (binding) {
binding.kind = "param";
binding.path = paramPath;
binding.identifier = identifier;
}
prepend(
functionPath,
new Template(`
if(!${functionName}) {
${functionName} = {functionExpression};
}
`).single({ functionExpression: functionExpression })
);
path.remove();
me.changeData.functionParameters++;
},
},
VariableDeclaration: {
exit(path) {
if (path.node.kind !== "var") return;
if (path.node.declarations.length !== 1) return;
var insertionMethod = "variableDeclaration";
var functionPath = path.findParent((path) =>
path.isFunction()
) as NodePath<t.Function>;
const declaration = path.node.declarations[0];
if (!t.isIdentifier(declaration.id)) return;
const varName = declaration.id.name;
var allowDefaultParamValue = true;
if (functionPath && (functionPath.node as NodeSymbol)[PREDICTABLE]) {
// Check for "use strict" directive
// Strict mode disallows non-simple parameters
// So we can't move the declaration to the function parameters
var strictMode = isStrictMode(functionPath);
if (strictMode) {
allowDefaultParamValue = false;
}
// Cannot add variables after rest element
// Cannot add over 1,000 parameters
if (isFunctionEligibleForParameterPacking(functionPath, varName)) {
insertionMethod = "functionParameter";
}
}
const { name } = declaration.id;
const value = declaration.init || t.identifier("undefined");
const isStatic = isStaticValue(value);
let isDefinedAtTop = false;
const parentPath = path.parentPath;
if (parentPath.isBlock()) {
isDefinedAtTop = parentPath.get("body").indexOf(path) === 0;
}
// Already at the top - nothing will change
if (insertionMethod === "variableDeclaration" && isDefinedAtTop) {
return;
}
let defaultParamValue: t.Expression;
if (
insertionMethod === "functionParameter" &&
isStatic &&
isDefinedAtTop &&
allowDefaultParamValue
) {
defaultParamValue = value;
path.remove();
} else {
// For-in / For-of can only reference the variable name
if (
parentPath.isForInStatement() ||
parentPath.isForOfStatement()
) {
path.replaceWith(t.identifier(name));
} else {
path.replaceWith(
t.assignmentExpression(
"=",
t.identifier(name),
declaration.init || t.identifier("undefined")
)
);
}
}
switch (insertionMethod) {
case "functionParameter":
var identifier = t.identifier(name);
var param: t.Pattern | t.Identifier = identifier;
if (allowDefaultParamValue && defaultParamValue) {
param = t.assignmentPattern(param, defaultParamValue);
}
functionPath.node.params.push(param);
var paramPath = functionPath.get("params").at(-1);
// Update binding to point to new path
const binding = functionPath.scope.getBinding(name);
if (binding) {
binding.kind = "param";
binding.path = paramPath;
binding.identifier = identifier;
}
me.changeData.functionParameters++;
break;
case "variableDeclaration":
var block = path.findParent((path) =>
path.isBlock()
) as NodePath<t.Block>;
var topNode = block.node.body[0];
const variableDeclarator = t.variableDeclarator(
t.identifier(name)
);
if (t.isVariableDeclaration(topNode) && topNode.kind === "var") {
topNode.declarations.push(variableDeclarator);
break;
} else {
block.node.body.unshift(
t.variableDeclaration("var", [variableDeclarator])
);
}
me.changeData.variableDeclarations++;
break;
}
},
},
},
};
};