visit.js 570 B

12345678910111213141516171819202122232425
  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. };