visitors.js 6.1 KB

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