From 51ffd93e73fd6d50685ec9c90a0dd89dc4f164ae Mon Sep 17 00:00:00 2001 From: Shivasurya Date: Mon, 20 May 2024 17:47:06 -0400 Subject: [PATCH] Support for Method declaration has_access (#28) 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. --- sourcecode-parser/construct.go | 44 +++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/sourcecode-parser/construct.go b/sourcecode-parser/construct.go index 72f91e4..990fa4b 100644 --- a/sourcecode-parser/construct.go +++ b/sourcecode-parser/construct.go @@ -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) } @@ -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.