modification.js 7.9 KB

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