Skip to content

Commit 23220e9

Browse files
committed
refactor(goast): improve handling of qualified identifiers in GoFullIdentListener
Updated the `GoFullIdentListener` to better manage qualified identifiers by resolving local variable references within a qualified expression. This change ensures that when a qualified identifier is encountered (e.g., `d.Field`), if the first part of the identifier (local variable `d`) is present in the local variables map, it is replaced accordingly (e.g., `Dao.Field`). Additionally, removed a println statement and corrected an assertion in the `GoFullIdentListenerTest` to reflect the expected node name with the updated handling of qualified identifiers.
1 parent c825273 commit 23220e9

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt

+13
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,19 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
366366

367367

368368
private fun handleForPrimary(child: PrimaryExprContext, isForLocalVar: Boolean = false): String? {
369+
// val possibleResult = handleForPrimary(first, isForLocalVar).orEmpty()
370+
if (child.primaryExpr()?.text?.contains(".") == true) {
371+
/// replace first.text to possibleResult
372+
/// d.Field -> Dao.Field
373+
val split = child.primaryExpr().text.split(".")
374+
val withoutFirst = split.drop(1).joinToString(".")
375+
// get first part and try to resolve in local var
376+
val first = split.first()
377+
if (localVars.containsKey(first)) {
378+
return "${localVars[first]}.$withoutFirst"
379+
}
380+
}
381+
369382
val nodeName = when (val first = child.getChild(0)) {
370383
is GoParser.OperandContext -> {
371384
first.text

chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,10 @@ func (d *Dao) GetLiveAchieve(ctx context.Context, uid int64) (achieve int64, err
546546
val functionCalls = codeFile.DataStructures.first().Functions.last().FunctionCalls
547547

548548
val getExecFunc = functionCalls[0]
549-
println(getExecFunc)
549+
550550
assertEquals(getExecFunc.FunctionName, "Userstatus")
551551
assertEquals(getExecFunc.Parameters.size, 2)
552552

553-
assertEquals(getExecFunc.NodeName, "Dao")
553+
assertEquals(getExecFunc.NodeName, "Dao.rcClient")
554554
}
555555
}

0 commit comments

Comments
 (0)