项目原始demo,不改动
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.
 
 
 
 

202 řádky
4.5 KiB

  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. var data = _interopRequireWildcard(require("@babel/types"));
  17. t = function t() {
  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. var path = this;
  27. while (path = path.parentPath) {
  28. if (callback(path)) return path;
  29. }
  30. return null;
  31. }
  32. function find(callback) {
  33. var 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(function (p) {
  41. return p.isFunction();
  42. });
  43. }
  44. function getStatementParent() {
  45. var path = this;
  46. do {
  47. if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
  48. break;
  49. } else {
  50. path = path.parentPath;
  51. }
  52. } while (path);
  53. if (path && (path.isProgram() || path.isFile())) {
  54. throw new Error("File/Program node, we can't possibly find a statement parent to this");
  55. }
  56. return path;
  57. }
  58. function getEarliestCommonAncestorFrom(paths) {
  59. return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
  60. var earliest;
  61. var keys = t().VISITOR_KEYS[deepest.type];
  62. var _arr = ancestries;
  63. for (var _i = 0; _i < _arr.length; _i++) {
  64. var ancestry = _arr[_i];
  65. var path = ancestry[i + 1];
  66. if (!earliest) {
  67. earliest = path;
  68. continue;
  69. }
  70. if (path.listKey && earliest.listKey === path.listKey) {
  71. if (path.key < earliest.key) {
  72. earliest = path;
  73. continue;
  74. }
  75. }
  76. var earliestKeyIndex = keys.indexOf(earliest.parentKey);
  77. var currentKeyIndex = keys.indexOf(path.parentKey);
  78. if (earliestKeyIndex > currentKeyIndex) {
  79. earliest = path;
  80. }
  81. }
  82. return earliest;
  83. });
  84. }
  85. function getDeepestCommonAncestorFrom(paths, filter) {
  86. var _this = this;
  87. if (!paths.length) {
  88. return this;
  89. }
  90. if (paths.length === 1) {
  91. return paths[0];
  92. }
  93. var minDepth = Infinity;
  94. var lastCommonIndex, lastCommon;
  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. var _arr2 = ancestries;
  109. for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
  110. var ancestry = _arr2[_i2];
  111. if (ancestry[i] !== shouldMatch) {
  112. break depthLoop;
  113. }
  114. }
  115. lastCommonIndex = i;
  116. lastCommon = shouldMatch;
  117. }
  118. if (lastCommon) {
  119. if (filter) {
  120. return filter(lastCommon, lastCommonIndex, ancestries);
  121. } else {
  122. return lastCommon;
  123. }
  124. } else {
  125. throw new Error("Couldn't find intersection");
  126. }
  127. }
  128. function getAncestry() {
  129. var path = this;
  130. var paths = [];
  131. do {
  132. paths.push(path);
  133. } while (path = path.parentPath);
  134. return paths;
  135. }
  136. function isAncestor(maybeDescendant) {
  137. return maybeDescendant.isDescendant(this);
  138. }
  139. function isDescendant(maybeAncestor) {
  140. return !!this.findParent(function (parent) {
  141. return parent === maybeAncestor;
  142. });
  143. }
  144. function inType() {
  145. var path = this;
  146. while (path) {
  147. var _arr3 = arguments;
  148. for (var _i3 = 0; _i3 < _arr3.length; _i3++) {
  149. var type = _arr3[_i3];
  150. if (path.node.type === type) return true;
  151. }
  152. path = path.parentPath;
  153. }
  154. return false;
  155. }