modification.js 7.5 KB

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