replacement.js 8.9 KB

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