@babel/traverse 7.0.0-beta.46 → 7.0.0-beta.47

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 _debug() {
14
14
  return data;
15
15
  };
16
16
 
@@ -18,7 +18,7 @@ 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
23
  _invariant = function _invariant() {
24
24
  return data;
@@ -32,7 +32,7 @@ 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
37
  t = function t() {
38
38
  return data;
@@ -44,7 +44,7 @@ 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
49
  _generator = function _generator() {
50
50
  return data;
@@ -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,85 @@ 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
202
  var _arr = t().TYPES;
211
203
 
212
- var _loop2 = function _loop2() {
213
- var type = _arr[_i];
214
- var typeKey = "is" + type;
215
- var fn = t()[typeKey];
204
+ for (var _i = 0; _i < _arr.length; _i++) {
205
+ const type = _arr[_i];
206
+ const typeKey = `is${type}`;
207
+ const fn = t()[typeKey];
216
208
 
217
209
  NodePath.prototype[typeKey] = function (opts) {
218
210
  return fn(this.node, opts);
219
211
  };
220
212
 
221
- NodePath.prototype["assert" + type] = function (opts) {
213
+ NodePath.prototype[`assert${type}`] = function (opts) {
222
214
  if (!fn(this.node, opts)) {
223
- throw new TypeError("Expected node path of type " + type);
215
+ throw new TypeError(`Expected node path of type ${type}`);
224
216
  }
225
217
  };
226
- };
227
-
228
- for (var _i = 0; _i < _arr.length; _i++) {
229
- _loop2();
230
218
  }
231
219
 
232
- var _loop = function _loop(type) {
233
- if (type[0] === "_") return "continue";
220
+ for (const type in virtualTypes) {
221
+ if (type[0] === "_") continue;
234
222
  if (t().TYPES.indexOf(type) < 0) t().TYPES.push(type);
235
- var virtualType = virtualTypes[type];
223
+ const virtualType = virtualTypes[type];
236
224
 
237
- NodePath.prototype["is" + type] = function (opts) {
225
+ NodePath.prototype[`is${type}`] = function (opts) {
238
226
  return virtualType.checkPath(this, opts);
239
227
  };
240
- };
241
-
242
- for (var type in virtualTypes) {
243
- var _ret = _loop(type);
244
-
245
- if (_ret === "continue") continue;
246
228
  }
@@ -13,7 +13,7 @@ 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
18
  t = function t() {
19
19
  return data;
@@ -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,20 +93,20 @@ 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
106
  var _arr = type.types;
107
107
 
108
108
  for (var _i = 0; _i < _arr.length; _i++) {
109
- var type2 = _arr[_i];
109
+ const type2 = _arr[_i];
110
110
 
111
111
  if (t().isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
112
112
  return true;
@@ -120,7 +120,7 @@ function couldBeBaseType(name) {
120
120
  }
121
121
 
122
122
  function baseTypeStrictlyMatches(right) {
123
- var left = this.getTypeAnnotation();
123
+ const left = this.getTypeAnnotation();
124
124
  right = right.getTypeAnnotation();
125
125
 
126
126
  if (!t().isAnyTypeAnnotation(left) && t().isFlowBaseAnnotation(left)) {
@@ -129,7 +129,7 @@ function baseTypeStrictlyMatches(right) {
129
129
  }
130
130
 
131
131
  function isGenericType(genericName) {
132
- var type = this.getTypeAnnotation();
132
+ const type = this.getTypeAnnotation();
133
133
  return t().isGenericTypeAnnotation(type) && t().isIdentifier(type.id, {
134
134
  name: genericName
135
135
  });
@@ -6,7 +6,7 @@ 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
11
  t = function t() {
12
12
  return data;
@@ -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,16 +37,14 @@ 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
 
@@ -55,7 +53,7 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
55
53
  var _arr = constantViolations;
56
54
 
57
55
  for (var _i = 0; _i < _arr.length; _i++) {
58
- var violation = _arr[_i];
56
+ const violation = _arr[_i];
59
57
  types.push(violation.getTypeAnnotation());
60
58
  }
61
59
  }
@@ -66,12 +64,12 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
66
64
  }
67
65
 
68
66
  function getConstantViolationsBefore(binding, path, functions) {
69
- var violations = binding.constantViolations.slice();
67
+ const violations = binding.constantViolations.slice();
70
68
  violations.unshift(binding.path);
71
- return violations.filter(function (violation) {
69
+ return violations.filter(violation => {
72
70
  violation = violation.resolve();
73
71
 
74
- var status = violation._guessExecutionStatusRelativeTo(path);
72
+ const status = violation._guessExecutionStatusRelativeTo(path);
75
73
 
76
74
  if (functions && status === "function") functions.push(violation);
77
75
  return status === "before";
@@ -79,17 +77,17 @@ function getConstantViolationsBefore(binding, path, functions) {
79
77
  }
80
78
 
81
79
  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;
80
+ const operator = path.node.operator;
81
+ const right = path.get("right").resolve();
82
+ const left = path.get("left").resolve();
83
+ let target;
86
84
 
87
85
  if (left.isIdentifier({
88
- name: name
86
+ name
89
87
  })) {
90
88
  target = right;
91
89
  } else if (right.isIdentifier({
92
- name: name
90
+ name
93
91
  })) {
94
92
  target = left;
95
93
  }
@@ -107,8 +105,8 @@ function inferAnnotationFromBinaryExpression(name, path) {
107
105
  }
108
106
 
109
107
  if (operator !== "===" && operator !== "==") return;
110
- var typeofPath;
111
- var typePath;
108
+ let typeofPath;
109
+ let typePath;
112
110
 
113
111
  if (left.isUnaryExpression({
114
112
  operator: "typeof"
@@ -124,17 +122,17 @@ function inferAnnotationFromBinaryExpression(name, path) {
124
122
 
125
123
  if (!typeofPath) return;
126
124
  if (!typeofPath.get("argument").isIdentifier({
127
- name: name
125
+ name
128
126
  })) return;
129
127
  typePath = typePath.resolve();
130
128
  if (!typePath.isLiteral()) return;
131
- var typeValue = typePath.node.value;
129
+ const typeValue = typePath.node.value;
132
130
  if (typeof typeValue !== "string") return;
133
131
  return t().createTypeAnnotationBasedOnTypeof(typeValue);
134
132
  }
135
133
 
136
134
  function getParentConditionalPath(binding, path, name) {
137
- var parentPath;
135
+ let parentPath;
138
136
 
139
137
  while (parentPath = path.parentPath) {
140
138
  if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
@@ -154,22 +152,22 @@ function getParentConditionalPath(binding, path, name) {
154
152
  }
155
153
 
156
154
  function getConditionalAnnotation(binding, path, name) {
157
- var ifStatement = getParentConditionalPath(binding, path, name);
155
+ const ifStatement = getParentConditionalPath(binding, path, name);
158
156
  if (!ifStatement) return;
159
- var test = ifStatement.get("test");
160
- var paths = [test];
161
- var types = [];
157
+ const test = ifStatement.get("test");
158
+ const paths = [test];
159
+ const types = [];
162
160
 
163
- for (var i = 0; i < paths.length; i++) {
164
- var _path = paths[i];
161
+ for (let i = 0; i < paths.length; i++) {
162
+ const path = paths[i];
165
163
 
166
- if (_path.isLogicalExpression()) {
167
- if (_path.node.operator === "&&") {
168
- paths.push(_path.get("left"));
169
- paths.push(_path.get("right"));
164
+ if (path.isLogicalExpression()) {
165
+ if (path.node.operator === "&&") {
166
+ paths.push(path.get("left"));
167
+ paths.push(path.get("right"));
170
168
  }
171
- } else if (_path.isBinaryExpression()) {
172
- var type = inferAnnotationFromBinaryExpression(name, _path);
169
+ } else if (path.isBinaryExpression()) {
170
+ const type = inferAnnotationFromBinaryExpression(name, path);
173
171
  if (type) types.push(type);
174
172
  }
175
173
  }
@@ -177,7 +175,7 @@ function getConditionalAnnotation(binding, path, name) {
177
175
  if (types.length) {
178
176
  return {
179
177
  typeAnnotation: t().createUnionTypeAnnotation(types),
180
- ifStatement: ifStatement
178
+ ifStatement
181
179
  };
182
180
  }
183
181
 
@@ -33,7 +33,7 @@ Object.defineProperty(exports, "Identifier", {
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
38
  t = function t() {
39
39
  return data;
@@ -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,13 @@ 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 callee = this.node.callee;
188
188
 
189
189
  if (isObjectKeys(callee)) {
190
190
  return t().arrayTypeAnnotation(t().stringTypeAnnotation());