modification.js 7.6 KB

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