UNPKG

7.05 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.insertBefore = insertBefore;
7exports._containerInsert = _containerInsert;
8exports._containerInsertBefore = _containerInsertBefore;
9exports._containerInsertAfter = _containerInsertAfter;
10exports.insertAfter = insertAfter;
11exports.updateSiblingKeys = updateSiblingKeys;
12exports._verifyNodeList = _verifyNodeList;
13exports.unshiftContainer = unshiftContainer;
14exports.pushContainer = pushContainer;
15exports.hoist = hoist;
16
17var _cache = require("../cache");
18
19var _hoister = _interopRequireDefault(require("./lib/hoister"));
20
21var _index = _interopRequireDefault(require("./index"));
22
23var t = _interopRequireWildcard(require("@babel/types"));
24
25function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
26
27function _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; }
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31function insertBefore(nodes) {
32 this._assertUnremoved();
33
34 nodes = this._verifyNodeList(nodes);
35 const {
36 parentPath
37 } = this;
38
39 if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
40 return parentPath.insertBefore(nodes);
41 } else if (this.isNodeType("Expression") && !this.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
42 if (this.node) nodes.push(this.node);
43 return this.replaceExpressionWithStatements(nodes);
44 } else if (Array.isArray(this.container)) {
45 return this._containerInsertBefore(nodes);
46 } else if (this.isStatementOrBlock()) {
47 const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
48 this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
49 return this.unshiftContainer("body", nodes);
50 } else {
51 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?");
52 }
53}
54
55function _containerInsert(from, nodes) {
56 this.updateSiblingKeys(from, nodes.length);
57 const paths = [];
58 this.container.splice(from, 0, ...nodes);
59
60 for (let i = 0; i < nodes.length; i++) {
61 const to = from + i;
62 const path = this.getSibling(to);
63 paths.push(path);
64
65 if (this.context && this.context.queue) {
66 path.pushContext(this.context);
67 }
68 }
69
70 const contexts = this._getQueueContexts();
71
72 for (const path of paths) {
73 path.setScope();
74 path.debug("Inserted.");
75
76 for (const context of contexts) {
77 context.maybeQueue(path, true);
78 }
79 }
80
81 return paths;
82}
83
84function _containerInsertBefore(nodes) {
85 return this._containerInsert(this.key, nodes);
86}
87
88function _containerInsertAfter(nodes) {
89 return this._containerInsert(this.key + 1, nodes);
90}
91
92function insertAfter(nodes) {
93 this._assertUnremoved();
94
95 nodes = this._verifyNodeList(nodes);
96 const {
97 parentPath
98 } = this;
99
100 if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
101 return parentPath.insertAfter(nodes.map(node => {
102 return t.isExpression(node) ? t.expressionStatement(node) : node;
103 }));
104 } else if (this.isNodeType("Expression") && !this.isJSXElement() && !parentPath.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
105 if (this.node) {
106 let {
107 scope
108 } = this;
109
110 if (parentPath.isMethod({
111 computed: true,
112 key: this.node
113 })) {
114 scope = scope.parent;
115 }
116
117 const temp = scope.generateDeclaredUidIdentifier();
118 nodes.unshift(t.expressionStatement(t.assignmentExpression("=", t.cloneNode(temp), this.node)));
119 nodes.push(t.expressionStatement(t.cloneNode(temp)));
120 }
121
122 return this.replaceExpressionWithStatements(nodes);
123 } else if (Array.isArray(this.container)) {
124 return this._containerInsertAfter(nodes);
125 } else if (this.isStatementOrBlock()) {
126 const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
127 this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
128 return this.pushContainer("body", nodes);
129 } else {
130 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?");
131 }
132}
133
134function updateSiblingKeys(fromIndex, incrementBy) {
135 if (!this.parent) return;
136
137 const paths = _cache.path.get(this.parent);
138
139 for (let i = 0; i < paths.length; i++) {
140 const path = paths[i];
141
142 if (path.key >= fromIndex) {
143 path.key += incrementBy;
144 }
145 }
146}
147
148function _verifyNodeList(nodes) {
149 if (!nodes) {
150 return [];
151 }
152
153 if (nodes.constructor !== Array) {
154 nodes = [nodes];
155 }
156
157 for (let i = 0; i < nodes.length; i++) {
158 const node = nodes[i];
159 let msg;
160
161 if (!node) {
162 msg = "has falsy node";
163 } else if (typeof node !== "object") {
164 msg = "contains a non-object node";
165 } else if (!node.type) {
166 msg = "without a type";
167 } else if (node instanceof _index.default) {
168 msg = "has a NodePath when it expected a raw object";
169 }
170
171 if (msg) {
172 const type = Array.isArray(node) ? "array" : typeof node;
173 throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
174 }
175 }
176
177 return nodes;
178}
179
180function unshiftContainer(listKey, nodes) {
181 this._assertUnremoved();
182
183 nodes = this._verifyNodeList(nodes);
184
185 const path = _index.default.get({
186 parentPath: this,
187 parent: this.node,
188 container: this.node[listKey],
189 listKey,
190 key: 0
191 });
192
193 return path._containerInsertBefore(nodes);
194}
195
196function pushContainer(listKey, nodes) {
197 this._assertUnremoved();
198
199 nodes = this._verifyNodeList(nodes);
200 const container = this.node[listKey];
201
202 const path = _index.default.get({
203 parentPath: this,
204 parent: this.node,
205 container: container,
206 listKey,
207 key: container.length
208 });
209
210 return path.replaceWithMultiple(nodes);
211}
212
213function hoist(scope = this.scope) {
214 const hoister = new _hoister.default(this, scope);
215 return hoister.run();
216}
\No newline at end of file