(node: Node, callback: (child: Node) => void)
| 29 | * allocating Object.entries() arrays on every call. |
| 30 | */ |
| 31 | export function forEachChild(node: Node, callback: (child: Node) => void) { |
| 32 | const keys = getChildKeys(node) |
| 33 | for (const key of keys) { |
| 34 | const value = (node as any)[key] |
| 35 | if (value === null) continue |
| 36 | if (Array.isArray(value)) { |
| 37 | for (const item of value) { |
| 38 | if (typeof item === 'object' && item !== null && 'type' in item) { |
| 39 | callback(item) |
| 40 | } |
| 41 | } |
| 42 | } else if ('type' in value) { |
| 43 | callback(value as Node) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Recursively walk AST nodes, calling `visitor` for each node with a `type`. |
no test coverage detected