Skip to content

Commit

Permalink
Support for Method declaration has_access (#28)
Browse files Browse the repository at this point in the history
From now on, `has_access` attribute should support method declaration entity. If a declared method within the project isn't being invoked, it's marked as has_access = false.
  • Loading branch information
shivasurya authored May 20, 2024
1 parent 6129d26 commit 51ffd93
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions sourcecode-parser/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,30 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
case "method_invocation":
methodName, methodID := extractMethodName(node, sourceCode) // Implement this
invokedNode, exists := graph.Nodes[methodID]
arguments := []string{}
// get argument list from arguments node iterate for child node
for i := 0; i < int(node.ChildCount()); i++ {
if node.Child(i).Type() == "argument_list" {
argumentsNode := node.Child(i)
for j := 0; j < int(argumentsNode.ChildCount()); j++ {
argument := argumentsNode.Child(j)
if argument.Type() == "identifier" {
arguments = append(arguments, argument.Content(sourceCode))
}
}
}
}

if !exists || (exists && invokedNode.ID != methodID) {
// Create a placeholder node for external or inbuilt method
invokedNode = &GraphNode{
ID: methodID,
Type: "method_invocation",
Name: methodName,
IsExternal: true,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1, // Lines start from 0 in the AST
ID: methodID,
Type: "method_invocation",
Name: methodName,
IsExternal: true,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1, // Lines start from 0 in the AST
MethodArgumentsValue: arguments,
}
graph.AddNode(invokedNode)
}
Expand Down Expand Up @@ -285,6 +300,23 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
child := node.Child(i)
buildGraphFromAST(child, sourceCode, graph, currentContext, file)
}

// iterate through method declaration from graph node
for _, node := range graph.Nodes {
if node.Type == "method_declaration" {
// iterate through method method_invocation from graph node
for _, invokedNode := range graph.Nodes {
if invokedNode.Type == "method_invocation" {
if invokedNode.Name == node.Name {
// check argument list count is same
if len(invokedNode.MethodArgumentsValue) == len(node.MethodArgumentsType) {
node.hasAccess = true
}
}
}
}
}
}
}

// write a function to generate unique method id from method name, class name, and package name, parameters, and return type.
Expand Down

0 comments on commit 51ffd93

Please sign in to comment.