visitors.js 6.6 KB

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