Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ancestry.js 3.6KB

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