MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / goLocalBindings

Function goLocalBindings

src/extraction/function-ref.ts:953–1009  ·  view source on GitHub ↗
(
  units: Iterable<SyntaxNode>,
  names: ReadonlySet<string>,
  source: string
)

Source from the content-addressed store, hash-verified

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

Callers 1

goScopedFnRefScopesFunction · 0.85

Calls 6

getChildByFieldFunction · 0.90
getNodeTextFunction · 0.90
pushMethod · 0.80
bindFunction · 0.70
declaresFunction · 0.70
deleteMethod · 0.65

Tested by

no test coverage detected