replacement.js 7.4 KB

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