@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,210 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.VariableDeclarator = VariableDeclarator;
5
+ exports.TypeCastExpression = TypeCastExpression;
6
+ exports.NewExpression = NewExpression;
7
+ exports.TemplateLiteral = TemplateLiteral;
8
+ exports.UnaryExpression = UnaryExpression;
9
+ exports.BinaryExpression = BinaryExpression;
10
+ exports.LogicalExpression = LogicalExpression;
11
+ exports.ConditionalExpression = ConditionalExpression;
12
+ exports.SequenceExpression = SequenceExpression;
13
+ exports.AssignmentExpression = AssignmentExpression;
14
+ exports.UpdateExpression = UpdateExpression;
15
+ exports.StringLiteral = StringLiteral;
16
+ exports.NumericLiteral = NumericLiteral;
17
+ exports.BooleanLiteral = BooleanLiteral;
18
+ exports.NullLiteral = NullLiteral;
19
+ exports.RegExpLiteral = RegExpLiteral;
20
+ exports.ObjectExpression = ObjectExpression;
21
+ exports.ArrayExpression = ArrayExpression;
22
+ exports.RestElement = RestElement;
23
+ exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = Func;
24
+ exports.CallExpression = CallExpression;
25
+ exports.TaggedTemplateExpression = TaggedTemplateExpression;
26
+ Object.defineProperty(exports, "Identifier", {
27
+ enumerable: true,
28
+ get: function get() {
29
+ return _infererReference.default;
30
+ }
31
+ });
32
+
33
+ var t = _interopRequireWildcard(require("@babel/types"));
34
+
35
+ var _infererReference = _interopRequireDefault(require("./inferer-reference"));
36
+
37
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
38
+
39
+ 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; } }
40
+
41
+ function VariableDeclarator() {
42
+ var id = this.get("id");
43
+ if (!id.isIdentifier()) return;
44
+ var init = this.get("init");
45
+ var type = init.getTypeAnnotation();
46
+
47
+ if (type && type.type === "AnyTypeAnnotation") {
48
+ if (init.isCallExpression() && init.get("callee").isIdentifier({
49
+ name: "Array"
50
+ }) && !init.scope.hasBinding("Array", true)) {
51
+ type = ArrayExpression();
52
+ }
53
+ }
54
+
55
+ return type;
56
+ }
57
+
58
+ function TypeCastExpression(node) {
59
+ return node.typeAnnotation;
60
+ }
61
+
62
+ TypeCastExpression.validParent = true;
63
+
64
+ function NewExpression(node) {
65
+ if (this.get("callee").isIdentifier()) {
66
+ return t.genericTypeAnnotation(node.callee);
67
+ }
68
+ }
69
+
70
+ function TemplateLiteral() {
71
+ return t.stringTypeAnnotation();
72
+ }
73
+
74
+ function UnaryExpression(node) {
75
+ var operator = node.operator;
76
+
77
+ if (operator === "void") {
78
+ return t.voidTypeAnnotation();
79
+ } else if (t.NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
80
+ return t.numberTypeAnnotation();
81
+ } else if (t.STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
82
+ return t.stringTypeAnnotation();
83
+ } else if (t.BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
84
+ return t.booleanTypeAnnotation();
85
+ }
86
+ }
87
+
88
+ function BinaryExpression(node) {
89
+ var operator = node.operator;
90
+
91
+ if (t.NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
92
+ return t.numberTypeAnnotation();
93
+ } else if (t.BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
94
+ return t.booleanTypeAnnotation();
95
+ } else if (operator === "+") {
96
+ var right = this.get("right");
97
+ var left = this.get("left");
98
+
99
+ if (left.isBaseType("number") && right.isBaseType("number")) {
100
+ return t.numberTypeAnnotation();
101
+ } else if (left.isBaseType("string") || right.isBaseType("string")) {
102
+ return t.stringTypeAnnotation();
103
+ }
104
+
105
+ return t.unionTypeAnnotation([t.stringTypeAnnotation(), t.numberTypeAnnotation()]);
106
+ }
107
+ }
108
+
109
+ function LogicalExpression() {
110
+ return t.createUnionTypeAnnotation([this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()]);
111
+ }
112
+
113
+ function ConditionalExpression() {
114
+ return t.createUnionTypeAnnotation([this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()]);
115
+ }
116
+
117
+ function SequenceExpression() {
118
+ return this.get("expressions").pop().getTypeAnnotation();
119
+ }
120
+
121
+ function AssignmentExpression() {
122
+ return this.get("right").getTypeAnnotation();
123
+ }
124
+
125
+ function UpdateExpression(node) {
126
+ var operator = node.operator;
127
+
128
+ if (operator === "++" || operator === "--") {
129
+ return t.numberTypeAnnotation();
130
+ }
131
+ }
132
+
133
+ function StringLiteral() {
134
+ return t.stringTypeAnnotation();
135
+ }
136
+
137
+ function NumericLiteral() {
138
+ return t.numberTypeAnnotation();
139
+ }
140
+
141
+ function BooleanLiteral() {
142
+ return t.booleanTypeAnnotation();
143
+ }
144
+
145
+ function NullLiteral() {
146
+ return t.nullLiteralTypeAnnotation();
147
+ }
148
+
149
+ function RegExpLiteral() {
150
+ return t.genericTypeAnnotation(t.identifier("RegExp"));
151
+ }
152
+
153
+ function ObjectExpression() {
154
+ return t.genericTypeAnnotation(t.identifier("Object"));
155
+ }
156
+
157
+ function ArrayExpression() {
158
+ return t.genericTypeAnnotation(t.identifier("Array"));
159
+ }
160
+
161
+ function RestElement() {
162
+ return ArrayExpression();
163
+ }
164
+
165
+ RestElement.validParent = true;
166
+
167
+ function Func() {
168
+ return t.genericTypeAnnotation(t.identifier("Function"));
169
+ }
170
+
171
+ var isArrayFrom = t.buildMatchMemberExpression("Array.from");
172
+ var isObjectKeys = t.buildMatchMemberExpression("Object.keys");
173
+ var isObjectValues = t.buildMatchMemberExpression("Object.values");
174
+ var isObjectEntries = t.buildMatchMemberExpression("Object.entries");
175
+
176
+ function CallExpression() {
177
+ var callee = this.node.callee;
178
+
179
+ if (isObjectKeys(callee)) {
180
+ return t.arrayTypeAnnotation(t.stringTypeAnnotation());
181
+ } else if (isArrayFrom(callee) || isObjectValues(callee)) {
182
+ return t.arrayTypeAnnotation(t.anyTypeAnnotation());
183
+ } else if (isObjectEntries(callee)) {
184
+ return t.arrayTypeAnnotation(t.tupleTypeAnnotation([t.stringTypeAnnotation(), t.anyTypeAnnotation()]));
185
+ }
186
+
187
+ return resolveCall(this.get("callee"));
188
+ }
189
+
190
+ function TaggedTemplateExpression() {
191
+ return resolveCall(this.get("tag"));
192
+ }
193
+
194
+ function resolveCall(callee) {
195
+ callee = callee.resolve();
196
+
197
+ if (callee.isFunction()) {
198
+ if (callee.is("async")) {
199
+ if (callee.is("generator")) {
200
+ return t.genericTypeAnnotation(t.identifier("AsyncIterator"));
201
+ } else {
202
+ return t.genericTypeAnnotation(t.identifier("Promise"));
203
+ }
204
+ } else {
205
+ if (callee.node.returnType) {
206
+ return callee.node.returnType;
207
+ } else {}
208
+ }
209
+ }
210
+ }
@@ -0,0 +1,361 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.matchesPattern = matchesPattern;
5
+ exports.has = has;
6
+ exports.isStatic = isStatic;
7
+ exports.isnt = isnt;
8
+ exports.equals = equals;
9
+ exports.isNodeType = isNodeType;
10
+ exports.canHaveVariableDeclarationOrExpression = canHaveVariableDeclarationOrExpression;
11
+ exports.canSwapBetweenExpressionAndStatement = canSwapBetweenExpressionAndStatement;
12
+ exports.isCompletionRecord = isCompletionRecord;
13
+ exports.isStatementOrBlock = isStatementOrBlock;
14
+ exports.referencesImport = referencesImport;
15
+ exports.getSource = getSource;
16
+ exports.willIMaybeExecuteBefore = willIMaybeExecuteBefore;
17
+ exports._guessExecutionStatusRelativeTo = _guessExecutionStatusRelativeTo;
18
+ exports._guessExecutionStatusRelativeToDifferentFunctions = _guessExecutionStatusRelativeToDifferentFunctions;
19
+ exports.resolve = resolve;
20
+ exports._resolve = _resolve;
21
+ exports.isConstantExpression = isConstantExpression;
22
+ exports.is = void 0;
23
+
24
+ var _includes = _interopRequireDefault(require("lodash/includes"));
25
+
26
+ var t = _interopRequireWildcard(require("@babel/types"));
27
+
28
+ 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; } }
29
+
30
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
+
32
+ function matchesPattern(pattern, allowPartial) {
33
+ return t.matchesPattern(this.node, pattern, allowPartial);
34
+ }
35
+
36
+ function has(key) {
37
+ var val = this.node && this.node[key];
38
+
39
+ if (val && Array.isArray(val)) {
40
+ return !!val.length;
41
+ } else {
42
+ return !!val;
43
+ }
44
+ }
45
+
46
+ function isStatic() {
47
+ return this.scope.isStatic(this.node);
48
+ }
49
+
50
+ var is = has;
51
+ exports.is = is;
52
+
53
+ function isnt(key) {
54
+ return !this.has(key);
55
+ }
56
+
57
+ function equals(key, value) {
58
+ return this.node[key] === value;
59
+ }
60
+
61
+ function isNodeType(type) {
62
+ return t.isType(this.type, type);
63
+ }
64
+
65
+ function canHaveVariableDeclarationOrExpression() {
66
+ return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
67
+ }
68
+
69
+ function canSwapBetweenExpressionAndStatement(replacement) {
70
+ if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
71
+ return false;
72
+ }
73
+
74
+ if (this.isExpression()) {
75
+ return t.isBlockStatement(replacement);
76
+ } else if (this.isBlockStatement()) {
77
+ return t.isExpression(replacement);
78
+ }
79
+
80
+ return false;
81
+ }
82
+
83
+ function isCompletionRecord(allowInsideFunction) {
84
+ var path = this;
85
+ var first = true;
86
+
87
+ do {
88
+ var container = path.container;
89
+
90
+ if (path.isFunction() && !first) {
91
+ return !!allowInsideFunction;
92
+ }
93
+
94
+ first = false;
95
+
96
+ if (Array.isArray(container) && path.key !== container.length - 1) {
97
+ return false;
98
+ }
99
+ } while ((path = path.parentPath) && !path.isProgram());
100
+
101
+ return true;
102
+ }
103
+
104
+ function isStatementOrBlock() {
105
+ if (this.parentPath.isLabeledStatement() || t.isBlockStatement(this.container)) {
106
+ return false;
107
+ } else {
108
+ return (0, _includes.default)(t.STATEMENT_OR_BLOCK_KEYS, this.key);
109
+ }
110
+ }
111
+
112
+ function referencesImport(moduleSource, importName) {
113
+ if (!this.isReferencedIdentifier()) return false;
114
+ var binding = this.scope.getBinding(this.node.name);
115
+ if (!binding || binding.kind !== "module") return false;
116
+ var path = binding.path;
117
+ var parent = path.parentPath;
118
+ if (!parent.isImportDeclaration()) return false;
119
+
120
+ if (parent.node.source.value === moduleSource) {
121
+ if (!importName) return true;
122
+ } else {
123
+ return false;
124
+ }
125
+
126
+ if (path.isImportDefaultSpecifier() && importName === "default") {
127
+ return true;
128
+ }
129
+
130
+ if (path.isImportNamespaceSpecifier() && importName === "*") {
131
+ return true;
132
+ }
133
+
134
+ if (path.isImportSpecifier() && path.node.imported.name === importName) {
135
+ return true;
136
+ }
137
+
138
+ return false;
139
+ }
140
+
141
+ function getSource() {
142
+ var node = this.node;
143
+
144
+ if (node.end) {
145
+ return this.hub.file.code.slice(node.start, node.end);
146
+ } else {
147
+ return "";
148
+ }
149
+ }
150
+
151
+ function willIMaybeExecuteBefore(target) {
152
+ return this._guessExecutionStatusRelativeTo(target) !== "after";
153
+ }
154
+
155
+ function _guessExecutionStatusRelativeTo(target) {
156
+ var targetFuncParent = target.scope.getFunctionParent() || target.scope.getProgramParent();
157
+ var selfFuncParent = this.scope.getFunctionParent() || target.scope.getProgramParent();
158
+
159
+ if (targetFuncParent.node !== selfFuncParent.node) {
160
+ var status = this._guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent);
161
+
162
+ if (status) {
163
+ return status;
164
+ } else {
165
+ target = targetFuncParent.path;
166
+ }
167
+ }
168
+
169
+ var targetPaths = target.getAncestry();
170
+ if (targetPaths.indexOf(this) >= 0) return "after";
171
+ var selfPaths = this.getAncestry();
172
+ var commonPath;
173
+ var targetIndex;
174
+ var selfIndex;
175
+
176
+ for (selfIndex = 0; selfIndex < selfPaths.length; selfIndex++) {
177
+ var selfPath = selfPaths[selfIndex];
178
+ targetIndex = targetPaths.indexOf(selfPath);
179
+
180
+ if (targetIndex >= 0) {
181
+ commonPath = selfPath;
182
+ break;
183
+ }
184
+ }
185
+
186
+ if (!commonPath) {
187
+ return "before";
188
+ }
189
+
190
+ var targetRelationship = targetPaths[targetIndex - 1];
191
+ var selfRelationship = selfPaths[selfIndex - 1];
192
+
193
+ if (!targetRelationship || !selfRelationship) {
194
+ return "before";
195
+ }
196
+
197
+ if (targetRelationship.listKey && targetRelationship.container === selfRelationship.container) {
198
+ return targetRelationship.key > selfRelationship.key ? "before" : "after";
199
+ }
200
+
201
+ var keys = t.VISITOR_KEYS[commonPath.type];
202
+ var targetKeyPosition = keys.indexOf(targetRelationship.key);
203
+ var selfKeyPosition = keys.indexOf(selfRelationship.key);
204
+ return targetKeyPosition > selfKeyPosition ? "before" : "after";
205
+ }
206
+
207
+ function _guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent) {
208
+ var targetFuncPath = targetFuncParent.path;
209
+ if (!targetFuncPath.isFunctionDeclaration()) return;
210
+ var binding = targetFuncPath.scope.getBinding(targetFuncPath.node.id.name);
211
+ if (!binding.references) return "before";
212
+ var referencePaths = binding.referencePaths;
213
+
214
+ for (var _iterator = referencePaths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
215
+ var _ref;
216
+
217
+ if (_isArray) {
218
+ if (_i >= _iterator.length) break;
219
+ _ref = _iterator[_i++];
220
+ } else {
221
+ _i = _iterator.next();
222
+ if (_i.done) break;
223
+ _ref = _i.value;
224
+ }
225
+
226
+ var _path2 = _ref;
227
+
228
+ if (_path2.key !== "callee" || !_path2.parentPath.isCallExpression()) {
229
+ return;
230
+ }
231
+ }
232
+
233
+ var allStatus;
234
+
235
+ for (var _iterator2 = referencePaths, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
236
+ var _ref2;
237
+
238
+ if (_isArray2) {
239
+ if (_i2 >= _iterator2.length) break;
240
+ _ref2 = _iterator2[_i2++];
241
+ } else {
242
+ _i2 = _iterator2.next();
243
+ if (_i2.done) break;
244
+ _ref2 = _i2.value;
245
+ }
246
+
247
+ var _path3 = _ref2;
248
+ var childOfFunction = !!_path3.find(function (path) {
249
+ return path.node === targetFuncPath.node;
250
+ });
251
+ if (childOfFunction) continue;
252
+
253
+ var status = this._guessExecutionStatusRelativeTo(_path3);
254
+
255
+ if (allStatus) {
256
+ if (allStatus !== status) return;
257
+ } else {
258
+ allStatus = status;
259
+ }
260
+ }
261
+
262
+ return allStatus;
263
+ }
264
+
265
+ function resolve(dangerous, resolved) {
266
+ return this._resolve(dangerous, resolved) || this;
267
+ }
268
+
269
+ function _resolve(dangerous, resolved) {
270
+ if (resolved && resolved.indexOf(this) >= 0) return;
271
+ resolved = resolved || [];
272
+ resolved.push(this);
273
+
274
+ if (this.isVariableDeclarator()) {
275
+ if (this.get("id").isIdentifier()) {
276
+ return this.get("init").resolve(dangerous, resolved);
277
+ } else {}
278
+ } else if (this.isReferencedIdentifier()) {
279
+ var binding = this.scope.getBinding(this.node.name);
280
+ if (!binding) return;
281
+ if (!binding.constant) return;
282
+ if (binding.kind === "module") return;
283
+
284
+ if (binding.path !== this) {
285
+ var ret = binding.path.resolve(dangerous, resolved);
286
+ if (this.find(function (parent) {
287
+ return parent.node === ret.node;
288
+ })) return;
289
+ return ret;
290
+ }
291
+ } else if (this.isTypeCastExpression()) {
292
+ return this.get("expression").resolve(dangerous, resolved);
293
+ } else if (dangerous && this.isMemberExpression()) {
294
+ var targetKey = this.toComputedKey();
295
+ if (!t.isLiteral(targetKey)) return;
296
+ var targetName = targetKey.value;
297
+ var target = this.get("object").resolve(dangerous, resolved);
298
+
299
+ if (target.isObjectExpression()) {
300
+ var props = target.get("properties");
301
+ var _arr = props;
302
+
303
+ for (var _i3 = 0; _i3 < _arr.length; _i3++) {
304
+ var prop = _arr[_i3];
305
+ if (!prop.isProperty()) continue;
306
+ var key = prop.get("key");
307
+ var match = prop.isnt("computed") && key.isIdentifier({
308
+ name: targetName
309
+ });
310
+ match = match || key.isLiteral({
311
+ value: targetName
312
+ });
313
+ if (match) return prop.get("value").resolve(dangerous, resolved);
314
+ }
315
+ } else if (target.isArrayExpression() && !isNaN(+targetName)) {
316
+ var elems = target.get("elements");
317
+ var elem = elems[targetName];
318
+ if (elem) return elem.resolve(dangerous, resolved);
319
+ }
320
+ }
321
+ }
322
+
323
+ function isConstantExpression() {
324
+ if (this.isIdentifier()) {
325
+ var binding = this.scope.getBinding(this.node.name);
326
+
327
+ if (!binding) {
328
+ return false;
329
+ }
330
+
331
+ return binding.constant && binding.path.get("init").isConstantExpression();
332
+ }
333
+
334
+ if (this.isLiteral()) {
335
+ if (this.isRegExpLiteral()) {
336
+ return false;
337
+ }
338
+
339
+ if (this.isTemplateLiteral()) {
340
+ return this.get("expressions").every(function (expression) {
341
+ return expression.isConstantExpression();
342
+ });
343
+ }
344
+
345
+ return true;
346
+ }
347
+
348
+ if (this.isUnaryExpression()) {
349
+ if (this.get("operator").node !== "void") {
350
+ return false;
351
+ }
352
+
353
+ return this.get("argument").isConstantExpression();
354
+ }
355
+
356
+ if (this.isBinaryExpression()) {
357
+ return this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
358
+ }
359
+
360
+ return false;
361
+ }