From 457648d828d6e1fdaaaf91925df6b03edc280533 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 12 Feb 2020 11:14:41 +0800 Subject: [PATCH] feat: add basic return type support --- .../chapi/ast/goast/GoFullIdentListener.kt | 9 +++++--- .../ast/goast/GoFullIdentListenerTest.kt | 23 ++++++++++++++++++- 2 files changed, 28 insertions(+), 4 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 edc363a6..6a0f43ad 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 @@ -52,8 +52,13 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { val result = ctx.signature().result() if (result != null) { - println("enterMethodDecl -> " + result.text) + val returnType = CodeProperty( + TypeType = result.text, + TypeValue = "" + ) + codeFunction.MultipleReturns += returnType } + this.buildParameters(ctx.signature().parameters()) val receiverName = this.getTypeNameFromReceiver(ctx.receiver().parameters())!! @@ -80,8 +85,6 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { when (typeChild::class.java.simpleName) { "StructTypeContext" -> { buildStruct(identifyName, typeChild) - - } else -> { 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 122c272e..900e2952 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 @@ -102,9 +102,30 @@ func (a *Animal) Move() { val codeFile = GoAnalyser().analysis(code, "") assertEquals(codeFile.DataStructures.size, 1) assertEquals(codeFile.DataStructures[0].NodeName, "Animal") - println(codeFile.DataStructures[0]) assertEquals(codeFile.DataStructures[0].Fields.size, 1) assertEquals(codeFile.DataStructures[0].Functions.size, 1) assertEquals(codeFile.DataStructures[0].Functions[0].Name, "Move") } + + @Test + internal fun shouldIdentifyStructFunctionReturnType() { + var code = """ +package main + +import "fmt" + +type Animal struct { + Age int +} + +func (a *Animal) Move() string { + return "" +} +""" + + val codeFile = GoAnalyser().analysis(code, "") + assertEquals(codeFile.DataStructures.size, 1) + assertEquals(codeFile.DataStructures[0].Functions[0].MultipleReturns.size, 1) + assertEquals(codeFile.DataStructures[0].Functions[0].MultipleReturns[0].TypeType, "string") + } }