replacement.js 7.3 KB

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