source: trip-planner-front/node_modules/@babel/traverse/lib/path/modification.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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