Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
Exclude KtLint rules which detect same issues as detekt itself (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbosch authored Jun 3, 2020
1 parent 23d2c2d commit 64441e2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ import java.util.ServiceLoader

val defaultConfig: Config = loadDefaultConfig()

val excludedDuplicates = setOf(
"Filename", // from KtLint; same as MatchingDeclarationName
"MaximumLineLength", // from KtLint; same as MaxLineLength
"NoUnitReturn" // from KtLint; same as OptionalUnit
)

val allLoadedRules: List<Rule> = ServiceLoader.load(RuleSetProvider::class.java, Config::class.java.classLoader)
.flatMap { loadRules(it) }
.flatMap { (it as? MultiRule)?.rules ?: listOf(it) }
.asSequence()
.flatMap { loadRules(it).asSequence() }
.flatMap { (it as? MultiRule)?.rules?.asSequence() ?: sequenceOf(it) }
.filterIsInstance<Rule>()
.filterNot { it.ruleId in excludedDuplicates }
.toList()

private fun loadRules(provider: RuleSetProvider): List<BaseRule> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.cli.baseline.BaselineFacade
import io.gitlab.arturbosch.detekt.sonar.foundation.BASELINE_KEY
import io.gitlab.arturbosch.detekt.sonar.foundation.logger
import io.gitlab.arturbosch.detekt.sonar.rules.excludedDuplicates
import io.gitlab.arturbosch.detekt.sonar.rules.ruleKeyLookup
import org.sonar.api.batch.fs.InputFile
import org.sonar.api.batch.sensor.SensorContext
Expand All @@ -22,9 +23,9 @@ class IssueReporter(
private val config = context.config()

fun run() {
val baseline = tryFindBaseline(config, baseDir)
for ((ruleSet, findings) in result.findings) {
logger.info("RuleSet: $ruleSet - ${findings.size}")
val baseline = tryFindBaseline(config, baseDir)
val filtered = baseline?.filter(findings) ?: findings
filtered.forEach(this::reportIssue)
}
Expand Down Expand Up @@ -55,6 +56,9 @@ class IssueReporter(
}

private fun reportIssue(issue: Finding) {
if (issue.id in excludedDuplicates) {
return
}
if (issue.startPosition.line < 0) {
logger.info("Invalid location for ${issue.compactWithSignature()}.")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Severity
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.sonar.api.rule.RuleKey
import org.spekframework.spek2.Spek

class DetektRuleKeyTest {
class DetektRuleKeySpec : Spek({

@Test
fun `The rule 'DetektRuleKey(*)' does not exist`() {
test("The rule 'DetektRuleKey(*)' does not exist") {
val rulesByRuleKey = HashMap<RuleKey, Any?>()

rulesByRuleKey[RuleKey.of("detekt-kotlin", "NewLineAtEndOfFile")] = "COUCOU"
Expand All @@ -21,4 +20,10 @@ class DetektRuleKeyTest {
assertThat(rulesByRuleKey).containsKey(ruleKey)
assertThat(rulesByRuleKey[ruleKey]).isNotNull
}
}

test("loaded rules do not contain duplicates rules from KtLint") {
val ids = allLoadedRules.map { it.ruleId }

assertThat(ids).doesNotContainSequence(excludedDuplicates)
}
})

0 comments on commit 64441e2

Please sign in to comment.