From f2e8ce8f53a9956eda438bf5801a2301225dbe3e Mon Sep 17 00:00:00 2001 From: Evan Wang Date: Fri, 1 May 2026 22:34:53 +0800 Subject: [PATCH] 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. --- .../ingestion/languages/go/range-binding.ts | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/gitnexus/src/core/ingestion/languages/go/range-binding.ts b/gitnexus/src/core/ingestion/languages/go/range-binding.ts index 4ba63f46b..34f0997f8 100644 --- a/gitnexus/src/core/ingestion/languages/go/range-binding.ts +++ b/gitnexus/src/core/ingestion/languages/go/range-binding.ts @@ -93,10 +93,32 @@ function extractElementType(binding: TypeRef): string | null { } function findEnclosingFunctionScope( - _node: unknown, - _scopeMap: ReadonlyMap, + node: unknown, + scopeMap: ReadonlyMap, ): 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; }