From f2fc9427c7de0943de20b2d0d32534e8892481b3 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 12 Feb 2020 20:20:23 +0800 Subject: [PATCH] feat: add basic struct func call support --- .../chapi/ast/goast/GoFullIdentListener.kt | 9 ++++---- .../ast/goast/GoFullIdentListenerTest.kt | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt b/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt index 40689c00..36b61841 100644 --- a/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt +++ b/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt @@ -72,14 +72,13 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { codeFunction.MultipleReturns = this.buildReturnTypeFromSignature(codeFunction, ctx.signature()) codeFunction.Parameters = this.buildParameters(ctx.signature().parameters()) - val receiverName = this.getStructNameFromReceiver(ctx.receiver().parameters())!! - - this.addReceiverToStruct(receiverName, codeFunction) + currentFunction = codeFunction } override fun exitMethodDecl(ctx: GoParser.MethodDeclContext?) { -// defaultNode.Functions += currentFunction -// currentFunction = CodeFunction() + val receiverName = this.getStructNameFromReceiver(ctx!!.receiver().parameters())!! + this.addReceiverToStruct(receiverName, currentFunction) + currentFunction = CodeFunction() } private fun addReceiverToStruct(receiverName: String, codeFunction: CodeFunction) { diff --git a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt index 76457282..9fabdaed 100644 --- a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt +++ b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt @@ -175,6 +175,29 @@ func get(x int, y int) (int, int) { assertEquals(codeFile.DataStructures[0].Functions[0].Parameters[0].TypeType, "int") } + @Test + internal fun shouldIdentifyStructFuncCall() { + var code = """ +package main + +import "fmt" + +type Animal struct { + Age int +} + +func (a *Animal) Move() { + fmt.Println("Animal moved") +} +""" + val codeFile = GoAnalyser().analysis(code, "") + assertEquals(codeFile.DataStructures.size, 1) + assertEquals(codeFile.DataStructures[0].Functions.size, 1) + assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls.size, 1) + assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[0].NodeName, "fmt.Println") + assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[0].Parameters.size, 1) + } + @Test internal fun shouldIdentifyFuncCall() { var code = """