@babel/traverse 7.0.0-beta.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of @babel/traverse might be problematic. Click here for more details.

@@ -0,0 +1,450 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.evaluateTruthy = evaluateTruthy;
5
+ exports.evaluate = evaluate;
6
+ var VALID_CALLEES = ["String", "Number", "Math"];
7
+ var INVALID_METHODS = ["random"];
8
+
9
+ function evaluateTruthy() {
10
+ var res = this.evaluate();
11
+ if (res.confident) return !!res.value;
12
+ }
13
+
14
+ function deopt(path, state) {
15
+ if (!state.confident) return;
16
+ state.deoptPath = path;
17
+ state.confident = false;
18
+ }
19
+
20
+ function evaluateCached(path, state) {
21
+ var node = path.node;
22
+ var seen = state.seen;
23
+
24
+ if (seen.has(node)) {
25
+ var existing = seen.get(node);
26
+
27
+ if (existing.resolved) {
28
+ return existing.value;
29
+ } else {
30
+ deopt(path, state);
31
+ return;
32
+ }
33
+ } else {
34
+ var item = {
35
+ resolved: false
36
+ };
37
+ seen.set(node, item);
38
+
39
+ var val = _evaluate(path, state);
40
+
41
+ if (state.confident) {
42
+ item.resolved = true;
43
+ item.value = val;
44
+ }
45
+
46
+ return val;
47
+ }
48
+ }
49
+
50
+ function _evaluate(path, state) {
51
+ if (!state.confident) return;
52
+ var node = path.node;
53
+
54
+ if (path.isSequenceExpression()) {
55
+ var exprs = path.get("expressions");
56
+ return evaluateCached(exprs[exprs.length - 1], state);
57
+ }
58
+
59
+ if (path.isStringLiteral() || path.isNumericLiteral() || path.isBooleanLiteral()) {
60
+ return node.value;
61
+ }
62
+
63
+ if (path.isNullLiteral()) {
64
+ return null;
65
+ }
66
+
67
+ if (path.isTemplateLiteral()) {
68
+ return evaluateQuasis(path, node.quasis, state);
69
+ }
70
+
71
+ if (path.isTaggedTemplateExpression() && path.get("tag").isMemberExpression()) {
72
+ var object = path.get("tag.object");
73
+ var name = object.node.name;
74
+ var property = path.get("tag.property");
75
+
76
+ if (object.isIdentifier() && name === "String" && !path.scope.getBinding(name, true) && property.isIdentifier && property.node.name === "raw") {
77
+ return evaluateQuasis(path, node.quasi.quasis, state, true);
78
+ }
79
+ }
80
+
81
+ if (path.isConditionalExpression()) {
82
+ var testResult = evaluateCached(path.get("test"), state);
83
+ if (!state.confident) return;
84
+
85
+ if (testResult) {
86
+ return evaluateCached(path.get("consequent"), state);
87
+ } else {
88
+ return evaluateCached(path.get("alternate"), state);
89
+ }
90
+ }
91
+
92
+ if (path.isExpressionWrapper()) {
93
+ return evaluateCached(path.get("expression"), state);
94
+ }
95
+
96
+ if (path.isMemberExpression() && !path.parentPath.isCallExpression({
97
+ callee: node
98
+ })) {
99
+ var _property = path.get("property");
100
+
101
+ var _object = path.get("object");
102
+
103
+ if (_object.isLiteral() && _property.isIdentifier()) {
104
+ var value = _object.node.value;
105
+ var type = typeof value;
106
+
107
+ if (type === "number" || type === "string") {
108
+ return value[_property.node.name];
109
+ }
110
+ }
111
+ }
112
+
113
+ if (path.isReferencedIdentifier()) {
114
+ var binding = path.scope.getBinding(node.name);
115
+
116
+ if (binding && binding.constantViolations.length > 0) {
117
+ return deopt(binding.path, state);
118
+ }
119
+
120
+ if (binding && path.node.start < binding.path.node.end) {
121
+ return deopt(binding.path, state);
122
+ }
123
+
124
+ if (binding && binding.hasValue) {
125
+ return binding.value;
126
+ } else {
127
+ if (node.name === "undefined") {
128
+ return binding ? deopt(binding.path, state) : undefined;
129
+ } else if (node.name === "Infinity") {
130
+ return binding ? deopt(binding.path, state) : Infinity;
131
+ } else if (node.name === "NaN") {
132
+ return binding ? deopt(binding.path, state) : NaN;
133
+ }
134
+
135
+ var resolved = path.resolve();
136
+
137
+ if (resolved === path) {
138
+ return deopt(path, state);
139
+ } else {
140
+ return evaluateCached(resolved, state);
141
+ }
142
+ }
143
+ }
144
+
145
+ if (path.isUnaryExpression({
146
+ prefix: true
147
+ })) {
148
+ if (node.operator === "void") {
149
+ return undefined;
150
+ }
151
+
152
+ var argument = path.get("argument");
153
+
154
+ if (node.operator === "typeof" && (argument.isFunction() || argument.isClass())) {
155
+ return "function";
156
+ }
157
+
158
+ var arg = evaluateCached(argument, state);
159
+ if (!state.confident) return;
160
+
161
+ switch (node.operator) {
162
+ case "!":
163
+ return !arg;
164
+
165
+ case "+":
166
+ return +arg;
167
+
168
+ case "-":
169
+ return -arg;
170
+
171
+ case "~":
172
+ return ~arg;
173
+
174
+ case "typeof":
175
+ return typeof arg;
176
+ }
177
+ }
178
+
179
+ if (path.isArrayExpression()) {
180
+ var arr = [];
181
+ var elems = path.get("elements");
182
+
183
+ for (var _iterator = elems, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
184
+ var _ref;
185
+
186
+ if (_isArray) {
187
+ if (_i >= _iterator.length) break;
188
+ _ref = _iterator[_i++];
189
+ } else {
190
+ _i = _iterator.next();
191
+ if (_i.done) break;
192
+ _ref = _i.value;
193
+ }
194
+
195
+ var _elem = _ref;
196
+ _elem = _elem.evaluate();
197
+
198
+ if (_elem.confident) {
199
+ arr.push(_elem.value);
200
+ } else {
201
+ return deopt(_elem, state);
202
+ }
203
+ }
204
+
205
+ return arr;
206
+ }
207
+
208
+ if (path.isObjectExpression()) {
209
+ var obj = {};
210
+ var props = path.get("properties");
211
+
212
+ for (var _iterator2 = props, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
213
+ var _ref2;
214
+
215
+ if (_isArray2) {
216
+ if (_i2 >= _iterator2.length) break;
217
+ _ref2 = _iterator2[_i2++];
218
+ } else {
219
+ _i2 = _iterator2.next();
220
+ if (_i2.done) break;
221
+ _ref2 = _i2.value;
222
+ }
223
+
224
+ var _prop = _ref2;
225
+
226
+ if (_prop.isObjectMethod() || _prop.isSpreadElement()) {
227
+ return deopt(_prop, state);
228
+ }
229
+
230
+ var keyPath = _prop.get("key");
231
+
232
+ var key = keyPath;
233
+
234
+ if (_prop.node.computed) {
235
+ key = key.evaluate();
236
+
237
+ if (!key.confident) {
238
+ return deopt(keyPath, state);
239
+ }
240
+
241
+ key = key.value;
242
+ } else if (key.isIdentifier()) {
243
+ key = key.node.name;
244
+ } else {
245
+ key = key.node.value;
246
+ }
247
+
248
+ var valuePath = _prop.get("value");
249
+
250
+ var _value2 = valuePath.evaluate();
251
+
252
+ if (!_value2.confident) {
253
+ return deopt(valuePath, state);
254
+ }
255
+
256
+ _value2 = _value2.value;
257
+ obj[key] = _value2;
258
+ }
259
+
260
+ return obj;
261
+ }
262
+
263
+ if (path.isLogicalExpression()) {
264
+ var wasConfident = state.confident;
265
+ var left = evaluateCached(path.get("left"), state);
266
+ var leftConfident = state.confident;
267
+ state.confident = wasConfident;
268
+ var right = evaluateCached(path.get("right"), state);
269
+ var rightConfident = state.confident;
270
+ state.confident = leftConfident && rightConfident;
271
+
272
+ switch (node.operator) {
273
+ case "||":
274
+ if (left && leftConfident) {
275
+ state.confident = true;
276
+ return left;
277
+ }
278
+
279
+ if (!state.confident) return;
280
+ return left || right;
281
+
282
+ case "&&":
283
+ if (!left && leftConfident || !right && rightConfident) {
284
+ state.confident = true;
285
+ }
286
+
287
+ if (!state.confident) return;
288
+ return left && right;
289
+ }
290
+ }
291
+
292
+ if (path.isBinaryExpression()) {
293
+ var _left = evaluateCached(path.get("left"), state);
294
+
295
+ if (!state.confident) return;
296
+
297
+ var _right = evaluateCached(path.get("right"), state);
298
+
299
+ if (!state.confident) return;
300
+
301
+ switch (node.operator) {
302
+ case "-":
303
+ return _left - _right;
304
+
305
+ case "+":
306
+ return _left + _right;
307
+
308
+ case "/":
309
+ return _left / _right;
310
+
311
+ case "*":
312
+ return _left * _right;
313
+
314
+ case "%":
315
+ return _left % _right;
316
+
317
+ case "**":
318
+ return Math.pow(_left, _right);
319
+
320
+ case "<":
321
+ return _left < _right;
322
+
323
+ case ">":
324
+ return _left > _right;
325
+
326
+ case "<=":
327
+ return _left <= _right;
328
+
329
+ case ">=":
330
+ return _left >= _right;
331
+
332
+ case "==":
333
+ return _left == _right;
334
+
335
+ case "!=":
336
+ return _left != _right;
337
+
338
+ case "===":
339
+ return _left === _right;
340
+
341
+ case "!==":
342
+ return _left !== _right;
343
+
344
+ case "|":
345
+ return _left | _right;
346
+
347
+ case "&":
348
+ return _left & _right;
349
+
350
+ case "^":
351
+ return _left ^ _right;
352
+
353
+ case "<<":
354
+ return _left << _right;
355
+
356
+ case ">>":
357
+ return _left >> _right;
358
+
359
+ case ">>>":
360
+ return _left >>> _right;
361
+ }
362
+ }
363
+
364
+ if (path.isCallExpression()) {
365
+ var callee = path.get("callee");
366
+ var context;
367
+ var func;
368
+
369
+ if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name, true) && VALID_CALLEES.indexOf(callee.node.name) >= 0) {
370
+ func = global[node.callee.name];
371
+ }
372
+
373
+ if (callee.isMemberExpression()) {
374
+ var _object2 = callee.get("object");
375
+
376
+ var _property2 = callee.get("property");
377
+
378
+ if (_object2.isIdentifier() && _property2.isIdentifier() && VALID_CALLEES.indexOf(_object2.node.name) >= 0 && INVALID_METHODS.indexOf(_property2.node.name) < 0) {
379
+ context = global[_object2.node.name];
380
+ func = context[_property2.node.name];
381
+ }
382
+
383
+ if (_object2.isLiteral() && _property2.isIdentifier()) {
384
+ var _type = typeof _object2.node.value;
385
+
386
+ if (_type === "string" || _type === "number") {
387
+ context = _object2.node.value;
388
+ func = context[_property2.node.name];
389
+ }
390
+ }
391
+ }
392
+
393
+ if (func) {
394
+ var args = path.get("arguments").map(function (arg) {
395
+ return evaluateCached(arg, state);
396
+ });
397
+ if (!state.confident) return;
398
+ return func.apply(context, args);
399
+ }
400
+ }
401
+
402
+ deopt(path, state);
403
+ }
404
+
405
+ function evaluateQuasis(path, quasis, state, raw) {
406
+ if (raw === void 0) {
407
+ raw = false;
408
+ }
409
+
410
+ var str = "";
411
+ var i = 0;
412
+ var exprs = path.get("expressions");
413
+
414
+ for (var _iterator3 = quasis, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
415
+ var _ref3;
416
+
417
+ if (_isArray3) {
418
+ if (_i3 >= _iterator3.length) break;
419
+ _ref3 = _iterator3[_i3++];
420
+ } else {
421
+ _i3 = _iterator3.next();
422
+ if (_i3.done) break;
423
+ _ref3 = _i3.value;
424
+ }
425
+
426
+ var _elem2 = _ref3;
427
+ if (!state.confident) break;
428
+ str += raw ? _elem2.value.raw : _elem2.value.cooked;
429
+ var expr = exprs[i++];
430
+ if (expr) str += String(evaluateCached(expr, state));
431
+ }
432
+
433
+ if (!state.confident) return;
434
+ return str;
435
+ }
436
+
437
+ function evaluate() {
438
+ var state = {
439
+ confident: true,
440
+ deoptPath: null,
441
+ seen: new Map()
442
+ };
443
+ var value = evaluateCached(this, state);
444
+ if (!state.confident) value = undefined;
445
+ return {
446
+ confident: state.confident,
447
+ deopt: state.deoptPath,
448
+ value: value
449
+ };
450
+ }
@@ -0,0 +1,244 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.getOpposite = getOpposite;
5
+ exports.getCompletionRecords = getCompletionRecords;
6
+ exports.getSibling = getSibling;
7
+ exports.getPrevSibling = getPrevSibling;
8
+ exports.getNextSibling = getNextSibling;
9
+ exports.getAllNextSiblings = getAllNextSiblings;
10
+ exports.getAllPrevSiblings = getAllPrevSiblings;
11
+ exports.get = get;
12
+ exports._getKey = _getKey;
13
+ exports._getPattern = _getPattern;
14
+ exports.getBindingIdentifiers = getBindingIdentifiers;
15
+ exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
16
+ exports.getBindingIdentifierPaths = getBindingIdentifierPaths;
17
+ exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
18
+
19
+ var _index = _interopRequireDefault(require("./index"));
20
+
21
+ var t = _interopRequireWildcard(require("@babel/types"));
22
+
23
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
24
+
25
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
+
27
+ function getOpposite() {
28
+ if (this.key === "left") {
29
+ return this.getSibling("right");
30
+ } else if (this.key === "right") {
31
+ return this.getSibling("left");
32
+ }
33
+ }
34
+
35
+ function addCompletionRecords(path, paths) {
36
+ if (path) return paths.concat(path.getCompletionRecords());
37
+ return paths;
38
+ }
39
+
40
+ function getCompletionRecords() {
41
+ var paths = [];
42
+
43
+ if (this.isIfStatement()) {
44
+ paths = addCompletionRecords(this.get("consequent"), paths);
45
+ paths = addCompletionRecords(this.get("alternate"), paths);
46
+ } else if (this.isDoExpression() || this.isFor() || this.isWhile()) {
47
+ paths = addCompletionRecords(this.get("body"), paths);
48
+ } else if (this.isProgram() || this.isBlockStatement()) {
49
+ paths = addCompletionRecords(this.get("body").pop(), paths);
50
+ } else if (this.isFunction()) {
51
+ return this.get("body").getCompletionRecords();
52
+ } else if (this.isTryStatement()) {
53
+ paths = addCompletionRecords(this.get("block"), paths);
54
+ paths = addCompletionRecords(this.get("handler"), paths);
55
+ paths = addCompletionRecords(this.get("finalizer"), paths);
56
+ } else if (this.isCatchClause()) {
57
+ paths = addCompletionRecords(this.get("body"), paths);
58
+ } else {
59
+ paths.push(this);
60
+ }
61
+
62
+ return paths;
63
+ }
64
+
65
+ function getSibling(key) {
66
+ return _index.default.get({
67
+ parentPath: this.parentPath,
68
+ parent: this.parent,
69
+ container: this.container,
70
+ listKey: this.listKey,
71
+ key: key
72
+ });
73
+ }
74
+
75
+ function getPrevSibling() {
76
+ return this.getSibling(this.key - 1);
77
+ }
78
+
79
+ function getNextSibling() {
80
+ return this.getSibling(this.key + 1);
81
+ }
82
+
83
+ function getAllNextSiblings() {
84
+ var _key = this.key;
85
+ var sibling = this.getSibling(++_key);
86
+ var siblings = [];
87
+
88
+ while (sibling.node) {
89
+ siblings.push(sibling);
90
+ sibling = this.getSibling(++_key);
91
+ }
92
+
93
+ return siblings;
94
+ }
95
+
96
+ function getAllPrevSiblings() {
97
+ var _key = this.key;
98
+ var sibling = this.getSibling(--_key);
99
+ var siblings = [];
100
+
101
+ while (sibling.node) {
102
+ siblings.push(sibling);
103
+ sibling = this.getSibling(--_key);
104
+ }
105
+
106
+ return siblings;
107
+ }
108
+
109
+ function get(key, context) {
110
+ if (context === true) context = this.context;
111
+ var parts = key.split(".");
112
+
113
+ if (parts.length === 1) {
114
+ return this._getKey(key, context);
115
+ } else {
116
+ return this._getPattern(parts, context);
117
+ }
118
+ }
119
+
120
+ function _getKey(key, context) {
121
+ var _this = this;
122
+
123
+ var node = this.node;
124
+ var container = node[key];
125
+
126
+ if (Array.isArray(container)) {
127
+ return container.map(function (_, i) {
128
+ return _index.default.get({
129
+ listKey: key,
130
+ parentPath: _this,
131
+ parent: node,
132
+ container: container,
133
+ key: i
134
+ }).setContext(context);
135
+ });
136
+ } else {
137
+ return _index.default.get({
138
+ parentPath: this,
139
+ parent: node,
140
+ container: node,
141
+ key: key
142
+ }).setContext(context);
143
+ }
144
+ }
145
+
146
+ function _getPattern(parts, context) {
147
+ var path = this;
148
+ var _arr = parts;
149
+
150
+ for (var _i = 0; _i < _arr.length; _i++) {
151
+ var part = _arr[_i];
152
+
153
+ if (part === ".") {
154
+ path = path.parentPath;
155
+ } else {
156
+ if (Array.isArray(path)) {
157
+ path = path[part];
158
+ } else {
159
+ path = path.get(part, context);
160
+ }
161
+ }
162
+ }
163
+
164
+ return path;
165
+ }
166
+
167
+ function getBindingIdentifiers(duplicates) {
168
+ return t.getBindingIdentifiers(this.node, duplicates);
169
+ }
170
+
171
+ function getOuterBindingIdentifiers(duplicates) {
172
+ return t.getOuterBindingIdentifiers(this.node, duplicates);
173
+ }
174
+
175
+ function getBindingIdentifierPaths(duplicates, outerOnly) {
176
+ if (duplicates === void 0) {
177
+ duplicates = false;
178
+ }
179
+
180
+ if (outerOnly === void 0) {
181
+ outerOnly = false;
182
+ }
183
+
184
+ var path = this;
185
+ var search = [].concat(path);
186
+ var ids = Object.create(null);
187
+
188
+ while (search.length) {
189
+ var id = search.shift();
190
+ if (!id) continue;
191
+ if (!id.node) continue;
192
+ var keys = t.getBindingIdentifiers.keys[id.node.type];
193
+
194
+ if (id.isIdentifier()) {
195
+ if (duplicates) {
196
+ var _ids = ids[id.node.name] = ids[id.node.name] || [];
197
+
198
+ _ids.push(id);
199
+ } else {
200
+ ids[id.node.name] = id;
201
+ }
202
+
203
+ continue;
204
+ }
205
+
206
+ if (id.isExportDeclaration()) {
207
+ var declaration = id.get("declaration");
208
+
209
+ if (declaration.isDeclaration()) {
210
+ search.push(declaration);
211
+ }
212
+
213
+ continue;
214
+ }
215
+
216
+ if (outerOnly) {
217
+ if (id.isFunctionDeclaration()) {
218
+ search.push(id.get("id"));
219
+ continue;
220
+ }
221
+
222
+ if (id.isFunctionExpression()) {
223
+ continue;
224
+ }
225
+ }
226
+
227
+ if (keys) {
228
+ for (var i = 0; i < keys.length; i++) {
229
+ var key = keys[i];
230
+ var child = id.get(key);
231
+
232
+ if (Array.isArray(child) || child.node) {
233
+ search = search.concat(child);
234
+ }
235
+ }
236
+ }
237
+ }
238
+
239
+ return ids;
240
+ }
241
+
242
+ function getOuterBindingIdentifierPaths(duplicates) {
243
+ return this.getBindingIdentifierPaths(duplicates, true);
244
+ }