/
index.js
278 lines (235 loc) · 6.5 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
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
274
275
276
277
278
"use strict";
module.exports = function evaluate(path, { tdz = false } = {}) {
if (!tdz && !path.isReferencedIdentifier()) {
return baseEvaluate(path);
}
if (path.isReferencedIdentifier()) {
return evaluateIdentifier(path);
}
const state = {
confident: true
};
// prepare
path.traverse({
Scope(scopePath) {
scopePath.skip();
},
ReferencedIdentifier(idPath) {
const binding = idPath.scope.getBinding(idPath.node.name);
// don't deopt globals
// let babel take care of it
if (!binding) return;
const evalResult = evaluateIdentifier(idPath);
if (!evalResult.confident) {
state.confident = evalResult.confident;
state.deoptPath = evalResult.deoptPath;
}
}
});
if (!state.confident) {
return state;
}
return baseEvaluate(path);
};
function baseEvaluate(path) {
try {
return path.evaluate();
} catch (e) {
return {
confident: false,
error: e
};
}
}
// Original Source:
// https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/evaluation.js
// modified for Babel-minify use
function evaluateIdentifier(path) {
if (!path.isReferencedIdentifier()) {
throw new Error(`Expected ReferencedIdentifier. Got ${path.type}`);
}
const { node } = path;
const binding = path.scope.getBinding(node.name);
if (!binding) {
const { name } = node;
if (!name) {
return deopt(path);
}
switch (name) {
case "undefined":
return { confident: true, value: undefined };
case "NaN":
return { confident: true, value: NaN };
case "Infinity":
return { confident: true, value: Infinity };
default:
return deopt(path);
}
}
if (binding.constantViolations.length > 0) {
return deopt(binding.path);
}
// referenced in a different scope - deopt
if (shouldDeoptBasedOnScope(binding, path)) {
return deopt(path);
}
// let/var/const referenced before init
// or "var" referenced in an outer scope
const flowEvalResult = evaluateBasedOnControlFlow(binding, path);
if (flowEvalResult.confident) {
return flowEvalResult;
}
if (flowEvalResult.shouldDeopt) {
return deopt(path);
}
return path.evaluate();
}
// check if referenced in a different fn scope
// we can't determine if this function is called sync or async
// if the binding is in program scope
// all it's references inside a different function should be deopted
function shouldDeoptBasedOnScope(binding, refPath) {
if (binding.scope.path.isProgram() && refPath.scope !== binding.scope) {
return true;
}
return false;
}
function evaluateBasedOnControlFlow(binding, refPath) {
if (binding.kind === "var") {
// early-exit
const declaration = binding.path.parentPath;
if (declaration.parentPath) {
/**
* Handle when binding is created inside a parent block and
* the corresponding parent is removed by other plugins
* if (false) { var a } -> var a
*/
if (declaration.parentPath.removed) {
return {
confident: true,
value: void 0
};
}
if (
declaration.parentPath.isIfStatement() ||
declaration.parentPath.isLoop() ||
declaration.parentPath.isSwitchCase()
) {
return {
shouldDeopt: true
};
}
}
const fnParent = (
binding.path.scope.getFunctionParent() ||
binding.path.scope.getProgramParent()
).path;
let blockParentPath = binding.path.scope.getBlockParent().path;
let blockParent = blockParentPath.node;
if (blockParentPath === fnParent && !fnParent.isProgram()) {
blockParent = blockParent.body;
}
// detect Usage Outside Init Scope
const blockBody = blockParent.body;
if (
Array.isArray(blockBody) &&
!blockBody.some(stmt => isAncestor(stmt, refPath))
) {
return {
shouldDeopt: true
};
}
// Detect usage before init
const stmts = fnParent.isProgram()
? fnParent.node.body
: fnParent.node.body.body;
const compareResult = compareBindingAndReference({
binding,
refPath,
stmts
});
if (compareResult.reference && compareResult.binding) {
if (
compareResult.reference.scope === "current" &&
compareResult.reference.idx < compareResult.binding.idx
) {
return { confident: true, value: void 0 };
}
}
} else if (binding.kind === "let" || binding.kind === "const") {
// binding.path is the declarator
const declarator = binding.path;
const declaration = declarator.parentPath;
if (
declaration.parentPath &&
(declaration.parentPath.isIfStatement() ||
declaration.parentPath.isLoop() ||
declaration.parentPath.isSwitchCase())
) {
return { shouldDeopt: true };
}
const scopePath = declarator.scope.path;
let scopeNode = scopePath.node;
if (scopePath.isFunction() || scopePath.isCatchClause()) {
scopeNode = scopeNode.body;
}
// Detect Usage before Init
let stmts = scopeNode.body;
if (!Array.isArray(stmts)) {
stmts = [stmts];
}
const compareResult = compareBindingAndReference({
binding,
refPath,
stmts
});
if (compareResult.reference && compareResult.binding) {
if (
compareResult.reference.scope === "current" &&
compareResult.reference.idx < compareResult.binding.idx
) {
throw new Error(
`ReferenceError: Used ${refPath.node.name}: ` +
`${binding.kind} binding before declaration`
);
}
if (compareResult.reference.scope === "other") {
return { shouldDeopt: true };
}
}
}
return { confident: false, shouldDeopt: false };
}
function compareBindingAndReference({ binding, refPath, stmts }) {
const state = {
binding: null,
reference: null
};
for (const [idx, stmt] of stmts.entries()) {
if (isAncestor(stmt, binding.path)) {
state.binding = { idx };
}
for (const ref of binding.referencePaths) {
if (ref === refPath && isAncestor(stmt, ref)) {
state.reference = {
idx,
scope: binding.path.scope === ref.scope ? "current" : "other"
};
break;
}
}
}
return state;
}
function deopt(deoptPath) {
return {
confident: false,
deoptPath
};
}
/**
* is nodeParent an ancestor of path
*/
function isAncestor(nodeParent, path) {
return !!path.findParent(parent => parent.node === nodeParent);
}