replacement.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.replaceWithMultiple = replaceWithMultiple;
  6. exports.replaceWithSourceString = replaceWithSourceString;
  7. exports.replaceWith = replaceWith;
  8. exports._replaceWith = _replaceWith;
  9. exports.replaceExpressionWithStatements = replaceExpressionWithStatements;
  10. exports.replaceInline = replaceInline;
  11. var _codeFrame = require("@babel/code-frame");
  12. var _index = require("../index");
  13. var _index2 = require("./index");
  14. var _cache = require("../cache");
  15. var _parser = require("@babel/parser");
  16. var t = require("@babel/types");
  17. const hoistVariablesVisitor = {
  18. Function(path) {
  19. path.skip();
  20. },
  21. VariableDeclaration(path) {
  22. if (path.node.kind !== "var") return;
  23. const bindings = path.getBindingIdentifiers();
  24. for (const key of Object.keys(bindings)) {
  25. path.scope.push({
  26. id: bindings[key]
  27. });
  28. }
  29. const exprs = [];
  30. for (const declar of path.node.declarations) {
  31. if (declar.init) {
  32. exprs.push(t.expressionStatement(t.assignmentExpression("=", declar.id, declar.init)));
  33. }
  34. }
  35. path.replaceWithMultiple(exprs);
  36. }
  37. };
  38. function replaceWithMultiple(nodes) {
  39. var _pathCache$get;
  40. this.resync();
  41. nodes = this._verifyNodeList(nodes);
  42. t.inheritLeadingComments(nodes[0], this.node);
  43. t.inheritTrailingComments(nodes[nodes.length - 1], this.node);
  44. (_pathCache$get = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get.delete(this.node);
  45. this.node = this.container[this.key] = null;
  46. const paths = this.insertAfter(nodes);
  47. if (this.node) {
  48. this.requeue();
  49. } else {
  50. this.remove();
  51. }
  52. return paths;
  53. }
  54. function replaceWithSourceString(replacement) {
  55. this.resync();
  56. try {
  57. replacement = `(${replacement})`;
  58. replacement = (0, _parser.parse)(replacement);
  59. } catch (err) {
  60. const loc = err.loc;
  61. if (loc) {
  62. err.message += " - make sure this is an expression.\n" + (0, _codeFrame.codeFrameColumns)(replacement, {
  63. start: {
  64. line: loc.line,
  65. column: loc.column + 1
  66. }
  67. });
  68. err.code = "BABEL_REPLACE_SOURCE_ERROR";
  69. }
  70. throw err;
  71. }
  72. replacement = replacement.program.body[0].expression;
  73. _index.default.removeProperties(replacement);
  74. return this.replaceWith(replacement);
  75. }
  76. function replaceWith(replacement) {
  77. this.resync();
  78. if (this.removed) {
  79. throw new Error("You can't replace this node, we've already removed it");
  80. }
  81. if (replacement instanceof _index2.default) {
  82. replacement = replacement.node;
  83. }
  84. if (!replacement) {
  85. throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
  86. }
  87. if (this.node === replacement) {
  88. return [this];
  89. }
  90. if (this.isProgram() && !t.isProgram(replacement)) {
  91. throw new Error("You can only replace a Program root node with another Program node");
  92. }
  93. if (Array.isArray(replacement)) {
  94. throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
  95. }
  96. if (typeof replacement === "string") {
  97. throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
  98. }
  99. let nodePath = "";
  100. if (this.isNodeType("Statement") && t.isExpression(replacement)) {
  101. if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
  102. replacement = t.expressionStatement(replacement);
  103. nodePath = "expression";
  104. }
  105. }
  106. if (this.isNodeType("Expression") && t.isStatement(replacement)) {
  107. if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
  108. return this.replaceExpressionWithStatements([replacement]);
  109. }
  110. }
  111. const oldNode = this.node;
  112. if (oldNode) {
  113. t.inheritsComments(replacement, oldNode);
  114. t.removeComments(oldNode);
  115. }
  116. this._replaceWith(replacement);
  117. this.type = replacement.type;
  118. this.setScope();
  119. this.requeue();
  120. return [nodePath ? this.get(nodePath) : this];
  121. }
  122. function _replaceWith(node) {
  123. var _pathCache$get2;
  124. if (!this.container) {
  125. throw new ReferenceError("Container is falsy");
  126. }
  127. if (this.inList) {
  128. t.validate(this.parent, this.key, [node]);
  129. } else {
  130. t.validate(this.parent, this.key, node);
  131. }
  132. this.debug(`Replace with ${node == null ? void 0 : node.type}`);
  133. (_pathCache$get2 = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get2.set(node, this).delete(this.node);
  134. this.node = this.container[this.key] = node;
  135. }
  136. function replaceExpressionWithStatements(nodes) {
  137. this.resync();
  138. const toSequenceExpression = t.toSequenceExpression(nodes, this.scope);
  139. if (toSequenceExpression) {
  140. return this.replaceWith(toSequenceExpression)[0].get("expressions");
  141. }
  142. const functionParent = this.getFunctionParent();
  143. const isParentAsync = functionParent == null ? void 0 : functionParent.is("async");
  144. const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator");
  145. const container = t.arrowFunctionExpression([], t.blockStatement(nodes));
  146. this.replaceWith(t.callExpression(container, []));
  147. this.traverse(hoistVariablesVisitor);
  148. const completionRecords = this.get("callee").getCompletionRecords();
  149. for (const path of completionRecords) {
  150. if (!path.isExpressionStatement()) continue;
  151. const loop = path.findParent(path => path.isLoop());
  152. if (loop) {
  153. let uid = loop.getData("expressionReplacementReturnUid");
  154. if (!uid) {
  155. const callee = this.get("callee");
  156. uid = callee.scope.generateDeclaredUidIdentifier("ret");
  157. callee.get("body").pushContainer("body", t.returnStatement(t.cloneNode(uid)));
  158. loop.setData("expressionReplacementReturnUid", uid);
  159. } else {
  160. uid = t.identifier(uid.name);
  161. }
  162. path.get("expression").replaceWith(t.assignmentExpression("=", t.cloneNode(uid), path.node.expression));
  163. } else {
  164. path.replaceWith(t.returnStatement(path.node.expression));
  165. }
  166. }
  167. const callee = this.get("callee");
  168. callee.arrowFunctionToExpression();
  169. const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", t.FUNCTION_TYPES);
  170. const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", t.FUNCTION_TYPES);
  171. if (needToAwaitFunction) {
  172. callee.set("async", true);
  173. if (!needToYieldFunction) {
  174. this.replaceWith(t.awaitExpression(this.node));
  175. }
  176. }
  177. if (needToYieldFunction) {
  178. callee.set("generator", true);
  179. this.replaceWith(t.yieldExpression(this.node, true));
  180. }
  181. return callee.get("body.body");
  182. }
  183. function replaceInline(nodes) {
  184. this.resync();
  185. if (Array.isArray(nodes)) {
  186. if (Array.isArray(this.container)) {
  187. nodes = this._verifyNodeList(nodes);
  188. const paths = this._containerInsertAfter(nodes);
  189. this.remove();
  190. return paths;
  191. } else {
  192. return this.replaceWithMultiple(nodes);
  193. }
  194. } else {
  195. return this.replaceWith(nodes);
  196. }
  197. }