modification.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports._containerInsert = _containerInsert;
  6. exports._containerInsertAfter = _containerInsertAfter;
  7. exports._containerInsertBefore = _containerInsertBefore;
  8. exports._verifyNodeList = _verifyNodeList;
  9. exports.hoist = hoist;
  10. exports.insertAfter = insertAfter;
  11. exports.insertBefore = insertBefore;
  12. exports.pushContainer = pushContainer;
  13. exports.unshiftContainer = unshiftContainer;
  14. exports.updateSiblingKeys = updateSiblingKeys;
  15. var _cache = require("../cache");
  16. var _hoister = require("./lib/hoister");
  17. var _index = require("./index");
  18. var _t = require("@babel/types");
  19. const {
  20. arrowFunctionExpression,
  21. assertExpression,
  22. assignmentExpression,
  23. blockStatement,
  24. callExpression,
  25. cloneNode,
  26. expressionStatement,
  27. isAssignmentExpression,
  28. isCallExpression,
  29. isExportNamedDeclaration,
  30. isExpression,
  31. isIdentifier,
  32. isSequenceExpression,
  33. isSuper,
  34. thisExpression
  35. } = _t;
  36. function insertBefore(nodes_) {
  37. this._assertUnremoved();
  38. const nodes = this._verifyNodeList(nodes_);
  39. const {
  40. parentPath,
  41. parent
  42. } = this;
  43. if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || isExportNamedDeclaration(parent) || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
  44. return parentPath.insertBefore(nodes);
  45. } else if (this.isNodeType("Expression") && !this.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
  46. if (this.node) nodes.push(this.node);
  47. return this.replaceExpressionWithStatements(nodes);
  48. } else if (Array.isArray(this.container)) {
  49. return this._containerInsertBefore(nodes);
  50. } else if (this.isStatementOrBlock()) {
  51. const node = this.node;
  52. const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
  53. this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
  54. return this.unshiftContainer("body", nodes);
  55. } else {
  56. 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?");
  57. }
  58. }
  59. function _containerInsert(from, nodes) {
  60. this.updateSiblingKeys(from, nodes.length);
  61. const paths = [];
  62. this.container.splice(from, 0, ...nodes);
  63. for (let i = 0; i < nodes.length; i++) {
  64. const to = from + i;
  65. const path = this.getSibling(to);
  66. paths.push(path);
  67. if (this.context && this.context.queue) {
  68. path.pushContext(this.context);
  69. }
  70. }
  71. const contexts = this._getQueueContexts();
  72. for (const path of paths) {
  73. path.setScope();
  74. path.debug("Inserted.");
  75. for (const context of contexts) {
  76. context.maybeQueue(path, true);
  77. }
  78. }
  79. return paths;
  80. }
  81. function _containerInsertBefore(nodes) {
  82. return this._containerInsert(this.key, nodes);
  83. }
  84. function _containerInsertAfter(nodes) {
  85. return this._containerInsert(this.key + 1, nodes);
  86. }
  87. const last = arr => arr[arr.length - 1];
  88. function isHiddenInSequenceExpression(path) {
  89. return isSequenceExpression(path.parent) && (last(path.parent.expressions) !== path.node || isHiddenInSequenceExpression(path.parentPath));
  90. }
  91. function isAlmostConstantAssignment(node, scope) {
  92. if (!isAssignmentExpression(node) || !isIdentifier(node.left)) {
  93. return false;
  94. }
  95. const blockScope = scope.getBlockParent();
  96. return blockScope.hasOwnBinding(node.left.name) && blockScope.getOwnBinding(node.left.name).constantViolations.length <= 1;
  97. }
  98. function insertAfter(nodes_) {
  99. this._assertUnremoved();
  100. if (this.isSequenceExpression()) {
  101. return last(this.get("expressions")).insertAfter(nodes_);
  102. }
  103. const nodes = this._verifyNodeList(nodes_);
  104. const {
  105. parentPath,
  106. parent
  107. } = this;
  108. if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || isExportNamedDeclaration(parent) || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
  109. return parentPath.insertAfter(nodes.map(node => {
  110. return isExpression(node) ? expressionStatement(node) : node;
  111. }));
  112. } else if (this.isNodeType("Expression") && !this.isJSXElement() && !parentPath.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
  113. if (this.node) {
  114. const node = this.node;
  115. let {
  116. scope
  117. } = this;
  118. if (scope.path.isPattern()) {
  119. assertExpression(node);
  120. this.replaceWith(callExpression(arrowFunctionExpression([], node), []));
  121. this.get("callee.body").insertAfter(nodes);
  122. return [this];
  123. }
  124. if (isHiddenInSequenceExpression(this)) {
  125. nodes.unshift(node);
  126. } else if (isCallExpression(node) && isSuper(node.callee)) {
  127. nodes.unshift(node);
  128. nodes.push(thisExpression());
  129. } else if (isAlmostConstantAssignment(node, scope)) {
  130. nodes.unshift(node);
  131. nodes.push(cloneNode(node.left));
  132. } else if (scope.isPure(node, true)) {
  133. nodes.push(node);
  134. } else {
  135. if (parentPath.isMethod({
  136. computed: true,
  137. key: node
  138. })) {
  139. scope = scope.parent;
  140. }
  141. const temp = scope.generateDeclaredUidIdentifier();
  142. nodes.unshift(expressionStatement(assignmentExpression("=", cloneNode(temp), node)));
  143. nodes.push(expressionStatement(cloneNode(temp)));
  144. }
  145. }
  146. return this.replaceExpressionWithStatements(nodes);
  147. } else if (Array.isArray(this.container)) {
  148. return this._containerInsertAfter(nodes);
  149. } else if (this.isStatementOrBlock()) {
  150. const node = this.node;
  151. const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
  152. this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
  153. return this.pushContainer("body", nodes);
  154. } else {
  155. 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?");
  156. }
  157. }
  158. function updateSiblingKeys(fromIndex, incrementBy) {
  159. if (!this.parent) return;
  160. const paths = _cache.path.get(this.parent);
  161. for (const [, path] of paths) {
  162. if (path.key >= fromIndex) {
  163. path.key += incrementBy;
  164. }
  165. }
  166. }
  167. function _verifyNodeList(nodes) {
  168. if (!nodes) {
  169. return [];
  170. }
  171. if (!Array.isArray(nodes)) {
  172. nodes = [nodes];
  173. }
  174. for (let i = 0; i < nodes.length; i++) {
  175. const node = nodes[i];
  176. let msg;
  177. if (!node) {
  178. msg = "has falsy node";
  179. } else if (typeof node !== "object") {
  180. msg = "contains a non-object node";
  181. } else if (!node.type) {
  182. msg = "without a type";
  183. } else if (node instanceof _index.default) {
  184. msg = "has a NodePath when it expected a raw object";
  185. }
  186. if (msg) {
  187. const type = Array.isArray(node) ? "array" : typeof node;
  188. throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
  189. }
  190. }
  191. return nodes;
  192. }
  193. function unshiftContainer(listKey, nodes) {
  194. this._assertUnremoved();
  195. nodes = this._verifyNodeList(nodes);
  196. const path = _index.default.get({
  197. parentPath: this,
  198. parent: this.node,
  199. container: this.node[listKey],
  200. listKey,
  201. key: 0
  202. }).setContext(this.context);
  203. return path._containerInsertBefore(nodes);
  204. }
  205. function pushContainer(listKey, nodes) {
  206. this._assertUnremoved();
  207. const verifiedNodes = this._verifyNodeList(nodes);
  208. const container = this.node[listKey];
  209. const path = _index.default.get({
  210. parentPath: this,
  211. parent: this.node,
  212. container: container,
  213. listKey,
  214. key: container.length
  215. }).setContext(this.context);
  216. return path.replaceWithMultiple(verifiedNodes);
  217. }
  218. function hoist(scope = this.scope) {
  219. const hoister = new _hoister.default(this, scope);
  220. return hoister.run();
  221. }
  222. //# sourceMappingURL=modification.js.map