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