evaluation.js
10.2 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"use strict";
exports.__esModule = true;
var _typeof2 = require("babel-runtime/helpers/typeof");
var _typeof3 = _interopRequireDefault(_typeof2);
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _map = require("babel-runtime/core-js/map");
var _map2 = _interopRequireDefault(_map);
exports.evaluateTruthy = evaluateTruthy;
exports.evaluate = evaluate;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var VALID_CALLEES = ["String", "Number", "Math"];
var INVALID_METHODS = ["random"];
function evaluateTruthy() {
var res = this.evaluate();
if (res.confident) return !!res.value;
}
function evaluate() {
var confident = true;
var deoptPath = void 0;
var seen = new _map2.default();
function deopt(path) {
if (!confident) return;
deoptPath = path;
confident = false;
}
var value = evaluate(this);
if (!confident) value = undefined;
return {
confident: confident,
deopt: deoptPath,
value: value
};
function evaluate(path) {
var node = path.node;
if (seen.has(node)) {
var existing = seen.get(node);
if (existing.resolved) {
return existing.value;
} else {
deopt(path);
return;
}
} else {
var item = { resolved: false };
seen.set(node, item);
var val = _evaluate(path);
if (confident) {
item.resolved = true;
item.value = val;
}
return val;
}
}
function _evaluate(path) {
if (!confident) return;
var node = path.node;
if (path.isSequenceExpression()) {
var exprs = path.get("expressions");
return evaluate(exprs[exprs.length - 1]);
}
if (path.isStringLiteral() || path.isNumericLiteral() || path.isBooleanLiteral()) {
return node.value;
}
if (path.isNullLiteral()) {
return null;
}
if (path.isTemplateLiteral()) {
var str = "";
var i = 0;
var _exprs = path.get("expressions");
for (var _iterator = node.quasis, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var elem = _ref;
if (!confident) break;
str += elem.value.cooked;
var expr = _exprs[i++];
if (expr) str += String(evaluate(expr));
}
if (!confident) return;
return str;
}
if (path.isConditionalExpression()) {
var testResult = evaluate(path.get("test"));
if (!confident) return;
if (testResult) {
return evaluate(path.get("consequent"));
} else {
return evaluate(path.get("alternate"));
}
}
if (path.isExpressionWrapper()) {
return evaluate(path.get("expression"));
}
if (path.isMemberExpression() && !path.parentPath.isCallExpression({ callee: node })) {
var property = path.get("property");
var object = path.get("object");
if (object.isLiteral() && property.isIdentifier()) {
var _value = object.node.value;
var type = typeof _value === "undefined" ? "undefined" : (0, _typeof3.default)(_value);
if (type === "number" || type === "string") {
return _value[property.node.name];
}
}
}
if (path.isReferencedIdentifier()) {
var binding = path.scope.getBinding(node.name);
if (binding && binding.constantViolations.length > 0) {
return deopt(binding.path);
}
if (binding && path.node.start < binding.path.node.end) {
return deopt(binding.path);
}
if (binding && binding.hasValue) {
return binding.value;
} else {
if (node.name === "undefined") {
return binding ? deopt(binding.path) : undefined;
} else if (node.name === "Infinity") {
return binding ? deopt(binding.path) : Infinity;
} else if (node.name === "NaN") {
return binding ? deopt(binding.path) : NaN;
}
var resolved = path.resolve();
if (resolved === path) {
return deopt(path);
} else {
return evaluate(resolved);
}
}
}
if (path.isUnaryExpression({ prefix: true })) {
if (node.operator === "void") {
return undefined;
}
var argument = path.get("argument");
if (node.operator === "typeof" && (argument.isFunction() || argument.isClass())) {
return "function";
}
var arg = evaluate(argument);
if (!confident) return;
switch (node.operator) {
case "!":
return !arg;
case "+":
return +arg;
case "-":
return -arg;
case "~":
return ~arg;
case "typeof":
return typeof arg === "undefined" ? "undefined" : (0, _typeof3.default)(arg);
}
}
if (path.isArrayExpression()) {
var arr = [];
var elems = path.get("elements");
for (var _iterator2 = elems, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
var _ref2;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
var _elem = _ref2;
_elem = _elem.evaluate();
if (_elem.confident) {
arr.push(_elem.value);
} else {
return deopt(_elem);
}
}
return arr;
}
if (path.isObjectExpression()) {
var obj = {};
var props = path.get("properties");
for (var _iterator3 = props, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
var _ref3;
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
var prop = _ref3;
if (prop.isObjectMethod() || prop.isSpreadProperty()) {
return deopt(prop);
}
var keyPath = prop.get("key");
var key = keyPath;
if (prop.node.computed) {
key = key.evaluate();
if (!key.confident) {
return deopt(keyPath);
}
key = key.value;
} else if (key.isIdentifier()) {
key = key.node.name;
} else {
key = key.node.value;
}
var valuePath = prop.get("value");
var _value2 = valuePath.evaluate();
if (!_value2.confident) {
return deopt(valuePath);
}
_value2 = _value2.value;
obj[key] = _value2;
}
return obj;
}
if (path.isLogicalExpression()) {
var wasConfident = confident;
var left = evaluate(path.get("left"));
var leftConfident = confident;
confident = wasConfident;
var right = evaluate(path.get("right"));
var rightConfident = confident;
confident = leftConfident && rightConfident;
switch (node.operator) {
case "||":
if (left && leftConfident) {
confident = true;
return left;
}
if (!confident) return;
return left || right;
case "&&":
if (!left && leftConfident || !right && rightConfident) {
confident = true;
}
if (!confident) return;
return left && right;
}
}
if (path.isBinaryExpression()) {
var _left = evaluate(path.get("left"));
if (!confident) return;
var _right = evaluate(path.get("right"));
if (!confident) return;
switch (node.operator) {
case "-":
return _left - _right;
case "+":
return _left + _right;
case "/":
return _left / _right;
case "*":
return _left * _right;
case "%":
return _left % _right;
case "**":
return Math.pow(_left, _right);
case "<":
return _left < _right;
case ">":
return _left > _right;
case "<=":
return _left <= _right;
case ">=":
return _left >= _right;
case "==":
return _left == _right;
case "!=":
return _left != _right;
case "===":
return _left === _right;
case "!==":
return _left !== _right;
case "|":
return _left | _right;
case "&":
return _left & _right;
case "^":
return _left ^ _right;
case "<<":
return _left << _right;
case ">>":
return _left >> _right;
case ">>>":
return _left >>> _right;
}
}
if (path.isCallExpression()) {
var callee = path.get("callee");
var context = void 0;
var func = void 0;
if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name, true) && VALID_CALLEES.indexOf(callee.node.name) >= 0) {
func = global[node.callee.name];
}
if (callee.isMemberExpression()) {
var _object = callee.get("object");
var _property = callee.get("property");
if (_object.isIdentifier() && _property.isIdentifier() && VALID_CALLEES.indexOf(_object.node.name) >= 0 && INVALID_METHODS.indexOf(_property.node.name) < 0) {
context = global[_object.node.name];
func = context[_property.node.name];
}
if (_object.isLiteral() && _property.isIdentifier()) {
var _type = (0, _typeof3.default)(_object.node.value);
if (_type === "string" || _type === "number") {
context = _object.node.value;
func = context[_property.node.name];
}
}
}
if (func) {
var args = path.get("arguments").map(evaluate);
if (!confident) return;
return func.apply(context, args);
}
}
deopt(path);
}
}