ancestry.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  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. exports.inShadow = inShadow;
  16. var _babelTypes = require("babel-types");
  17. var t = _interopRequireWildcard(_babelTypes);
  18. var _index = require("./index");
  19. var _index2 = _interopRequireDefault(_index);
  20. 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. function findParent(callback) {
  23. var path = this;
  24. while (path = path.parentPath) {
  25. if (callback(path)) return path;
  26. }
  27. return null;
  28. }
  29. function find(callback) {
  30. var path = this;
  31. do {
  32. if (callback(path)) return path;
  33. } while (path = path.parentPath);
  34. return null;
  35. }
  36. function getFunctionParent() {
  37. return this.findParent(function (path) {
  38. return path.isFunction() || path.isProgram();
  39. });
  40. }
  41. function getStatementParent() {
  42. var path = this;
  43. do {
  44. if (Array.isArray(path.container)) {
  45. return path;
  46. }
  47. } while (path = path.parentPath);
  48. }
  49. function getEarliestCommonAncestorFrom(paths) {
  50. return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
  51. var earliest = void 0;
  52. var keys = t.VISITOR_KEYS[deepest.type];
  53. for (var _iterator = ancestries, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  54. var _ref;
  55. if (_isArray) {
  56. if (_i >= _iterator.length) break;
  57. _ref = _iterator[_i++];
  58. } else {
  59. _i = _iterator.next();
  60. if (_i.done) break;
  61. _ref = _i.value;
  62. }
  63. var ancestry = _ref;
  64. var path = ancestry[i + 1];
  65. if (!earliest) {
  66. earliest = path;
  67. continue;
  68. }
  69. if (path.listKey && earliest.listKey === path.listKey) {
  70. if (path.key < earliest.key) {
  71. earliest = path;
  72. continue;
  73. }
  74. }
  75. var earliestKeyIndex = keys.indexOf(earliest.parentKey);
  76. var currentKeyIndex = keys.indexOf(path.parentKey);
  77. if (earliestKeyIndex > currentKeyIndex) {
  78. earliest = path;
  79. }
  80. }
  81. return earliest;
  82. });
  83. }
  84. function getDeepestCommonAncestorFrom(paths, filter) {
  85. var _this = this;
  86. if (!paths.length) {
  87. return this;
  88. }
  89. if (paths.length === 1) {
  90. return paths[0];
  91. }
  92. var minDepth = Infinity;
  93. var lastCommonIndex = void 0,
  94. lastCommon = void 0;
  95. var ancestries = paths.map(function (path) {
  96. var ancestry = [];
  97. do {
  98. ancestry.unshift(path);
  99. } while ((path = path.parentPath) && path !== _this);
  100. if (ancestry.length < minDepth) {
  101. minDepth = ancestry.length;
  102. }
  103. return ancestry;
  104. });
  105. var first = ancestries[0];
  106. depthLoop: for (var i = 0; i < minDepth; i++) {
  107. var shouldMatch = first[i];
  108. for (var _iterator2 = ancestries, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  109. var _ref2;
  110. if (_isArray2) {
  111. if (_i2 >= _iterator2.length) break;
  112. _ref2 = _iterator2[_i2++];
  113. } else {
  114. _i2 = _iterator2.next();
  115. if (_i2.done) break;
  116. _ref2 = _i2.value;
  117. }
  118. var ancestry = _ref2;
  119. if (ancestry[i] !== shouldMatch) {
  120. break depthLoop;
  121. }
  122. }
  123. lastCommonIndex = i;
  124. lastCommon = shouldMatch;
  125. }
  126. if (lastCommon) {
  127. if (filter) {
  128. return filter(lastCommon, lastCommonIndex, ancestries);
  129. } else {
  130. return lastCommon;
  131. }
  132. } else {
  133. throw new Error("Couldn't find intersection");
  134. }
  135. }
  136. function getAncestry() {
  137. var path = this;
  138. var paths = [];
  139. do {
  140. paths.push(path);
  141. } while (path = path.parentPath);
  142. return paths;
  143. }
  144. function isAncestor(maybeDescendant) {
  145. return maybeDescendant.isDescendant(this);
  146. }
  147. function isDescendant(maybeAncestor) {
  148. return !!this.findParent(function (parent) {
  149. return parent === maybeAncestor;
  150. });
  151. }
  152. function inType() {
  153. var path = this;
  154. while (path) {
  155. for (var _iterator3 = arguments, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  156. var _ref3;
  157. if (_isArray3) {
  158. if (_i3 >= _iterator3.length) break;
  159. _ref3 = _iterator3[_i3++];
  160. } else {
  161. _i3 = _iterator3.next();
  162. if (_i3.done) break;
  163. _ref3 = _i3.value;
  164. }
  165. var type = _ref3;
  166. if (path.node.type === type) return true;
  167. }
  168. path = path.parentPath;
  169. }
  170. return false;
  171. }
  172. function inShadow(key) {
  173. var parentFn = this.isFunction() ? this : this.findParent(function (p) {
  174. return p.isFunction();
  175. });
  176. if (!parentFn) return;
  177. if (parentFn.isFunctionExpression() || parentFn.isFunctionDeclaration()) {
  178. var shadow = parentFn.node.shadow;
  179. if (shadow && (!key || shadow[key] !== false)) {
  180. return parentFn;
  181. }
  182. } else if (parentFn.isArrowFunctionExpression()) {
  183. return parentFn;
  184. }
  185. return null;
  186. }