Skip to content

Commit

Permalink
removed duplicates while processing ast nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
shivasurya committed Feb 23, 2025
1 parent dbf8e47 commit 4fc9c3b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ docs/public/rules/*.json
.DS_Store

node_modules

test-src/android/pathfinder.db
6 changes: 0 additions & 6 deletions sourcecode-parser/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
"database/sql"
"fmt"
"log"

_ "github.com/mattn/go-sqlite3"

Check warning on line 7 in sourcecode-parser/db/db.go

View workflow job for this annotation

GitHub Actions / lint

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)
Expand Down Expand Up @@ -185,11 +184,6 @@ func (s *StorageNode) AddClassDecl(node *model.Class) {

func (s *StorageNode) AddMethodDecl(node *model.Method) {
s.MethodDecl = append(s.MethodDecl, node)

Check warning on line 186 in sourcecode-parser/db/db.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/db/db.go#L185-L186

Added lines #L185 - L186 were not covered by tests
// save method node to database if not exist
err := node.Insert(s.DB)
if err != nil {
fmt.Println(err)
}
}

func (s *StorageNode) AddFieldDecl(node *model.FieldDeclaration) {
Expand Down
35 changes: 13 additions & 22 deletions sourcecode-parser/tree/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func buildQLTreeFromAST(node *sitter.Node, sourceCode []byte, file string, paren
for i := 0; i < int(node.ChildCount()); i++ {
buildQLTreeFromAST(node.Child(i), sourceCode, file, blockStmtTreeNode, storageNode)
}
return
case "return_statement":
returnStmtNode := javalang.ParseReturnStatement(node, sourceCode, file)
parentNode.AddChild(&model.TreeNode{Node: &model.Node{ReturnStmt: returnStmtNode}, Parent: parentNode})
Expand Down Expand Up @@ -60,26 +61,30 @@ func buildQLTreeFromAST(node *sitter.Node, sourceCode []byte, file string, paren
parentNode.AddChild(&model.TreeNode{Node: &model.Node{BinaryExpr: binaryExprNode}, Parent: parentNode})
case "method_declaration":
methodDeclaration := javalang.ParseMethodDeclaration(node, sourceCode, file)
utilities.Log("Processing method:", methodDeclaration.Name, "in file:", file)
methodNode := &model.TreeNode{Node: &model.Node{MethodDecl: methodDeclaration}, Parent: parentNode}
parentNode.AddChild(methodNode)
storageNode.AddMethodDecl(methodDeclaration)
for i := 0; i < int(node.ChildCount()); i++ {
buildQLTreeFromAST(node.Child(i), sourceCode, file, methodNode, storageNode)
}
return
case "method_invocation":
methodInvokedNode := javalang.ParseMethodInvoker(node, sourceCode, file)
methodInvocationTreeNode := &model.TreeNode{Node: &model.Node{MethodCall: methodInvokedNode}, Parent: parentNode}
parentNode.AddChild(methodInvocationTreeNode)
for i := 0; i < int(node.ChildCount()); i++ {
buildQLTreeFromAST(node.Child(i), sourceCode, file, methodInvocationTreeNode, storageNode)
}
return
case "class_declaration":
classNode := javalang.ParseClass(node, sourceCode, file)
classTreeNode := &model.TreeNode{Node: &model.Node{ClassDecl: classNode}, Children: nil, Parent: parentNode}
parentNode.AddChild(classTreeNode)
for i := 0; i < int(node.ChildCount()); i++ {
buildQLTreeFromAST(node.Child(i), sourceCode, file, classTreeNode, storageNode)
}
return
case "block_comment":
// Parse block comments
if strings.HasPrefix(node.Content(sourceCode), "/*") {
Expand Down Expand Up @@ -127,6 +132,7 @@ func readFile(path string) ([]byte, error) {

func Initialize(directory string) []*model.TreeNode {
treeHolder := []*model.TreeNode{}
storageNode := db.NewStorageNode("")
// record start time
start := time.Now()

Expand Down Expand Up @@ -172,7 +178,6 @@ func Initialize(directory string) []*model.TreeNode {
defer tree.Close()

rootNode := tree.RootNode()
storageNode := db.NewStorageNode("")
localTree := &model.TreeNode{
Parent: nil,
Node: &model.Node{
Expand Down Expand Up @@ -228,18 +233,14 @@ func Initialize(directory string) []*model.TreeNode {
}
}()

// Wait for all workers to finish
go func() {
wg.Wait()
close(statusChan)
close(progressChan)
close(treeChan)
}()
wg.Wait()
close(statusChan)
close(progressChan)
close(treeChan)

// Print tree structure recursively from treeChan
// for treeNode := range treeChan {
// printTree(treeNode, 0)
// }
for _, method := range storageNode.MethodDecl {
utilities.Log("Method: ", method.ReturnType, " ", method.Name, " ", method.Parameters, " ", method.SourceDeclaration)
}

Check warning on line 243 in sourcecode-parser/tree/construct.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/tree/construct.go#L236-L243

Added lines #L236 - L243 were not covered by tests

end := time.Now()
elapsed := end.Sub(start)
Expand All @@ -248,13 +249,3 @@ func Initialize(directory string) []*model.TreeNode {

return treeHolder

Check warning on line 250 in sourcecode-parser/tree/construct.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/tree/construct.go#L245-L250

Added lines #L245 - L250 were not covered by tests
}

// func printTree(node *model.TreeNode, level int) {
// tab := strings.Repeat("\t", level)
// fmt.Println(tab+"Value:", node.NodeType)
// fmt.Println(tab+"Code:", node.Node.CodeSnippet)
// fmt.Println(tab + "-------------------------------------")
// for _, child := range node.Children {
// printTree(child, level+1)
// }
// }

0 comments on commit 4fc9c3b

Please sign in to comment.