(ast: unknown)
| 43 | * Track declared variables/parameters to know what identifiers are "local" |
| 44 | */ |
| 45 | export function collectDeclaredIdentifiers(ast: unknown): Set<string> { |
| 46 | const declared = new Set<string>(); |
| 47 | |
| 48 | walkNode(ast, (node) => { |
| 49 | // Variable declarations: const x = ..., let y = ..., var z = ... |
| 50 | if (node.type === 'VariableDeclarator') { |
| 51 | const id = node.id as Record<string, unknown>; |
| 52 | if (id.type === 'Identifier') { |
| 53 | declared.add(id.name as string); |
| 54 | } |
| 55 | // Handle destructuring patterns |
| 56 | if (id.type === 'ObjectPattern') { |
| 57 | collectPatternIdentifiers(id, declared); |
| 58 | } |
| 59 | if (id.type === 'ArrayPattern') { |
| 60 | collectPatternIdentifiers(id, declared); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Function parameters |
| 65 | if ( |
| 66 | node.type === 'ArrowFunctionExpression' || |
| 67 | node.type === 'FunctionExpression' || |
| 68 | node.type === 'FunctionDeclaration' |
| 69 | ) { |
| 70 | const params = node.params as Array<Record<string, unknown>>; |
| 71 | for (const param of params) { |
| 72 | collectPatternIdentifiers(param, declared); |
| 73 | } |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | return declared; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Collect identifiers from destructuring patterns |
no test coverage detected