Skip to content

Commit

Permalink
refactor(goast): improve handling of qualified identifiers in GoFullI…
Browse files Browse the repository at this point in the history
…dentListener

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.
  • Loading branch information
phodal committed Nov 12, 2024
1 parent c825273 commit 23220e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,19 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {


private fun handleForPrimary(child: PrimaryExprContext, isForLocalVar: Boolean = false): String? {
// val possibleResult = handleForPrimary(first, isForLocalVar).orEmpty()
if (child.primaryExpr()?.text?.contains(".") == true) {
/// replace first.text to possibleResult
/// d.Field -> Dao.Field
val split = child.primaryExpr().text.split(".")
val withoutFirst = split.drop(1).joinToString(".")
// get first part and try to resolve in local var
val first = split.first()
if (localVars.containsKey(first)) {
return "${localVars[first]}.$withoutFirst"
}
}

val nodeName = when (val first = child.getChild(0)) {
is GoParser.OperandContext -> {
first.text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,10 @@ func (d *Dao) GetLiveAchieve(ctx context.Context, uid int64) (achieve int64, err
val functionCalls = codeFile.DataStructures.first().Functions.last().FunctionCalls

val getExecFunc = functionCalls[0]
println(getExecFunc)

assertEquals(getExecFunc.FunctionName, "Userstatus")
assertEquals(getExecFunc.Parameters.size, 2)

assertEquals(getExecFunc.NodeName, "Dao")
assertEquals(getExecFunc.NodeName, "Dao.rcClient")
}
}

0 comments on commit 23220e9

Please sign in to comment.