ancestry.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findParent = findParent;
  6. exports.find = find;
  7. exports.getFunctionParent = getFunctionParent;
  8. exports.getStatementParent = getStatementParent;
  9. exports.getEarliestCommonAncestorFrom = getEarliestCommonAncestorFrom;
  10. exports.getDeepestCommonAncestorFrom = getDeepestCommonAncestorFrom;
  11. exports.getAncestry = getAncestry;
  12. exports.isAncestor = isAncestor;
  13. exports.isDescendant = isDescendant;
  14. exports.inType = inType;
  15. function t() {
  16. const data = _interopRequireWildcard(require("@babel/types"));
  17. t = function () {
  18. return data;
  19. };
  20. return data;
  21. }
  22. var _index = _interopRequireDefault(require("./index"));
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  25. function findParent(callback) {
  26. let path = this;
  27. while (path = path.parentPath) {
  28. if (callback(path)) return path;
  29. }
  30. return null;
  31. }
  32. function find(callback) {
  33. let path = this;
  34. do {
  35. if (callback(path)) return path;
  36. } while (path = path.parentPath);
  37. return null;
  38. }
  39. function getFunctionParent() {
  40. return this.findParent(p => p.isFunction());
  41. }
  42. function getStatementParent() {
  43. let path = this;
  44. do {
  45. if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
  46. break;
  47. } else {
  48. path = path.parentPath;
  49. }
  50. } while (path);
  51. if (path && (path.isProgram() || path.isFile())) {
  52. throw new Error("File/Program node, we can't possibly find a statement parent to this");
  53. }
  54. return path;
  55. }
  56. function getEarliestCommonAncestorFrom(paths) {
  57. return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
  58. let earliest;
  59. const keys = t().VISITOR_KEYS[deepest.type];
  60. for (const ancestry of ancestries) {
  61. const path = ancestry[i + 1];
  62. if (!earliest) {
  63. earliest = path;
  64. continue;
  65. }
  66. if (path.listKey && earliest.listKey === path.listKey) {
  67. if (path.key < earliest.key) {
  68. earliest = path;
  69. continue;
  70. }
  71. }
  72. const earliestKeyIndex = keys.indexOf(earliest.parentKey);
  73. const currentKeyIndex = keys.indexOf(path.parentKey);
  74. if (earliestKeyIndex > currentKeyIndex) {
  75. earliest = path;
  76. }
  77. }
  78. return earliest;
  79. });
  80. }
  81. function getDeepestCommonAncestorFrom(paths, filter) {
  82. if (!paths.length) {
  83. return this;
  84. }
  85. if (paths.length === 1) {
  86. return paths[0];
  87. }
  88. let minDepth = Infinity;
  89. let lastCommonIndex, lastCommon;
  90. const ancestries = paths.map(path => {
  91. const ancestry = [];
  92. do {
  93. ancestry.unshift(path);
  94. } while ((path = path.parentPath) && path !== this);
  95. if (ancestry.length < minDepth) {
  96. minDepth = ancestry.length;
  97. }
  98. return ancestry;
  99. });
  100. const first = ancestries[0];
  101. depthLoop: for (let i = 0; i < minDepth; i++) {
  102. const shouldMatch = first[i];
  103. for (const ancestry of ancestries) {
  104. if (ancestry[i] !== shouldMatch) {
  105. break depthLoop;
  106. }
  107. }
  108. lastCommonIndex = i;
  109. lastCommon = shouldMatch;
  110. }
  111. if (lastCommon) {
  112. if (filter) {
  113. return filter(lastCommon, lastCommonIndex, ancestries);
  114. } else {
  115. return lastCommon;
  116. }
  117. } else {
  118. throw new Error("Couldn't find intersection");
  119. }
  120. }
  121. function getAncestry() {
  122. let path = this;
  123. const paths = [];
  124. do {
  125. paths.push(path);
  126. } while (path = path.parentPath);
  127. return paths;
  128. }
  129. function isAncestor(maybeDescendant) {
  130. return maybeDescendant.isDescendant(this);
  131. }
  132. function isDescendant(maybeAncestor) {
  133. return !!this.findParent(parent => parent === maybeAncestor);
  134. }
  135. function inType() {
  136. let path = this;
  137. while (path) {
  138. for (const type of arguments) {
  139. if (path.node.type === type) return true;
  140. }
  141. path = path.parentPath;
  142. }
  143. return false;
  144. }