modification.js
6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"use strict";
exports.__esModule = true;
exports.insertBefore = insertBefore;
exports._containerInsert = _containerInsert;
exports._containerInsertBefore = _containerInsertBefore;
exports._containerInsertAfter = _containerInsertAfter;
exports._maybePopFromStatements = _maybePopFromStatements;
exports.insertAfter = insertAfter;
exports.updateSiblingKeys = updateSiblingKeys;
exports._verifyNodeList = _verifyNodeList;
exports.unshiftContainer = unshiftContainer;
exports.pushContainer = pushContainer;
exports.hoist = hoist;
// istanbul ignore next
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; } }
// istanbul ignore next
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var _libHoister = require("./lib/hoister");
var _libHoister2 = _interopRequireDefault(_libHoister);
var _index = require("./index");
var _index2 = _interopRequireDefault(_index);
var _types = require("../../types");
var t = _interopRequireWildcard(_types);
/**
* Insert the provided nodes before the current one.
*/
function insertBefore(nodes) {
this._assertUnremoved();
nodes = this._verifyNodeList(nodes);
if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement()) {
return this.parentPath.insertBefore(nodes);
} else if (this.isNodeType("Expression") || this.parentPath.isForStatement() && this.key === "init") {
if (this.node) nodes.push(this.node);
this.replaceExpressionWithStatements(nodes);
} else {
this._maybePopFromStatements(nodes);
if (Array.isArray(this.container)) {
return this._containerInsertBefore(nodes);
} else if (this.isStatementOrBlock()) {
if (this.node) nodes.push(this.node);
this.node = this.container[this.key] = t.blockStatement(nodes);
} else {
throw new Error("We don't know what to do with this node type. We were previously a Statement but we can't fit in here?");
}
}
return [this];
}
/**
* [Please add a description.]
*/
function _containerInsert(from, nodes) {
this.updateSiblingKeys(from, nodes.length);
var paths = [];
for (var i = 0; i < nodes.length; i++) {
var to = from + i;
var node = nodes[i];
this.container.splice(to, 0, node);
if (this.context) {
var path = this.context.create(this.parent, this.container, to, this.listKey);
paths.push(path);
this.queueNode(path);
} else {
paths.push(_index2["default"].get({
parentPath: this,
parent: node,
container: this.container,
listKey: this.listKey,
key: to
}));
}
}
return paths;
}
/**
* [Please add a description.]
*/
function _containerInsertBefore(nodes) {
return this._containerInsert(this.key, nodes);
}
/**
* [Please add a description.]
*/
function _containerInsertAfter(nodes) {
return this._containerInsert(this.key + 1, nodes);
}
/**
* [Please add a description.]
*/
function _maybePopFromStatements(nodes) {
var last = nodes[nodes.length - 1];
if (t.isExpressionStatement(last) && t.isIdentifier(last.expression) && !this.isCompletionRecord()) {
nodes.pop();
}
}
/**
* Insert the provided nodes after the current one. When inserting nodes after an
* expression, ensure that the completion record is correct by pushing the current node.
*/
function insertAfter(nodes) {
this._assertUnremoved();
nodes = this._verifyNodeList(nodes);
if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement()) {
return this.parentPath.insertAfter(nodes);
} else if (this.isNodeType("Expression") || this.parentPath.isForStatement() && this.key === "init") {
if (this.node) {
var temp = this.scope.generateDeclaredUidIdentifier();
nodes.unshift(t.expressionStatement(t.assignmentExpression("=", temp, this.node)));
nodes.push(t.expressionStatement(temp));
}
this.replaceExpressionWithStatements(nodes);
} else {
this._maybePopFromStatements(nodes);
if (Array.isArray(this.container)) {
return this._containerInsertAfter(nodes);
} else if (this.isStatementOrBlock()) {
if (this.node) nodes.unshift(this.node);
this.node = this.container[this.key] = t.blockStatement(nodes);
} else {
throw new Error("We don't know what to do with this node type. We were previously a Statement but we can't fit in here?");
}
}
return [this];
}
/**
* Update all sibling node paths after `fromIndex` by `incrementBy`.
*/
function updateSiblingKeys(fromIndex, incrementBy) {
var paths = this.parent._paths;
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
if (path.key >= fromIndex) {
path.key += incrementBy;
}
}
}
/**
* [Please add a description.]
*/
function _verifyNodeList(nodes) {
if (nodes.constructor !== Array) {
nodes = [nodes];
}
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (!node) {
throw new Error("Node list has falsy node with the index of " + i);
} else if (typeof node !== "object") {
throw new Error("Node list contains a non-object node with the index of " + i);
} else if (!node.type) {
throw new Error("Node list contains a node without a type with the index of " + i);
} else if (node instanceof _index2["default"]) {
nodes[i] = node.node;
}
}
return nodes;
}
/**
* [Please add a description.]
*/
function unshiftContainer(listKey, nodes) {
this._assertUnremoved();
nodes = this._verifyNodeList(nodes);
// get the first path and insert our nodes before it, if it doesn't exist then it
// doesn't matter, our nodes will be inserted anyway
var container = this.node[listKey];
var path = _index2["default"].get({
parentPath: this,
parent: this.node,
container: container,
listKey: listKey,
key: 0
});
return path.insertBefore(nodes);
}
/**
* [Please add a description.]
*/
function pushContainer(listKey, nodes) {
this._assertUnremoved();
nodes = this._verifyNodeList(nodes);
// get an invisible path that represents the last node + 1 and replace it with our
// nodes, effectively inlining it
var container = this.node[listKey];
var i = container.length;
var path = _index2["default"].get({
parentPath: this,
parent: this.node,
container: container,
listKey: listKey,
key: i
});
return path.replaceWith(nodes, true);
}
/**
* Hoist the current node to the highest scope possible and return a UID
* referencing it.
*/
function hoist() {
var scope = arguments.length <= 0 || arguments[0] === undefined ? this.scope : arguments[0];
var hoister = new _libHoister2["default"](this, scope);
return hoister.run();
}