visitors.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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(
  79. visitor[nodeType]);
  80. }
  81. return visitor;
  82. }
  83. function verify(visitor) {
  84. if (visitor._verified) return;
  85. if (typeof visitor === "function") {
  86. throw new Error("You passed `traverse()` a function when it expected a visitor object, " + "are you sure you didn't mean `{ enter: Function }`?");
  87. }
  88. for (const nodeType of Object.keys(visitor)) {
  89. if (nodeType === "enter" || nodeType === "exit") {
  90. validateVisitorMethods(nodeType, visitor[nodeType]);
  91. }
  92. if (shouldIgnoreKey(nodeType)) continue;
  93. if (TYPES.indexOf(nodeType) < 0) {
  94. throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type`);
  95. }
  96. const visitors = visitor[nodeType];
  97. if (typeof visitors === "object") {
  98. for (const visitorKey of Object.keys(visitors)) {
  99. if (visitorKey === "enter" || visitorKey === "exit") {
  100. validateVisitorMethods(`${nodeType}.${visitorKey}`, visitors[visitorKey]);
  101. } else {
  102. throw new Error("You passed `traverse()` a visitor object with the property " + `${nodeType} that has the invalid property ${visitorKey}`);
  103. }
  104. }
  105. }
  106. }
  107. visitor._verified = true;
  108. }
  109. function validateVisitorMethods(path, val) {
  110. const fns = [].concat(val);
  111. for (const fn of fns) {
  112. if (typeof fn !== "function") {
  113. throw new TypeError(`Non-function found defined in ${path} with type ${typeof fn}`);
  114. }
  115. }
  116. }
  117. function merge(visitors, states = [], wrapper) {
  118. const rootVisitor = {};
  119. for (let i = 0; i < visitors.length; i++) {
  120. const visitor = visitors[i];
  121. const state = states[i];
  122. explode(visitor);
  123. for (const type of Object.keys(visitor)) {
  124. let visitorType = visitor[type];
  125. if (state || wrapper) {
  126. visitorType = wrapWithStateOrWrapper(visitorType, state, wrapper);
  127. }
  128. const nodeVisitor = rootVisitor[type] || (rootVisitor[type] = {});
  129. mergePair(nodeVisitor, visitorType);
  130. }
  131. }
  132. return rootVisitor;
  133. }
  134. function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
  135. const newVisitor = {};
  136. for (const key of Object.keys(oldVisitor)) {
  137. let fns = oldVisitor[key];
  138. if (!Array.isArray(fns)) continue;
  139. fns = fns.map(function (fn) {
  140. let newFn = fn;
  141. if (state) {
  142. newFn = function (path) {
  143. return fn.call(state, path, state);
  144. };
  145. }
  146. if (wrapper) {
  147. newFn = wrapper(state.key, key, newFn);
  148. }
  149. if (newFn !== fn) {
  150. newFn.toString = () => fn.toString();
  151. }
  152. return newFn;
  153. });
  154. newVisitor[key] = fns;
  155. }
  156. return newVisitor;
  157. }
  158. function ensureEntranceObjects(obj) {
  159. for (const key of Object.keys(obj)) {
  160. if (shouldIgnoreKey(key)) continue;
  161. const fns = obj[key];
  162. if (typeof fns === "function") {
  163. obj[key] = {
  164. enter: fns
  165. };
  166. }
  167. }
  168. }
  169. function ensureCallbackArrays(obj) {
  170. if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter];
  171. if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
  172. }
  173. function wrapCheck(nodeType, fn) {
  174. const newFn = function (path) {
  175. if (path[`is${nodeType}`]()) {
  176. return fn.apply(this, arguments);
  177. }
  178. };
  179. newFn.toString = () => fn.toString();
  180. return newFn;
  181. }
  182. function shouldIgnoreKey(key) {
  183. if (key[0] === "_") return true;
  184. if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
  185. if (key === "denylist" || key === "noScope" || key === "skipKeys" ||
  186. key === "blacklist") {
  187. return true;
  188. }
  189. return false;
  190. }
  191. function mergePair(dest, src) {
  192. for (const key of Object.keys(src)) {
  193. dest[key] = [].concat(dest[key] || [], src[key]);
  194. }
  195. }
  196. //# sourceMappingURL=visitors.js.map