mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
Handle MultiRules in Suppressors
This commit is contained in:
@@ -3,18 +3,39 @@ package io.gitlab.arturbosch.detekt.core.suppressors
|
||||
import io.gitlab.arturbosch.detekt.api.BaseRule
|
||||
import io.gitlab.arturbosch.detekt.api.ConfigAware
|
||||
import io.gitlab.arturbosch.detekt.api.Finding
|
||||
import io.gitlab.arturbosch.detekt.api.MultiRule
|
||||
import io.gitlab.arturbosch.detekt.api.Rule
|
||||
|
||||
/**
|
||||
* Given a Finding it decides if it should be suppressed (`true`) or not (`false`)
|
||||
*/
|
||||
typealias Suppressor = (Finding) -> Boolean
|
||||
|
||||
private fun buildSuppressors(rule: ConfigAware): List<Suppressor> {
|
||||
return listOfNotNull(
|
||||
annotationSuppressorFactory(rule),
|
||||
)
|
||||
}
|
||||
|
||||
internal fun getSuppressors(rule: BaseRule): List<Suppressor> {
|
||||
return if (rule is ConfigAware) {
|
||||
listOfNotNull(
|
||||
annotationSuppressorFactory(rule),
|
||||
)
|
||||
} else {
|
||||
emptyList()
|
||||
return when (rule) {
|
||||
is MultiRule -> rule.rules.flatMap { innerRule ->
|
||||
buildSuppressors(innerRule).map { suppressor -> InnerSuppressor(innerRule, suppressor) }
|
||||
}
|
||||
is ConfigAware -> buildSuppressors(rule)
|
||||
else -> emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
private class InnerSuppressor(
|
||||
private val rule: Rule,
|
||||
private val suppressor: Suppressor
|
||||
) : Suppressor {
|
||||
override fun invoke(finding: Finding): Boolean {
|
||||
return if (finding.id == rule.issue.id) {
|
||||
suppressor(finding)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,9 +49,30 @@ class SuppressorsSpec : Spek({
|
||||
|
||||
assertThat(suppress).isTrue()
|
||||
}
|
||||
|
||||
context("MultiRule") {
|
||||
it("A finding that should be suppressed") {
|
||||
val rule = AMultiRule(TestConfig("ignoreAnnotated" to listOf("Composable")))
|
||||
val suppress = getSuppressors(rule)
|
||||
.fold(false) { acc, suppressor -> acc || suppressor(noIgnorableCodeSmell) }
|
||||
|
||||
assertThat(suppress).isFalse()
|
||||
}
|
||||
it("A finding that should not be suppressed") {
|
||||
val rule = AMultiRule(TestConfig("ignoreAnnotated" to listOf("Composable")))
|
||||
val suppress = getSuppressors(rule)
|
||||
.fold(false) { acc, suppressor -> acc || suppressor(ignorableCodeSmell) }
|
||||
|
||||
assertThat(suppress).isTrue()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
private class AMultiRule(config: Config) : MultiRule() {
|
||||
override val rules: List<Rule> = listOf(ARule(config))
|
||||
}
|
||||
|
||||
private class ARule(config: Config = Config.empty) : Rule(config) {
|
||||
override val issue = Issue("IssueId", Severity.CodeSmell, "", Debt.TWENTY_MINS)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user