mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-09-29 01:41:42 +00:00
fix(go): implement enclosing function scope lookup for range bindings
Replace the V1 stub (always return null, using module scope as fallback) with actual AST parent walk to find the enclosing function_declaration or method_declaration and resolve its scope by matching start position. Range loop variables now bind to the correct function scope instead of polluting the module scope. Closes #1239.
This commit is contained in:
parent
d5870b6bdf
commit
f2e8ce8f53
1 changed files with 26 additions and 4 deletions
|
|
@ -93,10 +93,32 @@ function extractElementType(binding: TypeRef): string | null {
|
|||
}
|
||||
|
||||
function findEnclosingFunctionScope(
|
||||
_node: unknown,
|
||||
_scopeMap: ReadonlyMap<string, Scope>,
|
||||
node: unknown,
|
||||
scopeMap: ReadonlyMap<string, Scope>,
|
||||
): Scope | null {
|
||||
// V1 simplified: return null, use module scope as fallback.
|
||||
// TODO(#1239): walk up tree-sitter AST to find enclosing func/method declaration.
|
||||
const tsNode = node as {
|
||||
readonly parent: unknown;
|
||||
readonly type: string;
|
||||
readonly startPosition: { readonly row: number; readonly column: number };
|
||||
};
|
||||
// Walk up the AST to find the enclosing function or method declaration.
|
||||
let current: typeof tsNode | null = tsNode;
|
||||
while (current !== null) {
|
||||
if (current.type === 'function_declaration' || current.type === 'method_declaration') {
|
||||
// Match by source position: the scope whose range starts at the
|
||||
// same line/column as the tree-sitter node.
|
||||
for (const scope of scopeMap.values()) {
|
||||
if (
|
||||
scope.kind === 'Function' &&
|
||||
scope.range.startLine === current.startPosition.row &&
|
||||
scope.range.startCol === current.startPosition.column
|
||||
) {
|
||||
return scope;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
current = (current.parent as typeof tsNode) ?? null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue