mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
Suppressor as fun interface
This commit is contained in:
@@ -140,7 +140,7 @@ internal class Analyzer(
|
||||
private fun filterSuppressedFindings(rule: BaseRule): List<Finding> {
|
||||
val suppressors = getSuppressors(rule)
|
||||
return if (suppressors.isNotEmpty()) {
|
||||
rule.findings.filter { finding -> !suppressors.any { suppressor -> suppressor(finding) } }
|
||||
rule.findings.filter { finding -> !suppressors.any { suppressor -> suppressor.shouldSuppress(finding) } }
|
||||
} else {
|
||||
rule.findings
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
internal fun annotationSuppressorFactory(rule: ConfigAware): Suppressor? {
|
||||
val annotations = rule.valueOrDefault("ignoreAnnotated", emptyList<String>())
|
||||
return if (annotations.isNotEmpty()) {
|
||||
{ finding ->
|
||||
Suppressor { finding ->
|
||||
val element = finding.entity.ktElement
|
||||
element != null && annotationSuppressor(element, annotations)
|
||||
}
|
||||
|
||||
@@ -6,10 +6,12 @@ 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
|
||||
fun interface Suppressor {
|
||||
/**
|
||||
* Given a Finding it decides if it should be suppressed (`true`) or not (`false`)
|
||||
*/
|
||||
fun shouldSuppress(finding: Finding): Boolean
|
||||
}
|
||||
|
||||
private fun buildSuppressors(rule: ConfigAware): List<Suppressor> {
|
||||
return listOfNotNull(
|
||||
@@ -31,9 +33,9 @@ private class InnerSuppressor(
|
||||
private val rule: Rule,
|
||||
private val suppressor: Suppressor
|
||||
) : Suppressor {
|
||||
override fun invoke(finding: Finding): Boolean {
|
||||
override fun shouldSuppress(finding: Finding): Boolean {
|
||||
return if (finding.id == rule.issue.id) {
|
||||
suppressor(finding)
|
||||
suppressor.shouldSuppress(finding)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ class AnnotationSuppressorSpec : Spek({
|
||||
}
|
||||
|
||||
it("If KtElement is null it returns false") {
|
||||
assertThat(suppressor(buildFinding(element = null))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = null))).isFalse()
|
||||
}
|
||||
|
||||
context("If annotation is at file level") {
|
||||
@@ -79,20 +79,20 @@ class AnnotationSuppressorSpec : Spek({
|
||||
}
|
||||
|
||||
it("If reports root it returns true") {
|
||||
assertThat(suppressor(buildFinding(element = root))).isTrue()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = root))).isTrue()
|
||||
}
|
||||
|
||||
it("If reports class it returns true") {
|
||||
val ktClass = root.findChildByClass(KtClass::class.java)!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktClass))).isTrue()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktClass))).isTrue()
|
||||
}
|
||||
|
||||
it("If reports function in class it returns true") {
|
||||
val ktFunction = root.findChildByClass(KtClass::class.java)!!
|
||||
.findFunctionByName("function")!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktFunction))).isTrue()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktFunction))).isTrue()
|
||||
}
|
||||
|
||||
it("If reports parameter in function in class it returns true") {
|
||||
@@ -100,13 +100,13 @@ class AnnotationSuppressorSpec : Spek({
|
||||
.findFunctionByName("function")!!
|
||||
.findDescendantOfType<KtParameter>()!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktParameter))).isTrue()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktParameter))).isTrue()
|
||||
}
|
||||
|
||||
it("If reports top level function it returns true") {
|
||||
val ktFunction = root.findChildByClass(KtFunction::class.java)!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktFunction))).isTrue()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktFunction))).isTrue()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,20 +127,20 @@ class AnnotationSuppressorSpec : Spek({
|
||||
}
|
||||
|
||||
it("If reports root it returns false") {
|
||||
assertThat(suppressor(buildFinding(element = root))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = root))).isFalse()
|
||||
}
|
||||
|
||||
it("If reports class it returns false") {
|
||||
val ktClass = root.findChildByClass(KtClass::class.java)!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktClass))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktClass))).isFalse()
|
||||
}
|
||||
|
||||
it("If reports function in class it returns true") {
|
||||
val ktFunction = root.findChildByClass(KtClass::class.java)!!
|
||||
.findFunctionByName("function")!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktFunction))).isTrue()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktFunction))).isTrue()
|
||||
}
|
||||
|
||||
it("If reports parameter in function in class it returns true") {
|
||||
@@ -148,13 +148,13 @@ class AnnotationSuppressorSpec : Spek({
|
||||
.findFunctionByName("function")!!
|
||||
.findDescendantOfType<KtParameter>()!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktParameter))).isTrue()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktParameter))).isTrue()
|
||||
}
|
||||
|
||||
it("If reports top level function it returns false") {
|
||||
val ktFunction = root.findChildByClass(KtFunction::class.java)!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktFunction))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktFunction))).isFalse()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,20 +174,20 @@ class AnnotationSuppressorSpec : Spek({
|
||||
}
|
||||
|
||||
it("If reports root it returns false") {
|
||||
assertThat(suppressor(buildFinding(element = root))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = root))).isFalse()
|
||||
}
|
||||
|
||||
it("If reports class it returns false") {
|
||||
val ktClass = root.findChildByClass(KtClass::class.java)!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktClass))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktClass))).isFalse()
|
||||
}
|
||||
|
||||
it("If reports function in class it returns false") {
|
||||
val ktFunction = root.findChildByClass(KtClass::class.java)!!
|
||||
.findFunctionByName("function")!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktFunction))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktFunction))).isFalse()
|
||||
}
|
||||
|
||||
it("If reports parameter in function in class it returns false") {
|
||||
@@ -195,13 +195,13 @@ class AnnotationSuppressorSpec : Spek({
|
||||
.findFunctionByName("function")!!
|
||||
.findDescendantOfType<KtParameter>()!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktParameter))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktParameter))).isFalse()
|
||||
}
|
||||
|
||||
it("If reports top level function it returns false") {
|
||||
val ktFunction = root.findChildByClass(KtFunction::class.java)!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktFunction))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktFunction))).isFalse()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,20 +227,20 @@ class AnnotationSuppressorSpec : Spek({
|
||||
}
|
||||
|
||||
it("If reports root it returns false") {
|
||||
assertThat(suppressor(buildFinding(element = root))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = root))).isFalse()
|
||||
}
|
||||
|
||||
it("If reports class it returns false") {
|
||||
val ktClass = root.findChildByClass(KtClass::class.java)!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktClass))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktClass))).isFalse()
|
||||
}
|
||||
|
||||
it("If reports function in class it returns true") {
|
||||
val ktFunction = root.findChildByClass(KtClass::class.java)!!
|
||||
.findFunctionByName("function")!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktFunction))).isTrue()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktFunction))).isTrue()
|
||||
}
|
||||
|
||||
it("If reports parameter in function in class it returns true") {
|
||||
@@ -248,13 +248,13 @@ class AnnotationSuppressorSpec : Spek({
|
||||
.findFunctionByName("function")!!
|
||||
.findDescendantOfType<KtParameter>()!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktParameter))).isTrue()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktParameter))).isTrue()
|
||||
}
|
||||
|
||||
it("If reports top level function it returns false") {
|
||||
val ktFunction = root.findChildByClass(KtFunction::class.java)!!
|
||||
|
||||
assertThat(suppressor(buildFinding(element = ktFunction))).isFalse()
|
||||
assertThat(suppressor.shouldSuppress(buildFinding(element = ktFunction))).isFalse()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,14 +38,14 @@ class SuppressorsSpec : Spek({
|
||||
it("A finding that should be suppressed") {
|
||||
val rule = ARule(TestConfig("ignoreAnnotated" to listOf("Composable")))
|
||||
val suppress = getSuppressors(rule)
|
||||
.fold(false) { acc, suppressor -> acc || suppressor(noIgnorableCodeSmell) }
|
||||
.fold(false) { acc, suppressor -> acc || suppressor.shouldSuppress(noIgnorableCodeSmell) }
|
||||
|
||||
assertThat(suppress).isFalse()
|
||||
}
|
||||
it("A finding that should not be suppressed") {
|
||||
val rule = ARule(TestConfig("ignoreAnnotated" to listOf("Composable")))
|
||||
val suppress = getSuppressors(rule)
|
||||
.fold(false) { acc, suppressor -> acc || suppressor(ignorableCodeSmell) }
|
||||
.fold(false) { acc, suppressor -> acc || suppressor.shouldSuppress(ignorableCodeSmell) }
|
||||
|
||||
assertThat(suppress).isTrue()
|
||||
}
|
||||
@@ -54,14 +54,14 @@ class SuppressorsSpec : Spek({
|
||||
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) }
|
||||
.fold(false) { acc, suppressor -> acc || suppressor.shouldSuppress(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) }
|
||||
.fold(false) { acc, suppressor -> acc || suppressor.shouldSuppress(ignorableCodeSmell) }
|
||||
|
||||
assertThat(suppress).isTrue()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user