visit.js 569 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.default = function visit(node, keys, visitorSpec) {
  4. if (!node || !keys) {
  5. return;
  6. }
  7. const type = node.type;
  8. if (typeof visitorSpec[type] === 'function') {
  9. visitorSpec[type](node);
  10. }
  11. const childFields = keys[type];
  12. if (!childFields) {
  13. return;
  14. }
  15. childFields.forEach((fieldName) => {
  16. [].concat(node[fieldName]).forEach((item) => {
  17. visit(item, keys, visitorSpec);
  18. });
  19. });
  20. if (typeof visitorSpec[`${type}:Exit`] === 'function') {
  21. visitorSpec[`${type}:Exit`](node);
  22. }
  23. };