Skip to content

Commit 61a83d8

Browse files
committed
feat(kotlin): add object class support
1 parent 24bd961 commit 61a83d8

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

chapi-ast-kotlin/src/main/kotlin/chapi/ast/kotlinast/KotlinBasicIdentListener.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ open class KotlinBasicIdentListener(private val fileName: String) : KotlinAstLis
109109
currentNode = buildClass(ctx)
110110
}
111111

112+
112113
open fun buildClass(ctx: KotlinParser.ClassDeclarationContext): CodeDataStruct {
113114
val implements = ctx.delegationSpecifiers()?.text?.let(::getTypeFullName)?.let(::listOf) ?: emptyList()
114115
val annotations = ctx.modifiers().getAnnotations()
@@ -139,6 +140,29 @@ open class KotlinBasicIdentListener(private val fileName: String) : KotlinAstLis
139140
isEnteredClass.decrementAndGet()
140141
}
141142

143+
override fun enterObjectDeclaration(ctx: KotlinParser.ObjectDeclarationContext?) {
144+
isEnteredClass.incrementAndGet()
145+
currentNode = buildObject(ctx!!)
146+
}
147+
148+
private fun buildObject(ctx: KotlinParser.ObjectDeclarationContext): CodeDataStruct {
149+
val annotations = ctx.modifiers().getAnnotations()
150+
return CodeDataStruct().apply {
151+
NodeName = ctx.simpleIdentifier().Identifier().text
152+
Type = DataStructType.OBJECT
153+
Package = codeContainer.PackageName
154+
FilePath = codeContainer.FullName
155+
Annotations = annotations
156+
Imports = imports
157+
Position = buildPosition(ctx)
158+
}
159+
}
160+
161+
override fun exitObjectDeclaration(ctx: KotlinParser.ObjectDeclarationContext?) {
162+
classes.add(currentNode)
163+
isEnteredClass.decrementAndGet()
164+
}
165+
142166
override fun enterPrimaryConstructor(ctx: KotlinParser.PrimaryConstructorContext) {
143167
val parameters = ctx.classParameters().classParameter().map(::buildProperty)
144168
val fields = ctx.classParameters().classParameter().mapNotNull(::buildField)

chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/KotlinFunctionCallTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ class KotlinFunctionCallTest {
2424
assert(functionCall.NodeName == "A")
2525
}
2626

27+
@Test
28+
fun should_handle_for_kotlin_object() {
29+
val code = """
30+
object A {
31+
fun foo() {
32+
println("Hello, world!")
33+
}
34+
}
35+
""".trimIndent()
36+
37+
val codeContainer = KotlinAnalyser().analysis(code, "Test.kt", ParseMode.Full)
38+
val dataStructures = codeContainer.DataStructures.filter { it.NodeName == "A" }
39+
val codeFunction = dataStructures[0].Functions[0]
40+
41+
assert(codeFunction.Name == "foo")
42+
}
43+
2744
@Test
2845
fun should_success_parse_test_usecase() {
2946
val code = """

0 commit comments

Comments
 (0)