Skip to content

Commit

Permalink
WIP: Fix the UnnecessaryPassThroughClass false-positives
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Feb 14, 2024
1 parent f67bdff commit d9d5641
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.ivy.explicit.rule

import io.gitlab.arturbosch.detekt.api.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.isPrivate

class UnnecessaryPassThroughClassRule(config: Config) : Rule(config) {

Expand All @@ -22,9 +21,9 @@ class UnnecessaryPassThroughClassRule(config: Config) : Rule(config) {
klass.isData()
) return

val functions = klass.body?.functions?.filterNot { it.isPrivate() } ?: return
if (functions.isEmpty()) return
// TODO: Ignore if the class has any "val/var" in the body

val functions = klass.body?.functions?.takeIf { it.isNotEmpty() } ?: return
val passThroughClass = functions.all(::isPassThroughFunction)
if (passThroughClass) {
report(CodeSmell(issue, Entity.from(klass), failureMessage(klass)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,79 @@ internal class UnnecessaryPassThroughClassRuleTest(private val env: KotlinCoreEn
// then
findings shouldHaveSize 0
}

@Test
fun `does not report edge case 1`() {
// given
val code = """
class CalcWalletBalanceAct @Inject constructor(
private val accountsAct: AccountsAct,
private val calcAccBalanceAct: CalcAccBalanceAct,
private val exchangeAct: ExchangeAct,
) : FPAction<CalcWalletBalanceAct.Input, BigDecimal>() {
override suspend fun Input.compose(): suspend () -> BigDecimal = recipe().fixUnit()
private suspend fun Input.recipe(): suspend (Unit) -> BigDecimal =
accountsAct thenFilter {
withExcluded || it.includeInBalance
} thenMap {
calcAccBalanceAct(
CalcAccBalanceAct.Input(
account = it,
range = range
)
)
} thenMap {
exchangeAct(
ExchangeAct.Input(
data = ExchangeData(
baseCurrency = baseCurrency,
fromCurrency = (it.account.currency ?: baseCurrency).toOption(),
toCurrency = balanceCurrency
),
amount = it.balance
)
)
} thenSum {
it.orNull() ?: BigDecimal.ZERO
}
data class Input(
val baseCurrency: String,
val balanceCurrency: String = baseCurrency,
val range: ClosedTimeRange = ClosedTimeRange.allTimeIvy(),
val withExcluded: Boolean = false
)
}
"""

// when
val findings = rule.compileAndLintWithContext(env, code)

// then
findings shouldHaveSize 0
}

@Test
fun `does not report edge case 2`() {
// given
val code = """
@Singleton
class DataWriteEventBus @Inject constructor() {
private val internalEvents = MutableSharedFlow<DataWriteEvent>()
val events: Flow<DataWriteEvent> = internalEvents
suspend fun post(event: DataWriteEvent) {
internalEvents.emit(event)
}
}
"""

// when
val findings = rule.compileAndLintWithContext(env, code)

// then
findings shouldHaveSize 0
}
}

0 comments on commit d9d5641

Please sign in to comment.