modification.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. const self = this;
  115. if (self.node) {
  116. const node = self.node;
  117. let {
  118. scope
  119. } = this;
  120. if (scope.path.isPattern()) {
  121. assertExpression(node);
  122. self.replaceWith(callExpression(arrowFunctionExpression([], node), []));
  123. self.get("callee.body").insertAfter(nodes);
  124. return [self];
  125. }
  126. if (isHiddenInSequenceExpression(self)) {
  127. nodes.unshift(node);
  128. } else if (isCallExpression(node) && isSuper(node.callee)) {
  129. nodes.unshift(node);
  130. nodes.push(thisExpression());
  131. } else if (isAlmostConstantAssignment(node, scope)) {
  132. nodes.unshift(node);
  133. nodes.push(cloneNode(node.left));
  134. } else if (scope.isPure(node, true)) {
  135. nodes.push(node);
  136. } else {
  137. if (parentPath.isMethod({
  138. computed: true,
  139. key: node
  140. })) {
  141. scope = scope.parent;
  142. }
  143. const temp = scope.generateDeclaredUidIdentifier();
  144. nodes.unshift(expressionStatement(assignmentExpression("=", cloneNode(temp), node)));
  145. nodes.push(expressionStatement(cloneNode(temp)));
  146. }
  147. }
  148. return this.replaceExpressionWithStatements(nodes);
  149. } else if (Array.isArray(this.container)) {
  150. return this._containerInsertAfter(nodes);
  151. } else if (this.isStatementOrBlock()) {
  152. const node = this.node;
  153. const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
  154. this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
  155. return this.pushContainer("body", nodes);
  156. } else {
  157. 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?");
  158. }
  159. }
  160. function updateSiblingKeys(fromIndex, incrementBy) {
  161. if (!this.parent) return;
  162. const paths = (0, _cache.getCachedPaths)(this.hub, this.parent) || [];
  163. for (const [, path] of paths) {
  164. if (typeof path.key === "number" && path.key >= fromIndex) {
  165. path.key += incrementBy;
  166. }
  167. }
  168. }
  169. function _verifyNodeList(nodes) {
  170. if (!nodes) {
  171. return [];
  172. }
  173. if (!Array.isArray(nodes)) {
  174. nodes = [nodes];
  175. }
  176. for (let i = 0; i < nodes.length; i++) {
  177. const node = nodes[i];
  178. let msg;
  179. if (!node) {
  180. msg = "has falsy node";
  181. } else if (typeof node !== "object") {
  182. msg = "contains a non-object node";
  183. } else if (!node.type) {
  184. msg = "without a type";
  185. } else if (node instanceof _index.default) {
  186. msg = "has a NodePath when it expected a raw object";
  187. }
  188. if (msg) {
  189. const type = Array.isArray(node) ? "array" : typeof node;
  190. throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
  191. }
  192. }
  193. return nodes;
  194. }
  195. function unshiftContainer(listKey, nodes) {
  196. this._assertUnremoved();
  197. nodes = this._verifyNodeList(nodes);
  198. const path = _index.default.get({
  199. parentPath: this,
  200. parent: this.node,
  201. container: this.node[listKey],
  202. listKey,
  203. key: 0
  204. }).setContext(this.context);
  205. return path._containerInsertBefore(nodes);
  206. }
  207. function pushContainer(listKey, nodes) {
  208. this._assertUnremoved();
  209. const verifiedNodes = this._verifyNodeList(nodes);
  210. const container = this.node[listKey];
  211. const path = _index.default.get({
  212. parentPath: this,
  213. parent: this.node,
  214. container: container,
  215. listKey,
  216. key: container.length
  217. }).setContext(this.context);
  218. return path.replaceWithMultiple(verifiedNodes);
  219. }
  220. function hoist(scope = this.scope) {
  221. const hoister = new _hoister.default(this, scope);
  222. return hoister.run();
  223. }
  224. //# sourceMappingURL=modification.js.map