source: trip-planner-front/node_modules/@babel/traverse/lib/path/replacement.js@ fa375fe

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

initial commit

  • Property mode set to 100644
File size: 7.0 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.replaceWithMultiple = replaceWithMultiple;
7exports.replaceWithSourceString = replaceWithSourceString;
8exports.replaceWith = replaceWith;
9exports._replaceWith = _replaceWith;
10exports.replaceExpressionWithStatements = replaceExpressionWithStatements;
11exports.replaceInline = replaceInline;
12
13var _codeFrame = require("@babel/code-frame");
14
15var _index = require("../index");
16
17var _index2 = require("./index");
18
19var _cache = require("../cache");
20
21var _parser = require("@babel/parser");
22
23var _t = require("@babel/types");
24
25var _helperHoistVariables = require("@babel/helper-hoist-variables");
26
27const {
28 FUNCTION_TYPES,
29 arrowFunctionExpression,
30 assignmentExpression,
31 awaitExpression,
32 blockStatement,
33 callExpression,
34 cloneNode,
35 expressionStatement,
36 identifier,
37 inheritLeadingComments,
38 inheritTrailingComments,
39 inheritsComments,
40 isExpression,
41 isProgram,
42 isStatement,
43 removeComments,
44 returnStatement,
45 toSequenceExpression,
46 validate,
47 yieldExpression
48} = _t;
49
50function replaceWithMultiple(nodes) {
51 var _pathCache$get;
52
53 this.resync();
54 nodes = this._verifyNodeList(nodes);
55 inheritLeadingComments(nodes[0], this.node);
56 inheritTrailingComments(nodes[nodes.length - 1], this.node);
57 (_pathCache$get = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get.delete(this.node);
58 this.node = this.container[this.key] = null;
59 const paths = this.insertAfter(nodes);
60
61 if (this.node) {
62 this.requeue();
63 } else {
64 this.remove();
65 }
66
67 return paths;
68}
69
70function replaceWithSourceString(replacement) {
71 this.resync();
72
73 try {
74 replacement = `(${replacement})`;
75 replacement = (0, _parser.parse)(replacement);
76 } catch (err) {
77 const loc = err.loc;
78
79 if (loc) {
80 err.message += " - make sure this is an expression.\n" + (0, _codeFrame.codeFrameColumns)(replacement, {
81 start: {
82 line: loc.line,
83 column: loc.column + 1
84 }
85 });
86 err.code = "BABEL_REPLACE_SOURCE_ERROR";
87 }
88
89 throw err;
90 }
91
92 replacement = replacement.program.body[0].expression;
93
94 _index.default.removeProperties(replacement);
95
96 return this.replaceWith(replacement);
97}
98
99function replaceWith(replacement) {
100 this.resync();
101
102 if (this.removed) {
103 throw new Error("You can't replace this node, we've already removed it");
104 }
105
106 if (replacement instanceof _index2.default) {
107 replacement = replacement.node;
108 }
109
110 if (!replacement) {
111 throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
112 }
113
114 if (this.node === replacement) {
115 return [this];
116 }
117
118 if (this.isProgram() && !isProgram(replacement)) {
119 throw new Error("You can only replace a Program root node with another Program node");
120 }
121
122 if (Array.isArray(replacement)) {
123 throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
124 }
125
126 if (typeof replacement === "string") {
127 throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
128 }
129
130 let nodePath = "";
131
132 if (this.isNodeType("Statement") && isExpression(replacement)) {
133 if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
134 replacement = expressionStatement(replacement);
135 nodePath = "expression";
136 }
137 }
138
139 if (this.isNodeType("Expression") && isStatement(replacement)) {
140 if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
141 return this.replaceExpressionWithStatements([replacement]);
142 }
143 }
144
145 const oldNode = this.node;
146
147 if (oldNode) {
148 inheritsComments(replacement, oldNode);
149 removeComments(oldNode);
150 }
151
152 this._replaceWith(replacement);
153
154 this.type = replacement.type;
155 this.setScope();
156 this.requeue();
157 return [nodePath ? this.get(nodePath) : this];
158}
159
160function _replaceWith(node) {
161 var _pathCache$get2;
162
163 if (!this.container) {
164 throw new ReferenceError("Container is falsy");
165 }
166
167 if (this.inList) {
168 validate(this.parent, this.key, [node]);
169 } else {
170 validate(this.parent, this.key, node);
171 }
172
173 this.debug(`Replace with ${node == null ? void 0 : node.type}`);
174 (_pathCache$get2 = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get2.set(node, this).delete(this.node);
175 this.node = this.container[this.key] = node;
176}
177
178function replaceExpressionWithStatements(nodes) {
179 this.resync();
180 const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);
181
182 if (nodesAsSequenceExpression) {
183 return this.replaceWith(nodesAsSequenceExpression)[0].get("expressions");
184 }
185
186 const functionParent = this.getFunctionParent();
187 const isParentAsync = functionParent == null ? void 0 : functionParent.is("async");
188 const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator");
189 const container = arrowFunctionExpression([], blockStatement(nodes));
190 this.replaceWith(callExpression(container, []));
191 const callee = this.get("callee");
192 (0, _helperHoistVariables.default)(callee.get("body"), id => {
193 this.scope.push({
194 id
195 });
196 }, "var");
197 const completionRecords = this.get("callee").getCompletionRecords();
198
199 for (const path of completionRecords) {
200 if (!path.isExpressionStatement()) continue;
201 const loop = path.findParent(path => path.isLoop());
202
203 if (loop) {
204 let uid = loop.getData("expressionReplacementReturnUid");
205
206 if (!uid) {
207 uid = callee.scope.generateDeclaredUidIdentifier("ret");
208 callee.get("body").pushContainer("body", returnStatement(cloneNode(uid)));
209 loop.setData("expressionReplacementReturnUid", uid);
210 } else {
211 uid = identifier(uid.name);
212 }
213
214 path.get("expression").replaceWith(assignmentExpression("=", cloneNode(uid), path.node.expression));
215 } else {
216 path.replaceWith(returnStatement(path.node.expression));
217 }
218 }
219
220 callee.arrowFunctionToExpression();
221 const newCallee = callee;
222
223 const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", FUNCTION_TYPES);
224
225 const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", FUNCTION_TYPES);
226
227 if (needToAwaitFunction) {
228 newCallee.set("async", true);
229
230 if (!needToYieldFunction) {
231 this.replaceWith(awaitExpression(this.node));
232 }
233 }
234
235 if (needToYieldFunction) {
236 newCallee.set("generator", true);
237 this.replaceWith(yieldExpression(this.node, true));
238 }
239
240 return newCallee.get("body.body");
241}
242
243function replaceInline(nodes) {
244 this.resync();
245
246 if (Array.isArray(nodes)) {
247 if (Array.isArray(this.container)) {
248 nodes = this._verifyNodeList(nodes);
249
250 const paths = this._containerInsertAfter(nodes);
251
252 this.remove();
253 return paths;
254 } else {
255 return this.replaceWithMultiple(nodes);
256 }
257 } else {
258 return this.replaceWith(nodes);
259 }
260}
Note: See TracBrowser for help on using the repository browser.