visitors.js 5.8 KB

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