family.js 8.6 KB

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