modification.js 6.5 KB

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