Skip to content

Commit

Permalink
feat(go): enable for check function name
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 9, 2022
1 parent 3962805 commit 61d7701
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,29 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {

is GoParser.PrimaryExprContext -> {
CodeCall(NodeName = child.text)
when(val subchild = child.getChild(1)) {
// primaryExpr '.' IDENTIFIER
when (child.getChild(1)) {
is TerminalNodeImpl -> {
// todo: verify child1
val child1 = child.getChild(0)
val identifier = child.getChild(2).text
// primaryExpr '.' IDENTIFIER
val nodeName = when (val first = child.getChild(0)) {
is GoParser.OperandContext -> {
first.text
}

is GoParser.PrimaryExprContext -> {
localVars.getOrDefault(first.text, first.text)
}

else -> {
first.text
}
}

CodeCall(
NodeName = child1.text,
FunctionName = identifier
NodeName = nodeName,
FunctionName = child.getChild(2).text
)
}

else -> {
CodeCall(NodeName = child.text)
}
Expand All @@ -196,7 +207,7 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {

override fun enterShortVarDecl(ctx: GoParser.ShortVarDeclContext?) {
ctx?.identifierList()?.IDENTIFIER()?.forEach {
localVars[it.text] = ""
localVars[it.text] = ctx.expressionList()?.text ?: ""
}
}

Expand Down
12 changes: 12 additions & 0 deletions chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoAnalyserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,17 @@ func main() {
}"""
val container = GoAnalyser().analysis(helloworldApi, "")
println(Json.encodeToString(container))
val codeFunction = container.DataStructures[0].Functions[0]
assertEquals(codeFunction.FunctionCalls[0].NodeName, "gin")
assertEquals(codeFunction.FunctionCalls[0].FunctionName, "Default")

assertEquals(codeFunction.FunctionCalls[1].NodeName, "gin.Default()")
assertEquals(codeFunction.FunctionCalls[1].FunctionName, "GET")

assertEquals(codeFunction.FunctionCalls[2].NodeName, "c")
assertEquals(codeFunction.FunctionCalls[2].FunctionName, "String")

assertEquals(codeFunction.FunctionCalls[3].NodeName, "gin.Default()")
assertEquals(codeFunction.FunctionCalls[3].FunctionName, "Run")
}
}

0 comments on commit 61d7701

Please sign in to comment.