Skip to content

Commit

Permalink
Fix false positve with local scope
Browse files Browse the repository at this point in the history
  • Loading branch information
shivasurya committed May 12, 2024
1 parent f9bbdc9 commit aaf62db
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
13 changes: 7 additions & 6 deletions sourcecode-parser/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type GraphNode struct {
Scope string
VariableValue string
hasAccess bool
File string
}

type GraphEdge struct {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -276,14 +276,15 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
Scope: scope,
VariableValue: variableValue,
hasAccess: hasAccessValue,
File: file,
}
graph.AddNode(variableNode)
}

// 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)
}
}

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions sourcecode-parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aaf62db

Please sign in to comment.