Skip to content

Commit

Permalink
#5014 Fix MaxChainedCallsOnSameLine false positives (#5020)
Browse files Browse the repository at this point in the history
Fix issues where MaxChainedCallsOnSameLine would report long import and package directives, where a warning certainly isn't necessary.
  • Loading branch information
dzirbel committed Jul 2, 2022
1 parent ddcf77d commit 6edf6bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Expand Up @@ -10,6 +10,8 @@ import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.internal.Configuration
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.KtUnaryExpression

Expand Down Expand Up @@ -39,10 +41,13 @@ class MaxChainedCallsOnSameLine(config: Config = Config.empty) : Rule(config) {
override fun visitQualifiedExpression(expression: KtQualifiedExpression) {
super.visitQualifiedExpression(expression)

// skip if the parent is also a call on the same line to avoid duplicated warnings
val parent = expression.parent

// skip if the parent is also a call on the same line to avoid duplicated warnings
if (parent is KtQualifiedExpression && !parent.callOnNewLine()) return

if (parent is KtImportDirective || parent is KtPackageDirective) return

val chainedCalls = expression.countChainedCalls() + 1
if (chainedCalls > maxChainedCalls) {
report(
Expand Down
Expand Up @@ -3,6 +3,7 @@ package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.junit.jupiter.api.Test

class MaxChainedCallsOnSameLineSpec {
Expand Down Expand Up @@ -104,4 +105,20 @@ class MaxChainedCallsOnSameLineSpec {

assertThat(rule.compileAndLint(code)).hasSize(1)
}

@Test
fun `does not report long imports`() {
val rule = MaxChainedCallsOnSameLine(TestConfig(mapOf("maxChainedCalls" to 3)))
val code = "import a.b.c.d.e"

assertThat(rule.lint(code)).isEmpty()
}

@Test
fun `does not report long package declarations`() {
val rule = MaxChainedCallsOnSameLine(TestConfig(mapOf("maxChainedCalls" to 3)))
val code = "package a.b.c.d.e"

assertThat(rule.lint(code)).isEmpty()
}
}

0 comments on commit 6edf6bb

Please sign in to comment.