replacement.js 8.1 KB

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