You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.9 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.insertBefore = insertBefore;
  6. exports._containerInsert = _containerInsert;
  7. exports._containerInsertBefore = _containerInsertBefore;
  8. exports._containerInsertAfter = _containerInsertAfter;
  9. exports.insertAfter = insertAfter;
  10. exports.updateSiblingKeys = updateSiblingKeys;
  11. exports._verifyNodeList = _verifyNodeList;
  12. exports.unshiftContainer = unshiftContainer;
  13. exports.pushContainer = pushContainer;
  14. exports.hoist = hoist;
  15. var _cache = require("../cache");
  16. var _hoister = _interopRequireDefault(require("./lib/hoister"));
  17. var _index = _interopRequireDefault(require("./index"));
  18. var t = _interopRequireWildcard(require("@babel/types"));
  19. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  20. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. function insertBefore(nodes) {
  23. this._assertUnremoved();
  24. nodes = this._verifyNodeList(nodes);
  25. const {
  26. parentPath
  27. } = this;
  28. if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
  29. return parentPath.insertBefore(nodes);
  30. } else if (this.isNodeType("Expression") && !this.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
  31. if (this.node) nodes.push(this.node);
  32. return this.replaceExpressionWithStatements(nodes);
  33. } else if (Array.isArray(this.container)) {
  34. return this._containerInsertBefore(nodes);
  35. } else if (this.isStatementOrBlock()) {
  36. const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
  37. this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
  38. return this.unshiftContainer("body", nodes);
  39. } else {
  40. 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?");
  41. }
  42. }
  43. function _containerInsert(from, nodes) {
  44. this.updateSiblingKeys(from, nodes.length);
  45. const paths = [];
  46. this.container.splice(from, 0, ...nodes);
  47. for (let i = 0; i < nodes.length; i++) {
  48. const to = from + i;
  49. const path = this.getSibling(to);
  50. paths.push(path);
  51. if (this.context && this.context.queue) {
  52. path.pushContext(this.context);
  53. }
  54. }
  55. const contexts = this._getQueueContexts();
  56. for (const path of paths) {
  57. path.setScope();
  58. path.debug("Inserted.");
  59. for (const context of contexts) {
  60. context.maybeQueue(path, true);
  61. }
  62. }
  63. return paths;
  64. }
  65. function _containerInsertBefore(nodes) {
  66. return this._containerInsert(this.key, nodes);
  67. }
  68. function _containerInsertAfter(nodes) {
  69. return this._containerInsert(this.key + 1, nodes);
  70. }
  71. function insertAfter(nodes) {
  72. this._assertUnremoved();
  73. nodes = this._verifyNodeList(nodes);
  74. const {
  75. parentPath
  76. } = this;
  77. if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
  78. return parentPath.insertAfter(nodes.map(node => {
  79. return t.isExpression(node) ? t.expressionStatement(node) : node;
  80. }));
  81. } else if (this.isNodeType("Expression") && !this.isJSXElement() && !parentPath.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
  82. if (this.node) {
  83. let {
  84. scope
  85. } = this;
  86. if (parentPath.isMethod({
  87. computed: true,
  88. key: this.node
  89. })) {
  90. scope = scope.parent;
  91. }
  92. const temp = scope.generateDeclaredUidIdentifier();
  93. nodes.unshift(t.expressionStatement(t.assignmentExpression("=", t.cloneNode(temp), this.node)));
  94. nodes.push(t.expressionStatement(t.cloneNode(temp)));
  95. }
  96. return this.replaceExpressionWithStatements(nodes);
  97. } else if (Array.isArray(this.container)) {
  98. return this._containerInsertAfter(nodes);
  99. } else if (this.isStatementOrBlock()) {
  100. const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
  101. this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
  102. return this.pushContainer("body", nodes);
  103. } else {
  104. 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?");
  105. }
  106. }
  107. function updateSiblingKeys(fromIndex, incrementBy) {
  108. if (!this.parent) return;
  109. const paths = _cache.path.get(this.parent);
  110. for (let i = 0; i < paths.length; i++) {
  111. const path = paths[i];
  112. if (path.key >= fromIndex) {
  113. path.key += incrementBy;
  114. }
  115. }
  116. }
  117. function _verifyNodeList(nodes) {
  118. if (!nodes) {
  119. return [];
  120. }
  121. if (nodes.constructor !== Array) {
  122. nodes = [nodes];
  123. }
  124. for (let i = 0; i < nodes.length; i++) {
  125. const node = nodes[i];
  126. let msg;
  127. if (!node) {
  128. msg = "has falsy node";
  129. } else if (typeof node !== "object") {
  130. msg = "contains a non-object node";
  131. } else if (!node.type) {
  132. msg = "without a type";
  133. } else if (node instanceof _index.default) {
  134. msg = "has a NodePath when it expected a raw object";
  135. }
  136. if (msg) {
  137. const type = Array.isArray(node) ? "array" : typeof node;
  138. throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
  139. }
  140. }
  141. return nodes;
  142. }
  143. function unshiftContainer(listKey, nodes) {
  144. this._assertUnremoved();
  145. nodes = this._verifyNodeList(nodes);
  146. const path = _index.default.get({
  147. parentPath: this,
  148. parent: this.node,
  149. container: this.node[listKey],
  150. listKey,
  151. key: 0
  152. });
  153. return path._containerInsertBefore(nodes);
  154. }
  155. function pushContainer(listKey, nodes) {
  156. this._assertUnremoved();
  157. nodes = this._verifyNodeList(nodes);
  158. const container = this.node[listKey];
  159. const path = _index.default.get({
  160. parentPath: this,
  161. parent: this.node,
  162. container: container,
  163. listKey,
  164. key: container.length
  165. });
  166. return path.replaceWithMultiple(nodes);
  167. }
  168. function hoist(scope = this.scope) {
  169. const hoister = new _hoister.default(this, scope);
  170. return hoister.run();
  171. }