visitors.js 6.5 KB

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