replacement.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 _parser = require("@babel/parser");
  16. var _t = require("@babel/types");
  17. var _helperHoistVariables = require("@babel/helper-hoist-variables");
  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 = this._verifyNodeList(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. this._replaceWith(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. (0, _helperHoistVariables.default)(callee.get("body"), id => {
  162. this.scope.push({
  163. id
  164. });
  165. }, "var");
  166. const completionRecords = this.get("callee").getCompletionRecords();
  167. for (const path of completionRecords) {
  168. if (!path.isExpressionStatement()) continue;
  169. const loop = path.findParent(path => path.isLoop());
  170. if (loop) {
  171. let uid = loop.getData("expressionReplacementReturnUid");
  172. if (!uid) {
  173. uid = callee.scope.generateDeclaredUidIdentifier("ret");
  174. callee.get("body").pushContainer("body", returnStatement(cloneNode(uid)));
  175. loop.setData("expressionReplacementReturnUid", uid);
  176. } else {
  177. uid = identifier(uid.name);
  178. }
  179. path.get("expression").replaceWith(assignmentExpression("=", cloneNode(uid), path.node.expression));
  180. } else {
  181. path.replaceWith(returnStatement(path.node.expression));
  182. }
  183. }
  184. callee.arrowFunctionToExpression();
  185. const newCallee = callee;
  186. const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", FUNCTION_TYPES);
  187. const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", FUNCTION_TYPES);
  188. if (needToAwaitFunction) {
  189. newCallee.set("async", true);
  190. if (!needToYieldFunction) {
  191. this.replaceWith(awaitExpression(this.node));
  192. }
  193. }
  194. if (needToYieldFunction) {
  195. newCallee.set("generator", true);
  196. this.replaceWith(yieldExpression(this.node, true));
  197. }
  198. return newCallee.get("body.body");
  199. }
  200. function gatherSequenceExpressions(nodes, declars) {
  201. const exprs = [];
  202. let ensureLastUndefined = true;
  203. for (const node of nodes) {
  204. if (!isEmptyStatement(node)) {
  205. ensureLastUndefined = false;
  206. }
  207. if (isExpression(node)) {
  208. exprs.push(node);
  209. } else if (isExpressionStatement(node)) {
  210. exprs.push(node.expression);
  211. } else if (isVariableDeclaration(node)) {
  212. if (node.kind !== "var") return;
  213. for (const declar of node.declarations) {
  214. const bindings = getBindingIdentifiers(declar);
  215. for (const key of Object.keys(bindings)) {
  216. declars.push(cloneNode(bindings[key]));
  217. }
  218. if (declar.init) {
  219. exprs.push(assignmentExpression("=", declar.id, declar.init));
  220. }
  221. }
  222. ensureLastUndefined = true;
  223. } else if (isIfStatement(node)) {
  224. const consequent = node.consequent ? gatherSequenceExpressions([node.consequent], declars) : buildUndefinedNode();
  225. const alternate = node.alternate ? gatherSequenceExpressions([node.alternate], declars) : buildUndefinedNode();
  226. if (!consequent || !alternate) return;
  227. exprs.push(conditionalExpression(node.test, consequent, alternate));
  228. } else if (isBlockStatement(node)) {
  229. const body = gatherSequenceExpressions(node.body, declars);
  230. if (!body) return;
  231. exprs.push(body);
  232. } else if (isEmptyStatement(node)) {
  233. if (nodes.indexOf(node) === 0) {
  234. ensureLastUndefined = true;
  235. }
  236. } else {
  237. return;
  238. }
  239. }
  240. if (ensureLastUndefined) exprs.push(buildUndefinedNode());
  241. if (exprs.length === 1) {
  242. return exprs[0];
  243. } else {
  244. return sequenceExpression(exprs);
  245. }
  246. }
  247. function replaceInline(nodes) {
  248. this.resync();
  249. if (Array.isArray(nodes)) {
  250. if (Array.isArray(this.container)) {
  251. nodes = this._verifyNodeList(nodes);
  252. const paths = this._containerInsertAfter(nodes);
  253. this.remove();
  254. return paths;
  255. } else {
  256. return this.replaceWithMultiple(nodes);
  257. }
  258. } else {
  259. return this.replaceWith(nodes);
  260. }
  261. }
  262. //# sourceMappingURL=replacement.js.map