modification.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. var t = _interopRequireWildcard(require("@babel/types"));
  19. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  20. 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; }
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. function insertBefore(nodes_) {
  23. this._assertUnremoved();
  24. const nodes = this._verifyNodeList(nodes_);
  25. const {
  26. parentPath
  27. } = this;
  28. if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
  29. return parentPath.insertBefore(nodes);
  30. } else if (this.isNodeType("Expression") && !this.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
  31. if (this.node) nodes.push(this.node);
  32. return this.replaceExpressionWithStatements(nodes);
  33. } else if (Array.isArray(this.container)) {
  34. return this._containerInsertBefore(nodes);
  35. } else if (this.isStatementOrBlock()) {
  36. const node = this.node;
  37. const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
  38. this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [node] : []));
  39. return this.unshiftContainer("body", nodes);
  40. } else {
  41. 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?");
  42. }
  43. }
  44. function _containerInsert(from, nodes) {
  45. this.updateSiblingKeys(from, nodes.length);
  46. const paths = [];
  47. this.container.splice(from, 0, ...nodes);
  48. for (let i = 0; i < nodes.length; i++) {
  49. const to = from + i;
  50. const path = this.getSibling(to);
  51. paths.push(path);
  52. if (this.context && this.context.queue) {
  53. path.pushContext(this.context);
  54. }
  55. }
  56. const contexts = this._getQueueContexts();
  57. for (const path of paths) {
  58. path.setScope();
  59. path.debug("Inserted.");
  60. for (const context of contexts) {
  61. context.maybeQueue(path, true);
  62. }
  63. }
  64. return paths;
  65. }
  66. function _containerInsertBefore(nodes) {
  67. return this._containerInsert(this.key, nodes);
  68. }
  69. function _containerInsertAfter(nodes) {
  70. return this._containerInsert(this.key + 1, nodes);
  71. }
  72. function insertAfter(nodes_) {
  73. this._assertUnremoved();
  74. const nodes = this._verifyNodeList(nodes_);
  75. const {
  76. parentPath
  77. } = this;
  78. if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
  79. return parentPath.insertAfter(nodes.map(node => {
  80. return t.isExpression(node) ? t.expressionStatement(node) : node;
  81. }));
  82. } else if (this.isNodeType("Expression") && !this.isJSXElement() && !parentPath.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
  83. if (this.node) {
  84. const node = this.node;
  85. let {
  86. scope
  87. } = this;
  88. if (scope.path.isPattern()) {
  89. t.assertExpression(node);
  90. this.replaceWith(t.callExpression(t.arrowFunctionExpression([], node), []));
  91. this.get("callee.body").insertAfter(nodes);
  92. return [this];
  93. }
  94. if (parentPath.isMethod({
  95. computed: true,
  96. key: node
  97. })) {
  98. scope = scope.parent;
  99. }
  100. const temp = scope.generateDeclaredUidIdentifier();
  101. nodes.unshift(t.expressionStatement(t.assignmentExpression("=", t.cloneNode(temp), node)));
  102. nodes.push(t.expressionStatement(t.cloneNode(temp)));
  103. }
  104. return this.replaceExpressionWithStatements(nodes);
  105. } else if (Array.isArray(this.container)) {
  106. return this._containerInsertAfter(nodes);
  107. } else if (this.isStatementOrBlock()) {
  108. const node = this.node;
  109. const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
  110. this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [node] : []));
  111. return this.pushContainer("body", nodes);
  112. } else {
  113. 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?");
  114. }
  115. }
  116. function updateSiblingKeys(fromIndex, incrementBy) {
  117. if (!this.parent) return;
  118. const paths = _cache.path.get(this.parent);
  119. for (const [, path] of paths) {
  120. if (path.key >= fromIndex) {
  121. path.key += incrementBy;
  122. }
  123. }
  124. }
  125. function _verifyNodeList(nodes) {
  126. if (!nodes) {
  127. return [];
  128. }
  129. if (!Array.isArray(nodes)) {
  130. nodes = [nodes];
  131. }
  132. for (let i = 0; i < nodes.length; i++) {
  133. const node = nodes[i];
  134. let msg;
  135. if (!node) {
  136. msg = "has falsy node";
  137. } else if (typeof node !== "object") {
  138. msg = "contains a non-object node";
  139. } else if (!node.type) {
  140. msg = "without a type";
  141. } else if (node instanceof _index.default) {
  142. msg = "has a NodePath when it expected a raw object";
  143. }
  144. if (msg) {
  145. const type = Array.isArray(node) ? "array" : typeof node;
  146. throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
  147. }
  148. }
  149. return nodes;
  150. }
  151. function unshiftContainer(listKey, nodes) {
  152. this._assertUnremoved();
  153. nodes = this._verifyNodeList(nodes);
  154. const path = _index.default.get({
  155. parentPath: this,
  156. parent: this.node,
  157. container: this.node[listKey],
  158. listKey,
  159. key: 0
  160. }).setContext(this.context);
  161. return path._containerInsertBefore(nodes);
  162. }
  163. function pushContainer(listKey, nodes) {
  164. this._assertUnremoved();
  165. const verifiedNodes = this._verifyNodeList(nodes);
  166. const container = this.node[listKey];
  167. const path = _index.default.get({
  168. parentPath: this,
  169. parent: this.node,
  170. container: container,
  171. listKey,
  172. key: container.length
  173. }).setContext(this.context);
  174. return path.replaceWithMultiple(verifiedNodes);
  175. }
  176. function hoist(scope = this.scope) {
  177. const hoister = new _hoister.default(this, scope);
  178. return hoister.run();
  179. }