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.

286 lines
8.0 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getOpposite = getOpposite;
  6. exports.getCompletionRecords = getCompletionRecords;
  7. exports.getSibling = getSibling;
  8. exports.getPrevSibling = getPrevSibling;
  9. exports.getNextSibling = getNextSibling;
  10. exports.getAllNextSiblings = getAllNextSiblings;
  11. exports.getAllPrevSiblings = getAllPrevSiblings;
  12. exports.get = get;
  13. exports._getKey = _getKey;
  14. exports._getPattern = _getPattern;
  15. exports.getBindingIdentifiers = getBindingIdentifiers;
  16. exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
  17. exports.getBindingIdentifierPaths = getBindingIdentifierPaths;
  18. exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
  19. var _index = _interopRequireDefault(require("./index"));
  20. var t = _interopRequireWildcard(require("@babel/types"));
  21. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  22. 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; }
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. function getOpposite() {
  25. if (this.key === "left") {
  26. return this.getSibling("right");
  27. } else if (this.key === "right") {
  28. return this.getSibling("left");
  29. }
  30. }
  31. function addCompletionRecords(path, paths) {
  32. if (path) return paths.concat(path.getCompletionRecords());
  33. return paths;
  34. }
  35. function completionRecordForSwitch(cases, paths) {
  36. let isLastCaseWithConsequent = true;
  37. for (let i = cases.length - 1; i >= 0; i--) {
  38. const switchCase = cases[i];
  39. const consequent = switchCase.get("consequent");
  40. let breakStatement;
  41. findBreak: for (const statement of consequent) {
  42. if (statement.isBlockStatement()) {
  43. for (const statementInBlock of statement.get("body")) {
  44. if (statementInBlock.isBreakStatement()) {
  45. breakStatement = statementInBlock;
  46. break findBreak;
  47. }
  48. }
  49. } else if (statement.isBreakStatement()) {
  50. breakStatement = statement;
  51. break;
  52. }
  53. }
  54. if (breakStatement) {
  55. while (breakStatement.key === 0 && breakStatement.parentPath.isBlockStatement()) {
  56. breakStatement = breakStatement.parentPath;
  57. }
  58. const prevSibling = breakStatement.getPrevSibling();
  59. if (breakStatement.key > 0 && (prevSibling.isExpressionStatement() || prevSibling.isBlockStatement())) {
  60. paths = addCompletionRecords(prevSibling, paths);
  61. breakStatement.remove();
  62. } else {
  63. breakStatement.replaceWith(breakStatement.scope.buildUndefinedNode());
  64. paths = addCompletionRecords(breakStatement, paths);
  65. }
  66. } else if (isLastCaseWithConsequent) {
  67. const statementFinder = statement => !statement.isBlockStatement() || statement.get("body").some(statementFinder);
  68. const hasConsequent = consequent.some(statementFinder);
  69. if (hasConsequent) {
  70. paths = addCompletionRecords(consequent[consequent.length - 1], paths);
  71. isLastCaseWithConsequent = false;
  72. }
  73. }
  74. }
  75. return paths;
  76. }
  77. function getCompletionRecords() {
  78. let paths = [];
  79. if (this.isIfStatement()) {
  80. paths = addCompletionRecords(this.get("consequent"), paths);
  81. paths = addCompletionRecords(this.get("alternate"), paths);
  82. } else if (this.isDoExpression() || this.isFor() || this.isWhile()) {
  83. paths = addCompletionRecords(this.get("body"), paths);
  84. } else if (this.isProgram() || this.isBlockStatement()) {
  85. paths = addCompletionRecords(this.get("body").pop(), paths);
  86. } else if (this.isFunction()) {
  87. return this.get("body").getCompletionRecords();
  88. } else if (this.isTryStatement()) {
  89. paths = addCompletionRecords(this.get("block"), paths);
  90. paths = addCompletionRecords(this.get("handler"), paths);
  91. } else if (this.isCatchClause()) {
  92. paths = addCompletionRecords(this.get("body"), paths);
  93. } else if (this.isSwitchStatement()) {
  94. paths = completionRecordForSwitch(this.get("cases"), paths);
  95. } else {
  96. paths.push(this);
  97. }
  98. return paths;
  99. }
  100. function getSibling(key) {
  101. return _index.default.get({
  102. parentPath: this.parentPath,
  103. parent: this.parent,
  104. container: this.container,
  105. listKey: this.listKey,
  106. key: key
  107. });
  108. }
  109. function getPrevSibling() {
  110. return this.getSibling(this.key - 1);
  111. }
  112. function getNextSibling() {
  113. return this.getSibling(this.key + 1);
  114. }
  115. function getAllNextSiblings() {
  116. let _key = this.key;
  117. let sibling = this.getSibling(++_key);
  118. const siblings = [];
  119. while (sibling.node) {
  120. siblings.push(sibling);
  121. sibling = this.getSibling(++_key);
  122. }
  123. return siblings;
  124. }
  125. function getAllPrevSiblings() {
  126. let _key = this.key;
  127. let sibling = this.getSibling(--_key);
  128. const siblings = [];
  129. while (sibling.node) {
  130. siblings.push(sibling);
  131. sibling = this.getSibling(--_key);
  132. }
  133. return siblings;
  134. }
  135. function get(key, context) {
  136. if (context === true) context = this.context;
  137. const parts = key.split(".");
  138. if (parts.length === 1) {
  139. return this._getKey(key, context);
  140. } else {
  141. return this._getPattern(parts, context);
  142. }
  143. }
  144. function _getKey(key, context) {
  145. const node = this.node;
  146. const container = node[key];
  147. if (Array.isArray(container)) {
  148. return container.map((_, i) => {
  149. return _index.default.get({
  150. listKey: key,
  151. parentPath: this,
  152. parent: node,
  153. container: container,
  154. key: i
  155. }).setContext(context);
  156. });
  157. } else {
  158. return _index.default.get({
  159. parentPath: this,
  160. parent: node,
  161. container: node,
  162. key: key
  163. }).setContext(context);
  164. }
  165. }
  166. function _getPattern(parts, context) {
  167. let path = this;
  168. for (const part of parts) {
  169. if (part === ".") {
  170. path = path.parentPath;
  171. } else {
  172. if (Array.isArray(path)) {
  173. path = path[part];
  174. } else {
  175. path = path.get(part, context);
  176. }
  177. }
  178. }
  179. return path;
  180. }
  181. function getBindingIdentifiers(duplicates) {
  182. return t.getBindingIdentifiers(this.node, duplicates);
  183. }
  184. function getOuterBindingIdentifiers(duplicates) {
  185. return t.getOuterBindingIdentifiers(this.node, duplicates);
  186. }
  187. function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
  188. const path = this;
  189. let search = [].concat(path);
  190. const ids = Object.create(null);
  191. while (search.length) {
  192. const id = search.shift();
  193. if (!id) continue;
  194. if (!id.node) continue;
  195. const keys = t.getBindingIdentifiers.keys[id.node.type];
  196. if (id.isIdentifier()) {
  197. if (duplicates) {
  198. const _ids = ids[id.node.name] = ids[id.node.name] || [];
  199. _ids.push(id);
  200. } else {
  201. ids[id.node.name] = id;
  202. }
  203. continue;
  204. }
  205. if (id.isExportDeclaration()) {
  206. const declaration = id.get("declaration");
  207. if (declaration.isDeclaration()) {
  208. search.push(declaration);
  209. }
  210. continue;
  211. }
  212. if (outerOnly) {
  213. if (id.isFunctionDeclaration()) {
  214. search.push(id.get("id"));
  215. continue;
  216. }
  217. if (id.isFunctionExpression()) {
  218. continue;
  219. }
  220. }
  221. if (keys) {
  222. for (let i = 0; i < keys.length; i++) {
  223. const key = keys[i];
  224. const child = id.get(key);
  225. if (Array.isArray(child) || child.node) {
  226. search = search.concat(child);
  227. }
  228. }
  229. }
  230. }
  231. return ids;
  232. }
  233. function getOuterBindingIdentifierPaths(duplicates) {
  234. return this.getBindingIdentifierPaths(duplicates, true);
  235. }