@babel/traverse 7.0.0-beta.45 → 7.0.0-beta.49

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.

package/lib/path/index.js CHANGED
@@ -7,10 +7,10 @@ exports.default = void 0;
7
7
 
8
8
  var virtualTypes = _interopRequireWildcard(require("./lib/virtual-types"));
9
9
 
10
- function _debug2() {
11
- var data = _interopRequireDefault(require("debug"));
10
+ function _debug() {
11
+ const data = _interopRequireDefault(require("debug"));
12
12
 
13
- _debug2 = function _debug2() {
13
+ _debug = function () {
14
14
  return data;
15
15
  };
16
16
 
@@ -18,9 +18,9 @@ function _debug2() {
18
18
  }
19
19
 
20
20
  function _invariant() {
21
- var data = _interopRequireDefault(require("invariant"));
21
+ const data = _interopRequireDefault(require("invariant"));
22
22
 
23
- _invariant = function _invariant() {
23
+ _invariant = function () {
24
24
  return data;
25
25
  };
26
26
 
@@ -32,9 +32,9 @@ var _index = _interopRequireDefault(require("../index"));
32
32
  var _scope = _interopRequireDefault(require("../scope"));
33
33
 
34
34
  function t() {
35
- var data = _interopRequireWildcard(require("@babel/types"));
35
+ const data = _interopRequireWildcard(require("@babel/types"));
36
36
 
37
- t = function t() {
37
+ t = function () {
38
38
  return data;
39
39
  };
40
40
 
@@ -44,9 +44,9 @@ function t() {
44
44
  var _cache = require("../cache");
45
45
 
46
46
  function _generator() {
47
- var data = _interopRequireDefault(require("@babel/generator"));
47
+ const data = _interopRequireDefault(require("@babel/generator"));
48
48
 
49
- _generator = function _generator() {
49
+ _generator = function () {
50
50
  return data;
51
51
  };
52
52
 
@@ -79,10 +79,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
79
79
 
80
80
  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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
81
81
 
82
- var _debug = (0, _debug2().default)("babel");
82
+ const debug = (0, _debug().default)("babel");
83
83
 
84
- var NodePath = function () {
85
- function NodePath(hub, parent) {
84
+ class NodePath {
85
+ constructor(hub, parent) {
86
86
  this.parent = parent;
87
87
  this.hub = hub;
88
88
  this.contexts = [];
@@ -106,30 +106,30 @@ var NodePath = function () {
106
106
  this.typeAnnotation = null;
107
107
  }
108
108
 
109
- NodePath.get = function get(_ref) {
110
- var hub = _ref.hub,
111
- parentPath = _ref.parentPath,
112
- parent = _ref.parent,
113
- container = _ref.container,
114
- listKey = _ref.listKey,
115
- key = _ref.key;
116
-
109
+ static get({
110
+ hub,
111
+ parentPath,
112
+ parent,
113
+ container,
114
+ listKey,
115
+ key
116
+ }) {
117
117
  if (!hub && parentPath) {
118
118
  hub = parentPath.hub;
119
119
  }
120
120
 
121
121
  (0, _invariant().default)(parent, "To get a node path the parent needs to exist");
122
- var targetNode = container[key];
123
- var paths = _cache.path.get(parent) || [];
122
+ const targetNode = container[key];
123
+ const paths = _cache.path.get(parent) || [];
124
124
 
125
125
  if (!_cache.path.has(parent)) {
126
126
  _cache.path.set(parent, paths);
127
127
  }
128
128
 
129
- var path;
129
+ let path;
130
130
 
131
- for (var i = 0; i < paths.length; i++) {
132
- var pathCheck = paths[i];
131
+ for (let i = 0; i < paths.length; i++) {
132
+ const pathCheck = paths[i];
133
133
 
134
134
  if (pathCheck.node === targetNode) {
135
135
  path = pathCheck;
@@ -144,103 +144,83 @@ var NodePath = function () {
144
144
 
145
145
  path.setup(parentPath, container, listKey, key);
146
146
  return path;
147
- };
148
-
149
- var _proto = NodePath.prototype;
147
+ }
150
148
 
151
- _proto.getScope = function getScope(scope) {
149
+ getScope(scope) {
152
150
  return this.isScope() ? new _scope.default(this) : scope;
153
- };
151
+ }
154
152
 
155
- _proto.setData = function setData(key, val) {
153
+ setData(key, val) {
156
154
  return this.data[key] = val;
157
- };
155
+ }
158
156
 
159
- _proto.getData = function getData(key, def) {
160
- var val = this.data[key];
157
+ getData(key, def) {
158
+ let val = this.data[key];
161
159
  if (!val && def) val = this.data[key] = def;
162
160
  return val;
163
- };
164
-
165
- _proto.buildCodeFrameError = function buildCodeFrameError(msg, Error) {
166
- if (Error === void 0) {
167
- Error = SyntaxError;
168
- }
161
+ }
169
162
 
163
+ buildCodeFrameError(msg, Error = SyntaxError) {
170
164
  return this.hub.file.buildCodeFrameError(this.node, msg, Error);
171
- };
165
+ }
172
166
 
173
- _proto.traverse = function traverse(visitor, state) {
167
+ traverse(visitor, state) {
174
168
  (0, _index.default)(this.node, visitor, this.scope, state, this);
175
- };
169
+ }
176
170
 
177
- _proto.set = function set(key, node) {
171
+ set(key, node) {
178
172
  t().validate(this.node, key, node);
179
173
  this.node[key] = node;
180
- };
174
+ }
181
175
 
182
- _proto.getPathLocation = function getPathLocation() {
183
- var parts = [];
184
- var path = this;
176
+ getPathLocation() {
177
+ const parts = [];
178
+ let path = this;
185
179
 
186
180
  do {
187
- var key = path.key;
188
- if (path.inList) key = path.listKey + "[" + key + "]";
181
+ let key = path.key;
182
+ if (path.inList) key = `${path.listKey}[${key}]`;
189
183
  parts.unshift(key);
190
184
  } while (path = path.parentPath);
191
185
 
192
186
  return parts.join(".");
193
- };
194
-
195
- _proto.debug = function debug(message) {
196
- if (!_debug.enabled) return;
187
+ }
197
188
 
198
- _debug(this.getPathLocation() + " " + this.type + ": " + message);
199
- };
189
+ debug(message) {
190
+ if (!debug.enabled) return;
191
+ debug(`${this.getPathLocation()} ${this.type}: ${message}`);
192
+ }
200
193
 
201
- _proto.toString = function toString() {
194
+ toString() {
202
195
  return (0, _generator().default)(this.node).code;
203
- };
196
+ }
204
197
 
205
- return NodePath;
206
- }();
198
+ }
207
199
 
208
200
  exports.default = NodePath;
209
201
  Object.assign(NodePath.prototype, NodePath_ancestry, NodePath_inference, NodePath_replacement, NodePath_evaluation, NodePath_conversion, NodePath_introspection, NodePath_context, NodePath_removal, NodePath_modification, NodePath_family, NodePath_comments);
210
- var _arr = t().TYPES;
211
202
 
212
- var _loop2 = function _loop2() {
213
- var type = _arr[_i];
214
- var typeKey = "is" + type;
215
- var fn = t()[typeKey];
203
+ for (const type of t().TYPES) {
204
+ const typeKey = `is${type}`;
205
+ const fn = t()[typeKey];
216
206
 
217
207
  NodePath.prototype[typeKey] = function (opts) {
218
208
  return fn(this.node, opts);
219
209
  };
220
210
 
221
- NodePath.prototype["assert" + type] = function (opts) {
211
+ NodePath.prototype[`assert${type}`] = function (opts) {
222
212
  if (!fn(this.node, opts)) {
223
- throw new TypeError("Expected node path of type " + type);
213
+ throw new TypeError(`Expected node path of type ${type}`);
224
214
  }
225
215
  };
226
- };
227
-
228
- for (var _i = 0; _i < _arr.length; _i++) {
229
- _loop2();
230
216
  }
231
217
 
232
- var _loop = function _loop(type) {
233
- if (type[0] === "_") return "continue";
218
+ for (const type in virtualTypes) {
219
+ if (type[0] === "_") continue;
234
220
  if (t().TYPES.indexOf(type) < 0) t().TYPES.push(type);
235
- var virtualType = virtualTypes[type];
221
+ const virtualType = virtualTypes[type];
236
222
 
237
- NodePath.prototype["is" + type] = function (opts) {
223
+ NodePath.prototype[`is${type}`] = function (opts) {
238
224
  return virtualType.checkPath(this, opts);
239
225
  };
240
- };
241
-
242
- for (var type in virtualTypes) {
243
- var _ret = _loop(type);
244
-
245
- if (_ret === "continue") continue;
246
226
  }
@@ -13,9 +13,9 @@ exports.isGenericType = isGenericType;
13
13
  var inferers = _interopRequireWildcard(require("./inferers"));
14
14
 
15
15
  function t() {
16
- var data = _interopRequireWildcard(require("@babel/types"));
16
+ const data = _interopRequireWildcard(require("@babel/types"));
17
17
 
18
- t = function t() {
18
+ t = function () {
19
19
  return data;
20
20
  };
21
21
 
@@ -26,18 +26,18 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
26
26
 
27
27
  function getTypeAnnotation() {
28
28
  if (this.typeAnnotation) return this.typeAnnotation;
29
- var type = this._getTypeAnnotation() || t().anyTypeAnnotation();
29
+ let type = this._getTypeAnnotation() || t().anyTypeAnnotation();
30
30
  if (t().isTypeAnnotation(type)) type = type.typeAnnotation;
31
31
  return this.typeAnnotation = type;
32
32
  }
33
33
 
34
34
  function _getTypeAnnotation() {
35
- var node = this.node;
35
+ const node = this.node;
36
36
 
37
37
  if (!node) {
38
38
  if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
39
- var declar = this.parentPath.parentPath;
40
- var declarParent = declar.parentPath;
39
+ const declar = this.parentPath.parentPath;
40
+ const declarParent = declar.parentPath;
41
41
 
42
42
  if (declar.key === "left" && declarParent.isForInStatement()) {
43
43
  return t().stringTypeAnnotation();
@@ -57,7 +57,7 @@ function _getTypeAnnotation() {
57
57
  return node.typeAnnotation;
58
58
  }
59
59
 
60
- var inferer = inferers[node.type];
60
+ let inferer = inferers[node.type];
61
61
 
62
62
  if (inferer) {
63
63
  return inferer.call(this, node);
@@ -93,21 +93,17 @@ function _isBaseType(baseName, type, soft) {
93
93
  if (soft) {
94
94
  return false;
95
95
  } else {
96
- throw new Error("Unknown base type " + baseName);
96
+ throw new Error(`Unknown base type ${baseName}`);
97
97
  }
98
98
  }
99
99
  }
100
100
 
101
101
  function couldBeBaseType(name) {
102
- var type = this.getTypeAnnotation();
102
+ const type = this.getTypeAnnotation();
103
103
  if (t().isAnyTypeAnnotation(type)) return true;
104
104
 
105
105
  if (t().isUnionTypeAnnotation(type)) {
106
- var _arr = type.types;
107
-
108
- for (var _i = 0; _i < _arr.length; _i++) {
109
- var type2 = _arr[_i];
110
-
106
+ for (const type2 of type.types) {
111
107
  if (t().isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
112
108
  return true;
113
109
  }
@@ -120,7 +116,7 @@ function couldBeBaseType(name) {
120
116
  }
121
117
 
122
118
  function baseTypeStrictlyMatches(right) {
123
- var left = this.getTypeAnnotation();
119
+ const left = this.getTypeAnnotation();
124
120
  right = right.getTypeAnnotation();
125
121
 
126
122
  if (!t().isAnyTypeAnnotation(left) && t().isFlowBaseAnnotation(left)) {
@@ -129,7 +125,7 @@ function baseTypeStrictlyMatches(right) {
129
125
  }
130
126
 
131
127
  function isGenericType(genericName) {
132
- var type = this.getTypeAnnotation();
128
+ const type = this.getTypeAnnotation();
133
129
  return t().isGenericTypeAnnotation(type) && t().isIdentifier(type.id, {
134
130
  name: genericName
135
131
  });
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = _default;
7
7
 
8
8
  function t() {
9
- var data = _interopRequireWildcard(require("@babel/types"));
9
+ const data = _interopRequireWildcard(require("@babel/types"));
10
10
 
11
- t = function t() {
11
+ t = function () {
12
12
  return data;
13
13
  };
14
14
 
@@ -19,7 +19,7 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
19
19
 
20
20
  function _default(node) {
21
21
  if (!this.isReferenced()) return;
22
- var binding = this.scope.getBinding(node.name);
22
+ const binding = this.scope.getBinding(node.name);
23
23
 
24
24
  if (binding) {
25
25
  if (binding.identifier.typeAnnotation) {
@@ -37,25 +37,21 @@ function _default(node) {
37
37
  }
38
38
 
39
39
  function getTypeAnnotationBindingConstantViolations(binding, path, name) {
40
- var types = [];
41
- var functionConstantViolations = [];
42
- var constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
43
- var testType = getConditionalAnnotation(binding, path, name);
40
+ const types = [];
41
+ const functionConstantViolations = [];
42
+ let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
43
+ const testType = getConditionalAnnotation(binding, path, name);
44
44
 
45
45
  if (testType) {
46
- var testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
47
- constantViolations = constantViolations.filter(function (path) {
48
- return testConstantViolations.indexOf(path) < 0;
49
- });
46
+ const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
47
+ constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0);
50
48
  types.push(testType.typeAnnotation);
51
49
  }
52
50
 
53
51
  if (constantViolations.length) {
54
52
  constantViolations = constantViolations.concat(functionConstantViolations);
55
- var _arr = constantViolations;
56
53
 
57
- for (var _i = 0; _i < _arr.length; _i++) {
58
- var violation = _arr[_i];
54
+ for (const violation of constantViolations) {
59
55
  types.push(violation.getTypeAnnotation());
60
56
  }
61
57
  }
@@ -66,12 +62,12 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
66
62
  }
67
63
 
68
64
  function getConstantViolationsBefore(binding, path, functions) {
69
- var violations = binding.constantViolations.slice();
65
+ const violations = binding.constantViolations.slice();
70
66
  violations.unshift(binding.path);
71
- return violations.filter(function (violation) {
67
+ return violations.filter(violation => {
72
68
  violation = violation.resolve();
73
69
 
74
- var status = violation._guessExecutionStatusRelativeTo(path);
70
+ const status = violation._guessExecutionStatusRelativeTo(path);
75
71
 
76
72
  if (functions && status === "function") functions.push(violation);
77
73
  return status === "before";
@@ -79,17 +75,17 @@ function getConstantViolationsBefore(binding, path, functions) {
79
75
  }
80
76
 
81
77
  function inferAnnotationFromBinaryExpression(name, path) {
82
- var operator = path.node.operator;
83
- var right = path.get("right").resolve();
84
- var left = path.get("left").resolve();
85
- var target;
78
+ const operator = path.node.operator;
79
+ const right = path.get("right").resolve();
80
+ const left = path.get("left").resolve();
81
+ let target;
86
82
 
87
83
  if (left.isIdentifier({
88
- name: name
84
+ name
89
85
  })) {
90
86
  target = right;
91
87
  } else if (right.isIdentifier({
92
- name: name
88
+ name
93
89
  })) {
94
90
  target = left;
95
91
  }
@@ -107,8 +103,8 @@ function inferAnnotationFromBinaryExpression(name, path) {
107
103
  }
108
104
 
109
105
  if (operator !== "===" && operator !== "==") return;
110
- var typeofPath;
111
- var typePath;
106
+ let typeofPath;
107
+ let typePath;
112
108
 
113
109
  if (left.isUnaryExpression({
114
110
  operator: "typeof"
@@ -124,17 +120,17 @@ function inferAnnotationFromBinaryExpression(name, path) {
124
120
 
125
121
  if (!typeofPath) return;
126
122
  if (!typeofPath.get("argument").isIdentifier({
127
- name: name
123
+ name
128
124
  })) return;
129
125
  typePath = typePath.resolve();
130
126
  if (!typePath.isLiteral()) return;
131
- var typeValue = typePath.node.value;
127
+ const typeValue = typePath.node.value;
132
128
  if (typeof typeValue !== "string") return;
133
129
  return t().createTypeAnnotationBasedOnTypeof(typeValue);
134
130
  }
135
131
 
136
132
  function getParentConditionalPath(binding, path, name) {
137
- var parentPath;
133
+ let parentPath;
138
134
 
139
135
  while (parentPath = path.parentPath) {
140
136
  if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
@@ -154,22 +150,22 @@ function getParentConditionalPath(binding, path, name) {
154
150
  }
155
151
 
156
152
  function getConditionalAnnotation(binding, path, name) {
157
- var ifStatement = getParentConditionalPath(binding, path, name);
153
+ const ifStatement = getParentConditionalPath(binding, path, name);
158
154
  if (!ifStatement) return;
159
- var test = ifStatement.get("test");
160
- var paths = [test];
161
- var types = [];
155
+ const test = ifStatement.get("test");
156
+ const paths = [test];
157
+ const types = [];
162
158
 
163
- for (var i = 0; i < paths.length; i++) {
164
- var _path = paths[i];
159
+ for (let i = 0; i < paths.length; i++) {
160
+ const path = paths[i];
165
161
 
166
- if (_path.isLogicalExpression()) {
167
- if (_path.node.operator === "&&") {
168
- paths.push(_path.get("left"));
169
- paths.push(_path.get("right"));
162
+ if (path.isLogicalExpression()) {
163
+ if (path.node.operator === "&&") {
164
+ paths.push(path.get("left"));
165
+ paths.push(path.get("right"));
170
166
  }
171
- } else if (_path.isBinaryExpression()) {
172
- var type = inferAnnotationFromBinaryExpression(name, _path);
167
+ } else if (path.isBinaryExpression()) {
168
+ const type = inferAnnotationFromBinaryExpression(name, path);
173
169
  if (type) types.push(type);
174
170
  }
175
171
  }
@@ -177,7 +173,7 @@ function getConditionalAnnotation(binding, path, name) {
177
173
  if (types.length) {
178
174
  return {
179
175
  typeAnnotation: t().createUnionTypeAnnotation(types),
180
- ifStatement: ifStatement
176
+ ifStatement
181
177
  };
182
178
  }
183
179
 
@@ -27,15 +27,15 @@ exports.CallExpression = CallExpression;
27
27
  exports.TaggedTemplateExpression = TaggedTemplateExpression;
28
28
  Object.defineProperty(exports, "Identifier", {
29
29
  enumerable: true,
30
- get: function get() {
30
+ get: function () {
31
31
  return _infererReference.default;
32
32
  }
33
33
  });
34
34
 
35
35
  function t() {
36
- var data = _interopRequireWildcard(require("@babel/types"));
36
+ const data = _interopRequireWildcard(require("@babel/types"));
37
37
 
38
- t = function t() {
38
+ t = function () {
39
39
  return data;
40
40
  };
41
41
 
@@ -49,10 +49,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
49
49
  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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
50
50
 
51
51
  function VariableDeclarator() {
52
- var id = this.get("id");
52
+ const id = this.get("id");
53
53
  if (!id.isIdentifier()) return;
54
- var init = this.get("init");
55
- var type = init.getTypeAnnotation();
54
+ const init = this.get("init");
55
+ let type = init.getTypeAnnotation();
56
56
 
57
57
  if (type && type.type === "AnyTypeAnnotation") {
58
58
  if (init.isCallExpression() && init.get("callee").isIdentifier({
@@ -82,7 +82,7 @@ function TemplateLiteral() {
82
82
  }
83
83
 
84
84
  function UnaryExpression(node) {
85
- var operator = node.operator;
85
+ const operator = node.operator;
86
86
 
87
87
  if (operator === "void") {
88
88
  return t().voidTypeAnnotation();
@@ -96,15 +96,15 @@ function UnaryExpression(node) {
96
96
  }
97
97
 
98
98
  function BinaryExpression(node) {
99
- var operator = node.operator;
99
+ const operator = node.operator;
100
100
 
101
101
  if (t().NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
102
102
  return t().numberTypeAnnotation();
103
103
  } else if (t().BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
104
104
  return t().booleanTypeAnnotation();
105
105
  } else if (operator === "+") {
106
- var right = this.get("right");
107
- var left = this.get("left");
106
+ const right = this.get("right");
107
+ const left = this.get("left");
108
108
 
109
109
  if (left.isBaseType("number") && right.isBaseType("number")) {
110
110
  return t().numberTypeAnnotation();
@@ -133,7 +133,7 @@ function AssignmentExpression() {
133
133
  }
134
134
 
135
135
  function UpdateExpression(node) {
136
- var operator = node.operator;
136
+ const operator = node.operator;
137
137
 
138
138
  if (operator === "++" || operator === "--") {
139
139
  return t().numberTypeAnnotation();
@@ -178,13 +178,15 @@ function Func() {
178
178
  return t().genericTypeAnnotation(t().identifier("Function"));
179
179
  }
180
180
 
181
- var isArrayFrom = t().buildMatchMemberExpression("Array.from");
182
- var isObjectKeys = t().buildMatchMemberExpression("Object.keys");
183
- var isObjectValues = t().buildMatchMemberExpression("Object.values");
184
- var isObjectEntries = t().buildMatchMemberExpression("Object.entries");
181
+ const isArrayFrom = t().buildMatchMemberExpression("Array.from");
182
+ const isObjectKeys = t().buildMatchMemberExpression("Object.keys");
183
+ const isObjectValues = t().buildMatchMemberExpression("Object.values");
184
+ const isObjectEntries = t().buildMatchMemberExpression("Object.entries");
185
185
 
186
186
  function CallExpression() {
187
- var callee = this.node.callee;
187
+ const {
188
+ callee
189
+ } = this.node;
188
190
 
189
191
  if (isObjectKeys(callee)) {
190
192
  return t().arrayTypeAnnotation(t().stringTypeAnnotation());