Skip to content

Commit

Permalink
fix: fix kotlin crash issue
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 18, 2022
1 parent 0b7a0e0 commit 6b141d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package chapi.app.analyser
import chapi.app.analyser.config.ChapiConfig
import chapi.app.analyser.config.Language
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.nio.file.Paths
import kotlin.io.path.Path
import kotlin.io.path.absolutePathString
import kotlin.test.assertEquals

internal class KotlinAnalyserAppTest {
Expand All @@ -21,6 +24,16 @@ internal class KotlinAnalyserAppTest {
Assertions.assertEquals(nodeInfos[0].FilePath.endsWith("resources/test/languages/kotlin/Hello.kt"), true)
}

@Test
@Disabled
fun `boostrap for not crash`() {
val config = ChapiConfig(language = Language.KOTLIN)
// todo: can change to `../`
val path = Path("../")

KotlinAnalyserApp(config).analysisNodeByPath(path.absolutePathString())
}

@Nested
inner class DryRun {
private val relativePath = Paths.get("../../archguard-backend/src/main/kotlin")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ class KotlinFullIdentListener(fileName: String) : KotlinBasicIdentListener(fileN
private val postClassHandler = mutableListOf<(CodeDataStruct) -> Unit>()
override fun buildFunction(ctx: KotlinParser.FunctionDeclarationContext): CodeFunction =
super.buildFunction(ctx).apply {
FunctionCalls = buildFunctionCalls(ctx).toTypedArray()
val functionCalls = buildFunctionCalls(ctx)
if (functionCalls != null) {
FunctionCalls = functionCalls.toTypedArray()
}
}

private fun buildFunctionCalls(ctx: KotlinParser.FunctionDeclarationContext): List<CodeCall> =
ctx.functionBody().block().statements().statement().mapNotNull(::buildFunctionCall)
private fun buildFunctionCalls(ctx: KotlinParser.FunctionDeclarationContext): List<CodeCall>? =
ctx.functionBody()?.block()?.statements()?.statement()?.mapNotNull(::buildFunctionCall)

private fun buildFunctionCall(it: KotlinParser.StatementContext): CodeCall? {
val result = Regex("(\\w+\\.?)+\\((.*)\\)").find(it.text) ?: return null
Expand Down

0 comments on commit 6b141d5

Please # to comment.