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