family.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 findBreak(statements) {
  36. let breakStatement;
  37. if (!Array.isArray(statements)) {
  38. statements = [statements];
  39. }
  40. for (const statement of statements) {
  41. if (statement.isDoExpression() || statement.isProgram() || statement.isBlockStatement() || statement.isCatchClause() || statement.isLabeledStatement()) {
  42. breakStatement = findBreak(statement.get("body"));
  43. } else if (statement.isIfStatement()) {
  44. var _findBreak;
  45. breakStatement = (_findBreak = findBreak(statement.get("consequent"))) != null ? _findBreak : findBreak(statement.get("alternate"));
  46. } else if (statement.isTryStatement()) {
  47. var _findBreak2;
  48. breakStatement = (_findBreak2 = findBreak(statement.get("block"))) != null ? _findBreak2 : findBreak(statement.get("handler"));
  49. } else if (statement.isBreakStatement()) {
  50. breakStatement = statement;
  51. }
  52. if (breakStatement) {
  53. return breakStatement;
  54. }
  55. }
  56. return null;
  57. }
  58. function completionRecordForSwitch(cases, paths) {
  59. let isLastCaseWithConsequent = true;
  60. for (let i = cases.length - 1; i >= 0; i--) {
  61. const switchCase = cases[i];
  62. const consequent = switchCase.get("consequent");
  63. let breakStatement = findBreak(consequent);
  64. if (breakStatement) {
  65. while (breakStatement.key === 0 && breakStatement.parentPath.isBlockStatement()) {
  66. breakStatement = breakStatement.parentPath;
  67. }
  68. const prevSibling = breakStatement.getPrevSibling();
  69. if (breakStatement.key > 0 && (prevSibling.isExpressionStatement() || prevSibling.isBlockStatement())) {
  70. paths = addCompletionRecords(prevSibling, paths);
  71. breakStatement.remove();
  72. } else {
  73. breakStatement.replaceWith(breakStatement.scope.buildUndefinedNode());
  74. paths = addCompletionRecords(breakStatement, paths);
  75. }
  76. } else if (isLastCaseWithConsequent) {
  77. const statementFinder = statement => !statement.isBlockStatement() || statement.get("body").some(statementFinder);
  78. const hasConsequent = consequent.some(statementFinder);
  79. if (hasConsequent) {
  80. paths = addCompletionRecords(consequent[consequent.length - 1], paths);
  81. isLastCaseWithConsequent = false;
  82. }
  83. }
  84. }
  85. return paths;
  86. }
  87. function getCompletionRecords() {
  88. let paths = [];
  89. if (this.isIfStatement()) {
  90. paths = addCompletionRecords(this.get("consequent"), paths);
  91. paths = addCompletionRecords(this.get("alternate"), paths);
  92. } else if (this.isDoExpression() || this.isFor() || this.isWhile()) {
  93. paths = addCompletionRecords(this.get("body"), paths);
  94. } else if (this.isProgram() || this.isBlockStatement()) {
  95. paths = addCompletionRecords(this.get("body").pop(), paths);
  96. } else if (this.isFunction()) {
  97. return this.get("body").getCompletionRecords();
  98. } else if (this.isTryStatement()) {
  99. paths = addCompletionRecords(this.get("block"), paths);
  100. paths = addCompletionRecords(this.get("handler"), paths);
  101. } else if (this.isCatchClause()) {
  102. paths = addCompletionRecords(this.get("body"), paths);
  103. } else if (this.isSwitchStatement()) {
  104. paths = completionRecordForSwitch(this.get("cases"), paths);
  105. } else {
  106. paths.push(this);
  107. }
  108. return paths;
  109. }
  110. function getSibling(key) {
  111. return _index.default.get({
  112. parentPath: this.parentPath,
  113. parent: this.parent,
  114. container: this.container,
  115. listKey: this.listKey,
  116. key: key
  117. }).setContext(this.context);
  118. }
  119. function getPrevSibling() {
  120. return this.getSibling(this.key - 1);
  121. }
  122. function getNextSibling() {
  123. return this.getSibling(this.key + 1);
  124. }
  125. function getAllNextSiblings() {
  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 getAllPrevSiblings() {
  136. let _key = this.key;
  137. let sibling = this.getSibling(--_key);
  138. const siblings = [];
  139. while (sibling.node) {
  140. siblings.push(sibling);
  141. sibling = this.getSibling(--_key);
  142. }
  143. return siblings;
  144. }
  145. function get(key, context = true) {
  146. if (context === true) context = this.context;
  147. const parts = key.split(".");
  148. if (parts.length === 1) {
  149. return this._getKey(key, context);
  150. } else {
  151. return this._getPattern(parts, context);
  152. }
  153. }
  154. function _getKey(key, context) {
  155. const node = this.node;
  156. const container = node[key];
  157. if (Array.isArray(container)) {
  158. return container.map((_, i) => {
  159. return _index.default.get({
  160. listKey: key,
  161. parentPath: this,
  162. parent: node,
  163. container: container,
  164. key: i
  165. }).setContext(context);
  166. });
  167. } else {
  168. return _index.default.get({
  169. parentPath: this,
  170. parent: node,
  171. container: node,
  172. key: key
  173. }).setContext(context);
  174. }
  175. }
  176. function _getPattern(parts, context) {
  177. let path = this;
  178. for (const part of parts) {
  179. if (part === ".") {
  180. path = path.parentPath;
  181. } else {
  182. if (Array.isArray(path)) {
  183. path = path[part];
  184. } else {
  185. path = path.get(part, context);
  186. }
  187. }
  188. }
  189. return path;
  190. }
  191. function getBindingIdentifiers(duplicates) {
  192. return t.getBindingIdentifiers(this.node, duplicates);
  193. }
  194. function getOuterBindingIdentifiers(duplicates) {
  195. return t.getOuterBindingIdentifiers(this.node, duplicates);
  196. }
  197. function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
  198. const path = this;
  199. let search = [].concat(path);
  200. const ids = Object.create(null);
  201. while (search.length) {
  202. const id = search.shift();
  203. if (!id) continue;
  204. if (!id.node) continue;
  205. const keys = t.getBindingIdentifiers.keys[id.node.type];
  206. if (id.isIdentifier()) {
  207. if (duplicates) {
  208. const _ids = ids[id.node.name] = ids[id.node.name] || [];
  209. _ids.push(id);
  210. } else {
  211. ids[id.node.name] = id;
  212. }
  213. continue;
  214. }
  215. if (id.isExportDeclaration()) {
  216. const declaration = id.get("declaration");
  217. if (declaration.isDeclaration()) {
  218. search.push(declaration);
  219. }
  220. continue;
  221. }
  222. if (outerOnly) {
  223. if (id.isFunctionDeclaration()) {
  224. search.push(id.get("id"));
  225. continue;
  226. }
  227. if (id.isFunctionExpression()) {
  228. continue;
  229. }
  230. }
  231. if (keys) {
  232. for (let i = 0; i < keys.length; i++) {
  233. const key = keys[i];
  234. const child = id.get(key);
  235. if (Array.isArray(child) || child.node) {
  236. search = search.concat(child);
  237. }
  238. }
  239. }
  240. }
  241. return ids;
  242. }
  243. function getOuterBindingIdentifierPaths(duplicates) {
  244. return this.getBindingIdentifierPaths(duplicates, true);
  245. }