9003fb3698f28d212d03f1a6bb886bf00e2d3cb4.svn-base
6.89 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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.insertBefore = insertBefore;
exports._containerInsert = _containerInsert;
exports._containerInsertBefore = _containerInsertBefore;
exports._containerInsertAfter = _containerInsertAfter;
exports.insertAfter = insertAfter;
exports.updateSiblingKeys = updateSiblingKeys;
exports._verifyNodeList = _verifyNodeList;
exports.unshiftContainer = unshiftContainer;
exports.pushContainer = pushContainer;
exports.hoist = hoist;
var _cache = require("../cache");
var _hoister = _interopRequireDefault(require("./lib/hoister"));
var _index = _interopRequireDefault(require("./index"));
var t = _interopRequireWildcard(require("@babel/types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function insertBefore(nodes) {
this._assertUnremoved();
nodes = this._verifyNodeList(nodes);
const {
parentPath
} = this;
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
return parentPath.insertBefore(nodes);
} else if (this.isNodeType("Expression") && !this.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
if (this.node) nodes.push(this.node);
return this.replaceExpressionWithStatements(nodes);
} else if (Array.isArray(this.container)) {
return this._containerInsertBefore(nodes);
} else if (this.isStatementOrBlock()) {
const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
return this.unshiftContainer("body", 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?");
}
}
function _containerInsert(from, nodes) {
this.updateSiblingKeys(from, nodes.length);
const paths = [];
this.container.splice(from, 0, ...nodes);
for (let i = 0; i < nodes.length; i++) {
const to = from + i;
const path = this.getSibling(to);
paths.push(path);
if (this.context && this.context.queue) {
path.pushContext(this.context);
}
}
const contexts = this._getQueueContexts();
for (const path of paths) {
path.setScope();
path.debug("Inserted.");
for (const context of contexts) {
context.maybeQueue(path, true);
}
}
return paths;
}
function _containerInsertBefore(nodes) {
return this._containerInsert(this.key, nodes);
}
function _containerInsertAfter(nodes) {
return this._containerInsert(this.key + 1, nodes);
}
function insertAfter(nodes) {
this._assertUnremoved();
nodes = this._verifyNodeList(nodes);
const {
parentPath
} = this;
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
return parentPath.insertAfter(nodes.map(node => {
return t.isExpression(node) ? t.expressionStatement(node) : node;
}));
} else if (this.isNodeType("Expression") && !this.isJSXElement() && !parentPath.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
if (this.node) {
let {
scope
} = this;
if (parentPath.isMethod({
computed: true,
key: this.node
})) {
scope = scope.parent;
}
const temp = scope.generateDeclaredUidIdentifier();
nodes.unshift(t.expressionStatement(t.assignmentExpression("=", t.cloneNode(temp), this.node)));
nodes.push(t.expressionStatement(t.cloneNode(temp)));
}
return this.replaceExpressionWithStatements(nodes);
} else if (Array.isArray(this.container)) {
return this._containerInsertAfter(nodes);
} else if (this.isStatementOrBlock()) {
const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
return this.pushContainer("body", 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?");
}
}
function updateSiblingKeys(fromIndex, incrementBy) {
if (!this.parent) return;
const paths = _cache.path.get(this.parent);
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
if (path.key >= fromIndex) {
path.key += incrementBy;
}
}
}
function _verifyNodeList(nodes) {
if (!nodes) {
return [];
}
if (nodes.constructor !== Array) {
nodes = [nodes];
}
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
let msg;
if (!node) {
msg = "has falsy node";
} else if (typeof node !== "object") {
msg = "contains a non-object node";
} else if (!node.type) {
msg = "without a type";
} else if (node instanceof _index.default) {
msg = "has a NodePath when it expected a raw object";
}
if (msg) {
const type = Array.isArray(node) ? "array" : typeof node;
throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
}
}
return nodes;
}
function unshiftContainer(listKey, nodes) {
this._assertUnremoved();
nodes = this._verifyNodeList(nodes);
const path = _index.default.get({
parentPath: this,
parent: this.node,
container: this.node[listKey],
listKey,
key: 0
});
return path._containerInsertBefore(nodes);
}
function pushContainer(listKey, nodes) {
this._assertUnremoved();
nodes = this._verifyNodeList(nodes);
const container = this.node[listKey];
const path = _index.default.get({
parentPath: this,
parent: this.node,
container: container,
listKey,
key: container.length
});
return path.replaceWithMultiple(nodes);
}
function hoist(scope = this.scope) {
const hoister = new _hoister.default(this, scope);
return hoister.run();
}