Best-effort name extraction for a declaration node.
(node: any, source: string)
| 70 | |
| 71 | /** Best-effort name extraction for a declaration node. */ |
| 72 | function extractSymbolName(node: any, source: string): string | undefined { |
| 73 | // Most grammars expose a `name` child; some wrap (export_statement → declaration → name). |
| 74 | const nameNode = node.childForFieldName?.('name'); |
| 75 | if (nameNode) return source.slice(nameNode.startIndex, nameNode.endIndex); |
| 76 | // export_statement / decorated_definition: dig one level. |
| 77 | for (let i = 0; i < (node.childCount ?? 0); i++) { |
| 78 | const child = node.child(i); |
| 79 | if (!child) continue; |
| 80 | const inner = child.childForFieldName?.('name'); |
| 81 | if (inner) return source.slice(inner.startIndex, inner.endIndex); |
| 82 | } |
| 83 | // lexical_declaration: `const X = ...` → variable_declarator → name |
| 84 | for (let i = 0; i < (node.childCount ?? 0); i++) { |
| 85 | const child = node.child(i); |
| 86 | if (child?.type === 'variable_declarator') { |
| 87 | const id = child.childForFieldName?.('name'); |
| 88 | if (id) return source.slice(id.startIndex, id.endIndex); |
| 89 | } |
| 90 | } |
| 91 | return undefined; |
| 92 | } |
| 93 | |
| 94 | function mkChunk(rel: string, lines: string[], start: number, end: number, symbol?: string): Chunk { |
| 95 | const text = lines.slice(start, end).join('\n'); |