First descendant of a given type (breadth-first), or null.
(node: SyntaxNode, type: string)
| 7 | |
| 8 | /** First descendant of a given type (breadth-first), or null. */ |
| 9 | function findDescendant(node: SyntaxNode, type: string): SyntaxNode | null { |
| 10 | const queue: SyntaxNode[] = [...node.namedChildren]; |
| 11 | while (queue.length) { |
| 12 | const n = queue.shift()!; |
| 13 | if (n.type === type) return n; |
| 14 | queue.push(...n.namedChildren); |
| 15 | } |
| 16 | return null; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * If `callNode` is a `require(...)` call, return the module name; otherwise null. |