visitors.js 5.7 KB

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