modification.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 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() ||
  44. 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 this._containerInsertBefore(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",
  56. 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. const to = from + i;
  67. const path = this.getSibling(to);
  68. paths.push(path);
  69. if (this.context && this.context.queue) {
  70. path.pushContext(this.context);
  71. }
  72. }
  73. const contexts = this._getQueueContexts();
  74. for (const path of paths) {
  75. path.setScope();
  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 this._containerInsert(this.key, nodes);
  85. }
  86. function _containerInsertAfter(nodes) {
  87. return this._containerInsert(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. this._assertUnremoved();
  102. if (this.isSequenceExpression()) {
  103. return last(this.get("expressions")).insertAfter(nodes_);
  104. }
  105. const nodes = this._verifyNodeList(nodes_);
  106. const {
  107. parentPath,
  108. parent
  109. } = this;
  110. if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() ||
  111. 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. if (this.node) {
  117. const node = this.node;
  118. let {
  119. scope
  120. } = this;
  121. if (scope.path.isPattern()) {
  122. assertExpression(node);
  123. this.replaceWith(callExpression(arrowFunctionExpression([], node), []));
  124. this.get("callee.body").insertAfter(nodes);
  125. return [this];
  126. }
  127. if (isHiddenInSequenceExpression(this)) {
  128. nodes.unshift(node);
  129. }
  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(
  147. assignmentExpression("=", cloneNode(temp), node)));
  148. nodes.push(expressionStatement(cloneNode(temp)));
  149. }
  150. }
  151. return this.replaceExpressionWithStatements(nodes);
  152. } else if (Array.isArray(this.container)) {
  153. return this._containerInsertAfter(nodes);
  154. } else if (this.isStatementOrBlock()) {
  155. const node = this.node;
  156. const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
  157. this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
  158. return this.pushContainer("body", nodes);
  159. } else {
  160. 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?");
  161. }
  162. }
  163. function updateSiblingKeys(fromIndex, incrementBy) {
  164. if (!this.parent) return;
  165. const paths = _cache.path.get(this.parent);
  166. for (const [, path] of paths) {
  167. if (path.key >= fromIndex) {
  168. path.key += incrementBy;
  169. }
  170. }
  171. }
  172. function _verifyNodeList(nodes) {
  173. if (!nodes) {
  174. return [];
  175. }
  176. if (!Array.isArray(nodes)) {
  177. nodes = [nodes];
  178. }
  179. for (let i = 0; i < nodes.length; i++) {
  180. const node = nodes[i];
  181. let msg;
  182. if (!node) {
  183. msg = "has falsy node";
  184. } else if (typeof node !== "object") {
  185. msg = "contains a non-object node";
  186. } else if (!node.type) {
  187. msg = "without a type";
  188. } else if (node instanceof _index.default) {
  189. msg = "has a NodePath when it expected a raw object";
  190. }
  191. if (msg) {
  192. const type = Array.isArray(node) ? "array" : typeof node;
  193. throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
  194. }
  195. }
  196. return nodes;
  197. }
  198. function unshiftContainer(listKey, nodes) {
  199. this._assertUnremoved();
  200. nodes = this._verifyNodeList(nodes);
  201. const path = _index.default.get({
  202. parentPath: this,
  203. parent: this.node,
  204. container: this.node[listKey],
  205. listKey,
  206. key: 0
  207. }).setContext(this.context);
  208. return path._containerInsertBefore(
  209. nodes);
  210. }
  211. function pushContainer(listKey, nodes) {
  212. this._assertUnremoved();
  213. const verifiedNodes = this._verifyNodeList(
  214. nodes);
  215. const container = this.node[listKey];
  216. const path = _index.default.get({
  217. parentPath: this,
  218. parent: this.node,
  219. container: container,
  220. listKey,
  221. key: container.length
  222. }).setContext(this.context);
  223. return path.replaceWithMultiple(verifiedNodes);
  224. }
  225. function hoist(scope = this.scope) {
  226. const hoister = new _hoister.default(this, scope);
  227. return hoister.run();
  228. }
  229. //# sourceMappingURL=modification.js.map