modification.js 7.8 KB

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