modification.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. "use strict";
  2. exports.__esModule = true;
  3. var _typeof2 = require("babel-runtime/helpers/typeof");
  4. var _typeof3 = _interopRequireDefault(_typeof2);
  5. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  6. var _getIterator3 = _interopRequireDefault(_getIterator2);
  7. exports.insertBefore = insertBefore;
  8. exports._containerInsert = _containerInsert;
  9. exports._containerInsertBefore = _containerInsertBefore;
  10. exports._containerInsertAfter = _containerInsertAfter;
  11. exports._maybePopFromStatements = _maybePopFromStatements;
  12. exports.insertAfter = insertAfter;
  13. exports.updateSiblingKeys = updateSiblingKeys;
  14. exports._verifyNodeList = _verifyNodeList;
  15. exports.unshiftContainer = unshiftContainer;
  16. exports.pushContainer = pushContainer;
  17. exports.hoist = hoist;
  18. var _cache = require("../cache");
  19. var _hoister = require("./lib/hoister");
  20. var _hoister2 = _interopRequireDefault(_hoister);
  21. var _index = require("./index");
  22. var _index2 = _interopRequireDefault(_index);
  23. var _babelTypes = require("babel-types");
  24. var t = _interopRequireWildcard(_babelTypes);
  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)) 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()) {
  31. return this.parentPath.insertBefore(nodes);
  32. } else if (this.isNodeType("Expression") || this.parentPath.isForStatement() && this.key === "init") {
  33. if (this.node) nodes.push(this.node);
  34. this.replaceExpressionWithStatements(nodes);
  35. } else {
  36. this._maybePopFromStatements(nodes);
  37. if (Array.isArray(this.container)) {
  38. return this._containerInsertBefore(nodes);
  39. } else if (this.isStatementOrBlock()) {
  40. if (this.node) nodes.push(this.node);
  41. this._replaceWith(t.blockStatement(nodes));
  42. } else {
  43. 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?");
  44. }
  45. }
  46. return [this];
  47. }
  48. function _containerInsert(from, nodes) {
  49. this.updateSiblingKeys(from, nodes.length);
  50. var paths = [];
  51. for (var i = 0; i < nodes.length; i++) {
  52. var to = from + i;
  53. var node = nodes[i];
  54. this.container.splice(to, 0, node);
  55. if (this.context) {
  56. var path = this.context.create(this.parent, this.container, to, this.listKey);
  57. if (this.context.queue) path.pushContext(this.context);
  58. paths.push(path);
  59. } else {
  60. paths.push(_index2.default.get({
  61. parentPath: this.parentPath,
  62. parent: this.parent,
  63. container: this.container,
  64. listKey: this.listKey,
  65. key: to
  66. }));
  67. }
  68. }
  69. var contexts = this._getQueueContexts();
  70. for (var _iterator = paths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  71. var _ref;
  72. if (_isArray) {
  73. if (_i >= _iterator.length) break;
  74. _ref = _iterator[_i++];
  75. } else {
  76. _i = _iterator.next();
  77. if (_i.done) break;
  78. _ref = _i.value;
  79. }
  80. var _path = _ref;
  81. _path.setScope();
  82. _path.debug(function () {
  83. return "Inserted.";
  84. });
  85. for (var _iterator2 = contexts, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  86. var _ref2;
  87. if (_isArray2) {
  88. if (_i2 >= _iterator2.length) break;
  89. _ref2 = _iterator2[_i2++];
  90. } else {
  91. _i2 = _iterator2.next();
  92. if (_i2.done) break;
  93. _ref2 = _i2.value;
  94. }
  95. var context = _ref2;
  96. context.maybeQueue(_path, true);
  97. }
  98. }
  99. return paths;
  100. }
  101. function _containerInsertBefore(nodes) {
  102. return this._containerInsert(this.key, nodes);
  103. }
  104. function _containerInsertAfter(nodes) {
  105. return this._containerInsert(this.key + 1, nodes);
  106. }
  107. function _maybePopFromStatements(nodes) {
  108. var last = nodes[nodes.length - 1];
  109. var isIdentifier = t.isIdentifier(last) || t.isExpressionStatement(last) && t.isIdentifier(last.expression);
  110. if (isIdentifier && !this.isCompletionRecord()) {
  111. nodes.pop();
  112. }
  113. }
  114. function insertAfter(nodes) {
  115. this._assertUnremoved();
  116. nodes = this._verifyNodeList(nodes);
  117. if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement()) {
  118. return this.parentPath.insertAfter(nodes);
  119. } else if (this.isNodeType("Expression") || this.parentPath.isForStatement() && this.key === "init") {
  120. if (this.node) {
  121. var temp = this.scope.generateDeclaredUidIdentifier();
  122. nodes.unshift(t.expressionStatement(t.assignmentExpression("=", temp, this.node)));
  123. nodes.push(t.expressionStatement(temp));
  124. }
  125. this.replaceExpressionWithStatements(nodes);
  126. } else {
  127. this._maybePopFromStatements(nodes);
  128. if (Array.isArray(this.container)) {
  129. return this._containerInsertAfter(nodes);
  130. } else if (this.isStatementOrBlock()) {
  131. if (this.node) nodes.unshift(this.node);
  132. this._replaceWith(t.blockStatement(nodes));
  133. } else {
  134. 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?");
  135. }
  136. }
  137. return [this];
  138. }
  139. function updateSiblingKeys(fromIndex, incrementBy) {
  140. if (!this.parent) return;
  141. var paths = _cache.path.get(this.parent);
  142. for (var i = 0; i < paths.length; i++) {
  143. var path = paths[i];
  144. if (path.key >= fromIndex) {
  145. path.key += incrementBy;
  146. }
  147. }
  148. }
  149. function _verifyNodeList(nodes) {
  150. if (!nodes) {
  151. return [];
  152. }
  153. if (nodes.constructor !== Array) {
  154. nodes = [nodes];
  155. }
  156. for (var i = 0; i < nodes.length; i++) {
  157. var node = nodes[i];
  158. var msg = void 0;
  159. if (!node) {
  160. msg = "has falsy node";
  161. } else if ((typeof node === "undefined" ? "undefined" : (0, _typeof3.default)(node)) !== "object") {
  162. msg = "contains a non-object node";
  163. } else if (!node.type) {
  164. msg = "without a type";
  165. } else if (node instanceof _index2.default) {
  166. msg = "has a NodePath when it expected a raw object";
  167. }
  168. if (msg) {
  169. var type = Array.isArray(node) ? "array" : typeof node === "undefined" ? "undefined" : (0, _typeof3.default)(node);
  170. throw new Error("Node list " + msg + " with the index of " + i + " and type of " + type);
  171. }
  172. }
  173. return nodes;
  174. }
  175. function unshiftContainer(listKey, nodes) {
  176. this._assertUnremoved();
  177. nodes = this._verifyNodeList(nodes);
  178. var path = _index2.default.get({
  179. parentPath: this,
  180. parent: this.node,
  181. container: this.node[listKey],
  182. listKey: listKey,
  183. key: 0
  184. });
  185. return path.insertBefore(nodes);
  186. }
  187. function pushContainer(listKey, nodes) {
  188. this._assertUnremoved();
  189. nodes = this._verifyNodeList(nodes);
  190. var container = this.node[listKey];
  191. var path = _index2.default.get({
  192. parentPath: this,
  193. parent: this.node,
  194. container: container,
  195. listKey: listKey,
  196. key: container.length
  197. });
  198. return path.replaceWithMultiple(nodes);
  199. }
  200. function hoist() {
  201. var scope = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.scope;
  202. var hoister = new _hoister2.default(this, scope);
  203. return hoister.run();
  204. }