1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.insertBefore = insertBefore;
|
7 | exports._containerInsert = _containerInsert;
|
8 | exports._containerInsertBefore = _containerInsertBefore;
|
9 | exports._containerInsertAfter = _containerInsertAfter;
|
10 | exports.insertAfter = insertAfter;
|
11 | exports.updateSiblingKeys = updateSiblingKeys;
|
12 | exports._verifyNodeList = _verifyNodeList;
|
13 | exports.unshiftContainer = unshiftContainer;
|
14 | exports.pushContainer = pushContainer;
|
15 | exports.hoist = hoist;
|
16 |
|
17 | var _cache = require("../cache");
|
18 |
|
19 | var _hoister = _interopRequireDefault(require("./lib/hoister"));
|
20 |
|
21 | var _index = _interopRequireDefault(require("./index"));
|
22 |
|
23 | var t = _interopRequireWildcard(require("@babel/types"));
|
24 |
|
25 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
26 |
|
27 | 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; }
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 |
|
31 | function 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 |
|
55 | function _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 |
|
84 | function _containerInsertBefore(nodes) {
|
85 | return this._containerInsert(this.key, nodes);
|
86 | }
|
87 |
|
88 | function _containerInsertAfter(nodes) {
|
89 | return this._containerInsert(this.key + 1, nodes);
|
90 | }
|
91 |
|
92 | function 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 |
|
134 | function 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 |
|
148 | function _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 |
|
180 | function 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 |
|
196 | function 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 |
|
213 | function hoist(scope = this.scope) {
|
214 | const hoister = new _hoister.default(this, scope);
|
215 | return hoister.run();
|
216 | } |
\ | No newline at end of file |