Skip to content

Commit

Permalink
Merge pull request #28 from Doist/afzal/fix.consistent_when_multiline
Browse files Browse the repository at this point in the history
Don't report when entries with multiple conditions and trailing comma
  • Loading branch information
AfzalivE authored Sep 13, 2024
2 parents 59dc3c9 + a6c563a commit 96b85af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/doist/detekt/ConsistentWhenEntries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ class ConsistentWhenEntries(config: Config) : Rule(config) {
}
}

private fun KtWhenEntry.isMultiline() = text.count { it == '\n' } > 1
private fun KtWhenEntry.isMultiline() = (expression?.text?.count { it == '\n' } ?: 0) > 1
}
34 changes: 34 additions & 0 deletions src/test/kotlin/com/doist/detekt/ConsistentWhenEntriesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,38 @@ internal class ConsistentWhenEntriesTest(private val env: KotlinCoreEnvironment)
val findings = rule.compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `doesn't report single line when entries with multiple conditions and trailing comma`() {
val code = """
val a = listOf<Int>()
val b = when(a) {
is ArrayList,
is MutableList,
-> true
else -> false
}
"""
val rule = ConsistentWhenEntries(Config.empty)
val findings = rule.compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `report inconsistent when entries with multiple conditions and trailing comma`() {
val code = """
val a = listOf<Int>()
val b = when(a) {
is ArrayList,
is MutableList,
-> {
true
}
else -> false
}
"""
val rule = ConsistentWhenEntries(Config.empty)
val findings = rule.compileAndLintWithContext(env, code)
findings shouldHaveSize 1
}
}

0 comments on commit 96b85af

Please sign in to comment.