( units: Iterable<SyntaxNode>, names: ReadonlySet<string>, source: string )
| 930 | * enough. Mirrored in the kernel (go.rs `go_local_bindings`). |
| 931 | */ |
| 932 | export function goLocalBindings( |
| 933 | units: Iterable<SyntaxNode>, |
| 934 | names: ReadonlySet<string>, |
| 935 | source: string |
| 936 | ): Set<string> { |
| 937 | const out = new Set<string>(); |
| 938 | for (const unit of units) { |
| 939 | const bind = (id: SyntaxNode | null | undefined): void => { |
| 940 | if (id?.type !== 'identifier') return; |
| 941 | const name = getNodeText(id, source); |
| 942 | if (names.has(name)) out.add(`${unit.id}|${name}`); |
| 943 | }; |
| 944 | const declares = (n: SyntaxNode): boolean => { |
| 945 | for (let i = 0; i < n.childCount; i++) { |
| 946 | const c = n.child(i); |
| 947 | if (c && !c.isNamed && c.type === ':=') return true; |
| 948 | } |
| 949 | return false; |
| 950 | }; |
| 951 | const stack: SyntaxNode[] = [unit]; |
| 952 | while (stack.length > 0) { |
| 953 | const n = stack.pop()!; |
| 954 | switch (n.type) { |
| 955 | case 'short_var_declaration': |
| 956 | for (const c of getChildByField(n, 'left')?.namedChildren ?? []) bind(c); |
| 957 | break; |
| 958 | case 'range_clause': |
| 959 | case 'receive_statement': |
| 960 | if (declares(n)) for (const c of getChildByField(n, 'left')?.namedChildren ?? []) bind(c); |
| 961 | break; |
| 962 | case 'type_switch_statement': |
| 963 | for (const c of getChildByField(n, 'alias')?.namedChildren ?? []) bind(c); |
| 964 | break; |
| 965 | case 'var_spec': |
| 966 | case 'const_spec': |
| 967 | case 'parameter_declaration': |
| 968 | case 'variadic_parameter_declaration': |
| 969 | for (const c of n.childrenForFieldName('name')) bind(c); |
| 970 | break; |
| 971 | } |
| 972 | for (let i = n.namedChildCount - 1; i >= 0; i--) { |
| 973 | const c = n.namedChild(i); |
| 974 | if (c) stack.push(c); |
| 975 | } |
| 976 | } |
| 977 | for (const field of ['receiver', 'parameters', 'result']) { |
| 978 | const list = getChildByField(unit, field); |
| 979 | if (list?.type !== 'parameter_list') continue; |
| 980 | for (const p of list.namedChildren) { |
| 981 | if (p.type !== 'parameter_declaration' && p.type !== 'variadic_parameter_declaration') continue; |
| 982 | for (const c of p.childrenForFieldName('name')) { |
| 983 | if (c.type === 'identifier') out.delete(`${unit.id}|${getNodeText(c, source)}`); |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | return out; |
| 989 | } |
no test coverage detected