@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,251 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.replaceWithMultiple = replaceWithMultiple;
5
+ exports.replaceWithSourceString = replaceWithSourceString;
6
+ exports.replaceWith = replaceWith;
7
+ exports._replaceWith = _replaceWith;
8
+ exports.replaceExpressionWithStatements = replaceExpressionWithStatements;
9
+ exports.replaceInline = replaceInline;
10
+
11
+ var _codeFrame = require("@babel/code-frame");
12
+
13
+ var _index = _interopRequireDefault(require("../index"));
14
+
15
+ var _index2 = _interopRequireDefault(require("./index"));
16
+
17
+ var _babylon = require("babylon");
18
+
19
+ var t = _interopRequireWildcard(require("@babel/types"));
20
+
21
+ 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; } }
22
+
23
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
+
25
+ var hoistVariablesVisitor = {
26
+ Function: function Function(path) {
27
+ path.skip();
28
+ },
29
+ VariableDeclaration: function VariableDeclaration(path) {
30
+ if (path.node.kind !== "var") return;
31
+ var bindings = path.getBindingIdentifiers();
32
+
33
+ for (var key in bindings) {
34
+ path.scope.push({
35
+ id: bindings[key]
36
+ });
37
+ }
38
+
39
+ var exprs = [];
40
+ var _arr = path.node.declarations;
41
+
42
+ for (var _i = 0; _i < _arr.length; _i++) {
43
+ var declar = _arr[_i];
44
+
45
+ if (declar.init) {
46
+ exprs.push(t.expressionStatement(t.assignmentExpression("=", declar.id, declar.init)));
47
+ }
48
+ }
49
+
50
+ path.replaceWithMultiple(exprs);
51
+ }
52
+ };
53
+
54
+ function replaceWithMultiple(nodes) {
55
+ this.resync();
56
+ nodes = this._verifyNodeList(nodes);
57
+ t.inheritLeadingComments(nodes[0], this.node);
58
+ t.inheritTrailingComments(nodes[nodes.length - 1], this.node);
59
+ this.node = this.container[this.key] = null;
60
+ var paths = this.insertAfter(nodes);
61
+
62
+ if (this.node) {
63
+ this.requeue();
64
+ } else {
65
+ this.remove();
66
+ }
67
+
68
+ return paths;
69
+ }
70
+
71
+ function replaceWithSourceString(replacement) {
72
+ this.resync();
73
+
74
+ try {
75
+ replacement = "(" + replacement + ")";
76
+ replacement = (0, _babylon.parse)(replacement);
77
+ } catch (err) {
78
+ var loc = err.loc;
79
+
80
+ if (loc) {
81
+ err.loc = null;
82
+ err.message += " - make sure this is an expression.\n" + (0, _codeFrame.codeFrameColumns)(replacement, {
83
+ start: {
84
+ line: loc.line,
85
+ column: loc.column + 1
86
+ }
87
+ });
88
+ }
89
+
90
+ throw err;
91
+ }
92
+
93
+ replacement = replacement.program.body[0].expression;
94
+
95
+ _index.default.removeProperties(replacement);
96
+
97
+ return this.replaceWith(replacement);
98
+ }
99
+
100
+ function replaceWith(replacement) {
101
+ this.resync();
102
+
103
+ if (this.removed) {
104
+ throw new Error("You can't replace this node, we've already removed it");
105
+ }
106
+
107
+ if (replacement instanceof _index2.default) {
108
+ replacement = replacement.node;
109
+ }
110
+
111
+ if (!replacement) {
112
+ throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
113
+ }
114
+
115
+ if (this.node === replacement) {
116
+ return [this];
117
+ }
118
+
119
+ if (this.isProgram() && !t.isProgram(replacement)) {
120
+ throw new Error("You can only replace a Program root node with another Program node");
121
+ }
122
+
123
+ if (Array.isArray(replacement)) {
124
+ throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
125
+ }
126
+
127
+ if (typeof replacement === "string") {
128
+ throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
129
+ }
130
+
131
+ var nodePath = "";
132
+
133
+ if (this.isNodeType("Statement") && t.isExpression(replacement)) {
134
+ if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
135
+ replacement = t.expressionStatement(replacement);
136
+ nodePath = "expression";
137
+ }
138
+ }
139
+
140
+ if (this.isNodeType("Expression") && t.isStatement(replacement)) {
141
+ if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
142
+ return this.replaceExpressionWithStatements([replacement]);
143
+ }
144
+ }
145
+
146
+ var oldNode = this.node;
147
+
148
+ if (oldNode) {
149
+ t.inheritsComments(replacement, oldNode);
150
+ t.removeComments(oldNode);
151
+ }
152
+
153
+ this._replaceWith(replacement);
154
+
155
+ this.type = replacement.type;
156
+ this.setScope();
157
+ this.requeue();
158
+ return [nodePath ? this.get(nodePath) : this];
159
+ }
160
+
161
+ function _replaceWith(node) {
162
+ if (!this.container) {
163
+ throw new ReferenceError("Container is falsy");
164
+ }
165
+
166
+ if (this.inList) {
167
+ t.validate(this.parent, this.key, [node]);
168
+ } else {
169
+ t.validate(this.parent, this.key, node);
170
+ }
171
+
172
+ this.debug("Replace with " + (node && node.type));
173
+ this.node = this.container[this.key] = node;
174
+ }
175
+
176
+ function replaceExpressionWithStatements(nodes) {
177
+ this.resync();
178
+ var toSequenceExpression = t.toSequenceExpression(nodes, this.scope);
179
+
180
+ if (toSequenceExpression) {
181
+ return this.replaceWith(toSequenceExpression)[0].get("expressions");
182
+ }
183
+
184
+ var container = t.arrowFunctionExpression([], t.blockStatement(nodes));
185
+ this.replaceWith(t.callExpression(container, []));
186
+ this.traverse(hoistVariablesVisitor);
187
+ var completionRecords = this.get("callee").getCompletionRecords();
188
+
189
+ for (var _iterator = completionRecords, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
190
+ var _ref;
191
+
192
+ if (_isArray) {
193
+ if (_i2 >= _iterator.length) break;
194
+ _ref = _iterator[_i2++];
195
+ } else {
196
+ _i2 = _iterator.next();
197
+ if (_i2.done) break;
198
+ _ref = _i2.value;
199
+ }
200
+
201
+ var _path = _ref;
202
+ if (!_path.isExpressionStatement()) continue;
203
+
204
+ var loop = _path.findParent(function (path) {
205
+ return path.isLoop();
206
+ });
207
+
208
+ if (loop) {
209
+ var uid = loop.getData("expressionReplacementReturnUid");
210
+
211
+ if (!uid) {
212
+ var _callee = this.get("callee");
213
+
214
+ uid = _callee.scope.generateDeclaredUidIdentifier("ret");
215
+
216
+ _callee.get("body").pushContainer("body", t.returnStatement(uid));
217
+
218
+ loop.setData("expressionReplacementReturnUid", uid);
219
+ } else {
220
+ uid = t.identifier(uid.name);
221
+ }
222
+
223
+ _path.get("expression").replaceWith(t.assignmentExpression("=", uid, _path.node.expression));
224
+ } else {
225
+ _path.replaceWith(t.returnStatement(_path.node.expression));
226
+ }
227
+ }
228
+
229
+ var callee = this.get("callee");
230
+ callee.arrowFunctionToExpression();
231
+ return callee.get("body.body");
232
+ }
233
+
234
+ function replaceInline(nodes) {
235
+ this.resync();
236
+
237
+ if (Array.isArray(nodes)) {
238
+ if (Array.isArray(this.container)) {
239
+ nodes = this._verifyNodeList(nodes);
240
+
241
+ var paths = this._containerInsertAfter(nodes);
242
+
243
+ this.remove();
244
+ return paths;
245
+ } else {
246
+ return this.replaceWithMultiple(nodes);
247
+ }
248
+ } else {
249
+ return this.replaceWith(nodes);
250
+ }
251
+ }
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+
6
+ var Binding = function () {
7
+ function Binding(_ref) {
8
+ var identifier = _ref.identifier,
9
+ scope = _ref.scope,
10
+ path = _ref.path,
11
+ kind = _ref.kind;
12
+ this.constantViolations = void 0;
13
+ this.constant = void 0;
14
+ this.referencePaths = void 0;
15
+ this.referenced = void 0;
16
+ this.references = void 0;
17
+ this.hasDeoptedValue = void 0;
18
+ this.hasValue = void 0;
19
+ this.value = void 0;
20
+ this.identifier = identifier;
21
+ this.scope = scope;
22
+ this.path = path;
23
+ this.kind = kind;
24
+ this.constantViolations = [];
25
+ this.constant = true;
26
+ this.referencePaths = [];
27
+ this.referenced = false;
28
+ this.references = 0;
29
+ this.clearValue();
30
+ }
31
+
32
+ var _proto = Binding.prototype;
33
+
34
+ _proto.deoptValue = function deoptValue() {
35
+ this.clearValue();
36
+ this.hasDeoptedValue = true;
37
+ };
38
+
39
+ _proto.setValue = function setValue(value) {
40
+ if (this.hasDeoptedValue) return;
41
+ this.hasValue = true;
42
+ this.value = value;
43
+ };
44
+
45
+ _proto.clearValue = function clearValue() {
46
+ this.hasDeoptedValue = false;
47
+ this.hasValue = false;
48
+ this.value = null;
49
+ };
50
+
51
+ _proto.reassign = function reassign(path) {
52
+ this.constant = false;
53
+
54
+ if (this.constantViolations.indexOf(path) !== -1) {
55
+ return;
56
+ }
57
+
58
+ this.constantViolations.push(path);
59
+ };
60
+
61
+ _proto.reference = function reference(path) {
62
+ if (this.referencePaths.indexOf(path) !== -1) {
63
+ return;
64
+ }
65
+
66
+ this.referenced = true;
67
+ this.references++;
68
+ this.referencePaths.push(path);
69
+ };
70
+
71
+ _proto.dereference = function dereference() {
72
+ this.references--;
73
+ this.referenced = !!this.references;
74
+ };
75
+
76
+ return Binding;
77
+ }();
78
+
79
+ exports.default = Binding;