visitors.js 6.4 KB

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