source: frontend/node_modules/@babel/traverse/lib/path/replacement.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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