From aaf62dbf6372af4bc510eab560473c95e16070f9 Mon Sep 17 00:00:00 2001 From: Shivasurya Date: Sat, 11 May 2024 21:18:48 -0400 Subject: [PATCH] Fix false positve with local scope --- sourcecode-parser/construct.go | 13 +++++++------ sourcecode-parser/main.go | 3 +++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/sourcecode-parser/construct.go b/sourcecode-parser/construct.go index 94a3691..71dd417 100644 --- a/sourcecode-parser/construct.go +++ b/sourcecode-parser/construct.go @@ -35,6 +35,7 @@ type GraphNode struct { Scope string VariableValue string hasAccess bool + File string } type GraphEdge struct { @@ -92,8 +93,6 @@ func hasAccess(node *sitter.Node, variableName string, sourceCode []byte) bool { if node == nil { return false } - - // Check if current node is an identifier and matches the variableName if node.Type() == "identifier" && node.Content(sourceCode) == variableName { return true } @@ -110,7 +109,7 @@ func hasAccess(node *sitter.Node, variableName string, sourceCode []byte) bool { return hasAccess(node.NextSibling(), variableName, sourceCode) } -func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, currentContext *GraphNode) { +func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, currentContext *GraphNode, file string) { switch node.Type() { case "method_declaration": methodName, methodID := extractMethodName(node, sourceCode) @@ -260,9 +259,10 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c } if node.Type() == "local_variable_declaration" { scope = "local" - hasAccessValue = hasAccess(node, variableName, sourceCode) + hasAccessValue = hasAccess(node.NextSibling(), variableName, sourceCode) } else { scope = "field" + hasAccessValue = false } // Create a new node for the variable variableNode := &GraphNode{ @@ -276,6 +276,7 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c Scope: scope, VariableValue: variableValue, hasAccess: hasAccessValue, + File: file, } graph.AddNode(variableNode) } @@ -283,7 +284,7 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c // Recursively process child nodes for i := 0; i < int(node.ChildCount()); i++ { child := node.Child(i) - buildGraphFromAST(child, sourceCode, graph, currentContext) + buildGraphFromAST(child, sourceCode, graph, currentContext, file) } } @@ -407,7 +408,7 @@ func Initialize(directory string) *CodeGraph { rootNode := tree.RootNode() - buildGraphFromAST(rootNode, sourceCode, codeGraph, nil) + buildGraphFromAST(rootNode, sourceCode, codeGraph, nil, file) } //nolint:all // log.Println("Graph built successfully:", codeGraph) diff --git a/sourcecode-parser/main.go b/sourcecode-parser/main.go index d4d14e0..a2db869 100644 --- a/sourcecode-parser/main.go +++ b/sourcecode-parser/main.go @@ -30,7 +30,10 @@ func processQuery(input string, graph *CodeGraph, output string) (string, error) var result strings.Builder result.WriteString("------Query Results------\n") for _, entity := range entities { + result.WriteString("-------------------\n") result.WriteString(entity.CodeSnippet + "\n") + result.WriteString(entity.File + "\n") + result.WriteString("-------------------\n") } result.WriteString("-------------------\n") return result.String(), nil