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.

visitors.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.explode = explode;
  6. exports.verify = verify;
  7. exports.merge = merge;
  8. var virtualTypes = require("./path/lib/virtual-types");
  9. var t = require("@babel/types");
  10. function explode(visitor) {
  11. if (visitor._exploded) return visitor;
  12. visitor._exploded = true;
  13. for (const nodeType of Object.keys(visitor)) {
  14. if (shouldIgnoreKey(nodeType)) continue;
  15. const parts = nodeType.split("|");
  16. if (parts.length === 1) continue;
  17. const fns = visitor[nodeType];
  18. delete visitor[nodeType];
  19. for (const part of parts) {
  20. visitor[part] = fns;
  21. }
  22. }
  23. verify(visitor);
  24. delete visitor.__esModule;
  25. ensureEntranceObjects(visitor);
  26. ensureCallbackArrays(visitor);
  27. for (const nodeType of Object.keys(visitor)) {
  28. if (shouldIgnoreKey(nodeType)) continue;
  29. const wrapper = virtualTypes[nodeType];
  30. if (!wrapper) continue;
  31. const fns = visitor[nodeType];
  32. for (const type of Object.keys(fns)) {
  33. fns[type] = wrapCheck(wrapper, fns[type]);
  34. }
  35. delete visitor[nodeType];
  36. if (wrapper.types) {
  37. for (const type of wrapper.types) {
  38. if (visitor[type]) {
  39. mergePair(visitor[type], fns);
  40. } else {
  41. visitor[type] = fns;
  42. }
  43. }
  44. } else {
  45. mergePair(visitor, fns);
  46. }
  47. }
  48. for (const nodeType of Object.keys(visitor)) {
  49. if (shouldIgnoreKey(nodeType)) continue;
  50. const fns = visitor[nodeType];
  51. let aliases = t.FLIPPED_ALIAS_KEYS[nodeType];
  52. const deprecatedKey = t.DEPRECATED_KEYS[nodeType];
  53. if (deprecatedKey) {
  54. console.trace(`Visitor defined for ${nodeType} but it has been renamed to ${deprecatedKey}`);
  55. aliases = [deprecatedKey];
  56. }
  57. if (!aliases) continue;
  58. delete visitor[nodeType];
  59. for (const alias of aliases) {
  60. const existing = visitor[alias];
  61. if (existing) {
  62. mergePair(existing, fns);
  63. } else {
  64. visitor[alias] = Object.assign({}, fns);
  65. }
  66. }
  67. }
  68. for (const nodeType of Object.keys(visitor)) {
  69. if (shouldIgnoreKey(nodeType)) continue;
  70. ensureCallbackArrays(visitor[nodeType]);
  71. }
  72. return visitor;
  73. }
  74. function verify(visitor) {
  75. if (visitor._verified) return;
  76. if (typeof visitor === "function") {
  77. throw new Error("You passed `traverse()` a function when it expected a visitor object, " + "are you sure you didn't mean `{ enter: Function }`?");
  78. }
  79. for (const nodeType of Object.keys(visitor)) {
  80. if (nodeType === "enter" || nodeType === "exit") {
  81. validateVisitorMethods(nodeType, visitor[nodeType]);
  82. }
  83. if (shouldIgnoreKey(nodeType)) continue;
  84. if (t.TYPES.indexOf(nodeType) < 0) {
  85. throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type`);
  86. }
  87. const visitors = visitor[nodeType];
  88. if (typeof visitors === "object") {
  89. for (const visitorKey of Object.keys(visitors)) {
  90. if (visitorKey === "enter" || visitorKey === "exit") {
  91. validateVisitorMethods(`${nodeType}.${visitorKey}`, visitors[visitorKey]);
  92. } else {
  93. throw new Error("You passed `traverse()` a visitor object with the property " + `${nodeType} that has the invalid property ${visitorKey}`);
  94. }
  95. }
  96. }
  97. }
  98. visitor._verified = true;
  99. }
  100. function validateVisitorMethods(path, val) {
  101. const fns = [].concat(val);
  102. for (const fn of fns) {
  103. if (typeof fn !== "function") {
  104. throw new TypeError(`Non-function found defined in ${path} with type ${typeof fn}`);
  105. }
  106. }
  107. }
  108. function merge(visitors, states = [], wrapper) {
  109. const rootVisitor = {};
  110. for (let i = 0; i < visitors.length; i++) {
  111. const visitor = visitors[i];
  112. const state = states[i];
  113. explode(visitor);
  114. for (const type of Object.keys(visitor)) {
  115. let visitorType = visitor[type];
  116. if (state || wrapper) {
  117. visitorType = wrapWithStateOrWrapper(visitorType, state, wrapper);
  118. }
  119. const nodeVisitor = rootVisitor[type] = rootVisitor[type] || {};
  120. mergePair(nodeVisitor, visitorType);
  121. }
  122. }
  123. return rootVisitor;
  124. }
  125. function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
  126. const newVisitor = {};
  127. for (const key of Object.keys(oldVisitor)) {
  128. let fns = oldVisitor[key];
  129. if (!Array.isArray(fns)) continue;
  130. fns = fns.map(function (fn) {
  131. let newFn = fn;
  132. if (state) {
  133. newFn = function (path) {
  134. return fn.call(state, path, state);
  135. };
  136. }
  137. if (wrapper) {
  138. newFn = wrapper(state.key, key, newFn);
  139. }
  140. if (newFn !== fn) {
  141. newFn.toString = () => fn.toString();
  142. }
  143. return newFn;
  144. });
  145. newVisitor[key] = fns;
  146. }
  147. return newVisitor;
  148. }
  149. function ensureEntranceObjects(obj) {
  150. for (const key of Object.keys(obj)) {
  151. if (shouldIgnoreKey(key)) continue;
  152. const fns = obj[key];
  153. if (typeof fns === "function") {
  154. obj[key] = {
  155. enter: fns
  156. };
  157. }
  158. }
  159. }
  160. function ensureCallbackArrays(obj) {
  161. if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter];
  162. if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
  163. }
  164. function wrapCheck(wrapper, fn) {
  165. const newFn = function (path) {
  166. if (wrapper.checkPath(path)) {
  167. return fn.apply(this, arguments);
  168. }
  169. };
  170. newFn.toString = () => fn.toString();
  171. return newFn;
  172. }
  173. function shouldIgnoreKey(key) {
  174. if (key[0] === "_") return true;
  175. if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
  176. if (key === "denylist" || key === "noScope" || key === "skipKeys" || key === "blacklist") {
  177. return true;
  178. }
  179. return false;
  180. }
  181. function mergePair(dest, src) {
  182. for (const key of Object.keys(src)) {
  183. dest[key] = [].concat(dest[key] || [], src[key]);
  184. }
  185. }