modification.js 7.5 KB

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