( units: Iterable<SyntaxNode>, names: ReadonlySet<string>, source: string )
| 953 | * enough. Mirrored in the kernel (go.rs `go_local_bindings`). |
| 954 | */ |
| 955 | export function goLocalBindings( |
| 956 | units: Iterable<SyntaxNode>, |
| 957 | names: ReadonlySet<string>, |
| 958 | source: string |
| 959 | ): Set<string> { |
| 960 | const out = new Set<string>(); |
| 961 | for (const unit of units) { |
| 962 | const bind = (id: SyntaxNode | null | undefined): void => { |
| 963 | if (id?.type !== 'identifier') return; |
| 964 | const name = getNodeText(id, source); |
| 965 | if (names.has(name)) out.add(`${unit.id}|${name}`); |
| 966 | }; |
| 967 | const declares = (n: SyntaxNode): boolean => { |
| 968 | for (let i = 0; i < n.childCount; i++) { |
| 969 | const c = n.child(i); |
| 970 | if (c && !c.isNamed && c.type === ':=') return true; |
| 971 | } |
| 972 | return false; |
| 973 | }; |
| 974 | const stack: SyntaxNode[] = [unit]; |
| 975 | while (stack.length > 0) { |
| 976 | const n = stack.pop()!; |
| 977 | switch (n.type) { |
| 978 | case 'short_var_declaration': |
| 979 | for (const c of getChildByField(n, 'left')?.namedChildren ?? []) bind(c); |
| 980 | break; |
| 981 | case 'range_clause': |
| 982 | case 'receive_statement': |
| 983 | if (declares(n)) for (const c of getChildByField(n, 'left')?.namedChildren ?? []) bind(c); |
| 984 | break; |
| 985 | case 'type_switch_statement': |
| 986 | for (const c of getChildByField(n, 'alias')?.namedChildren ?? []) bind(c); |
| 987 | break; |
| 988 | case 'var_spec': |
| 989 | case 'const_spec': |
| 990 | case 'parameter_declaration': |
| 991 | case 'variadic_parameter_declaration': |
| 992 | for (const c of n.childrenForFieldName('name')) bind(c); |
| 993 | break; |
| 994 | } |
| 995 | for (let i = n.namedChildCount - 1; i >= 0; i--) { |
| 996 | const c = n.namedChild(i); |
| 997 | if (c) stack.push(c); |
| 998 | } |
| 999 | } |
| 1000 | for (const field of ['receiver', 'parameters', 'result']) { |
| 1001 | const list = getChildByField(unit, field); |
| 1002 | if (list?.type !== 'parameter_list') continue; |
| 1003 | for (const p of list.namedChildren) { |
| 1004 | if (p.type !== 'parameter_declaration' && p.type !== 'variadic_parameter_declaration') continue; |
| 1005 | for (const c of p.childrenForFieldName('name')) { |
| 1006 | if (c.type === 'identifier') out.delete(`${unit.id}|${getNodeText(c, source)}`); |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | return out; |
| 1012 | } |
no test coverage detected