modification.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.insertBefore = insertBefore;
  6. exports._containerInsert = _containerInsert;
  7. exports._containerInsertBefore = _containerInsertBefore;
  8. exports._containerInsertAfter = _containerInsertAfter;
  9. exports.insertAfter = insertAfter;
  10. exports.updateSiblingKeys = updateSiblingKeys;
  11. exports._verifyNodeList = _verifyNodeList;
  12. exports.unshiftContainer = unshiftContainer;
  13. exports.pushContainer = pushContainer;
  14. exports.hoist = hoist;
  15. var _cache = require("../cache");
  16. var _hoister = _interopRequireDefault(require("./lib/hoister"));
  17. var _index = _interopRequireDefault(require("./index"));
  18. function t() {
  19. var data = _interopRequireWildcard(require("@babel/types"));
  20. t = function t() {
  21. return data;
  22. };
  23. return data;
  24. }
  25. 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; } }
  26. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  27. function insertBefore(nodes) {
  28. this._assertUnremoved();
  29. nodes = this._verifyNodeList(nodes);
  30. if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement() || this.parentPath.isExportNamedDeclaration() || this.parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
  31. return this.parentPath.insertBefore(nodes);
  32. } else if (this.isNodeType("Expression") && this.listKey !== "params" && this.listKey !== "arguments" || this.parentPath.isForStatement() && this.key === "init") {
  33. if (this.node) nodes.push(this.node);
  34. return this.replaceExpressionWithStatements(nodes);
  35. } else if (Array.isArray(this.container)) {
  36. return this._containerInsertBefore(nodes);
  37. } else if (this.isStatementOrBlock()) {
  38. var shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
  39. this.replaceWith(t().blockStatement(shouldInsertCurrentNode ? [this.node] : []));
  40. return this.unshiftContainer("body", nodes);
  41. } else {
  42. throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
  43. }
  44. }
  45. function _containerInsert(from, nodes) {
  46. var _container;
  47. this.updateSiblingKeys(from, nodes.length);
  48. var paths = [];
  49. (_container = this.container).splice.apply(_container, [from, 0].concat(nodes));
  50. for (var i = 0; i < nodes.length; i++) {
  51. var to = from + i;
  52. var path = this.getSibling(to);
  53. paths.push(path);
  54. if (this.context && this.context.queue) {
  55. path.pushContext(this.context);
  56. }
  57. }
  58. var contexts = this._getQueueContexts();
  59. for (var _i = 0; _i < paths.length; _i++) {
  60. var _path = paths[_i];
  61. _path.setScope();
  62. _path.debug("Inserted.");
  63. for (var _iterator = contexts, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  64. var _ref;
  65. if (_isArray) {
  66. if (_i2 >= _iterator.length) break;
  67. _ref = _iterator[_i2++];
  68. } else {
  69. _i2 = _iterator.next();
  70. if (_i2.done) break;
  71. _ref = _i2.value;
  72. }
  73. var context = _ref;
  74. context.maybeQueue(_path, true);
  75. }
  76. }
  77. return paths;
  78. }
  79. function _containerInsertBefore(nodes) {
  80. return this._containerInsert(this.key, nodes);
  81. }
  82. function _containerInsertAfter(nodes) {
  83. return this._containerInsert(this.key + 1, nodes);
  84. }
  85. function insertAfter(nodes) {
  86. this._assertUnremoved();
  87. nodes = this._verifyNodeList(nodes);
  88. if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement() || this.parentPath.isExportNamedDeclaration() || this.parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
  89. return this.parentPath.insertAfter(nodes);
  90. } else if (this.isNodeType("Expression") || this.parentPath.isForStatement() && this.key === "init") {
  91. if (this.node) {
  92. var temp = this.scope.generateDeclaredUidIdentifier();
  93. nodes.unshift(t().expressionStatement(t().assignmentExpression("=", t().cloneNode(temp), this.node)));
  94. nodes.push(t().expressionStatement(t().cloneNode(temp)));
  95. }
  96. return this.replaceExpressionWithStatements(nodes);
  97. } else if (Array.isArray(this.container)) {
  98. return this._containerInsertAfter(nodes);
  99. } else if (this.isStatementOrBlock()) {
  100. var shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
  101. this.replaceWith(t().blockStatement(shouldInsertCurrentNode ? [this.node] : []));
  102. return this.pushContainer("body", nodes);
  103. } else {
  104. throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
  105. }
  106. }
  107. function updateSiblingKeys(fromIndex, incrementBy) {
  108. if (!this.parent) return;
  109. var paths = _cache.path.get(this.parent);
  110. for (var i = 0; i < paths.length; i++) {
  111. var path = paths[i];
  112. if (path.key >= fromIndex) {
  113. path.key += incrementBy;
  114. }
  115. }
  116. }
  117. function _verifyNodeList(nodes) {
  118. if (!nodes) {
  119. return [];
  120. }
  121. if (nodes.constructor !== Array) {
  122. nodes = [nodes];
  123. }
  124. for (var i = 0; i < nodes.length; i++) {
  125. var node = nodes[i];
  126. var msg = void 0;
  127. if (!node) {
  128. msg = "has falsy node";
  129. } else if (typeof node !== "object") {
  130. msg = "contains a non-object node";
  131. } else if (!node.type) {
  132. msg = "without a type";
  133. } else if (node instanceof _index.default) {
  134. msg = "has a NodePath when it expected a raw object";
  135. }
  136. if (msg) {
  137. var type = Array.isArray(node) ? "array" : typeof node;
  138. throw new Error("Node list " + msg + " with the index of " + i + " and type of " + type);
  139. }
  140. }
  141. return nodes;
  142. }
  143. function unshiftContainer(listKey, nodes) {
  144. this._assertUnremoved();
  145. nodes = this._verifyNodeList(nodes);
  146. var path = _index.default.get({
  147. parentPath: this,
  148. parent: this.node,
  149. container: this.node[listKey],
  150. listKey: listKey,
  151. key: 0
  152. });
  153. return path.insertBefore(nodes);
  154. }
  155. function pushContainer(listKey, nodes) {
  156. this._assertUnremoved();
  157. nodes = this._verifyNodeList(nodes);
  158. var container = this.node[listKey];
  159. var path = _index.default.get({
  160. parentPath: this,
  161. parent: this.node,
  162. container: container,
  163. listKey: listKey,
  164. key: container.length
  165. });
  166. return path.replaceWithMultiple(nodes);
  167. }
  168. function hoist(scope) {
  169. if (scope === void 0) {
  170. scope = this.scope;
  171. }
  172. var hoister = new _hoister.default(this, scope);
  173. return hoister.run();
  174. }