From 85533a0dbd0e316a9cc27b76a8b29aefa3754e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Paz?= Date: Sat, 19 Jun 2021 08:29:24 -0300 Subject: [PATCH] Improve LongParameterList rule by supporting ignoring annotated parameters (#3879) --- README.md | 1 + .../rules/complexity/LongParameterList.kt | 12 ++++--- .../rules/complexity/LongParameterListSpec.kt | 35 ++++++++++++++++++- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0f8d5cfec..66b68478e 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ If you contributed to detekt but your name is not in the list, please feel free - [Vinicius Montes Munhoz](https://github.com/vfmunhoz) - Documentation improvement - [Eliezer Graber](https://github.com/eygraber) - Rule fix: ModifierOrder - [Dominik Labuda](https://github.com/Dominick1993) - Gradle plugin improvement +- [Andre Paz](https://github.com/andrepaz) - Rule improvement: LongParameterList ### Mentions diff --git a/detekt-rules-complexity/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterList.kt b/detekt-rules-complexity/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterList.kt index 5c49f22d6..17ba7c971 100644 --- a/detekt-rules-complexity/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterList.kt +++ b/detekt-rules-complexity/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterList.kt @@ -63,9 +63,10 @@ class LongParameterList(config: Config = Config.empty) : Rule(config) { private val ignoreDataClasses: Boolean by config(defaultValue = true) @Configuration( - "ignore long parameters list for constructors or functions in the " + - "context of these annotation class names; (e.g. ['Inject', 'Module', 'Suppress']); " + - "the most common case is for dependency injection where constructors are annotated with `@Inject`." + "ignore long parameters list for constructors, functions or their parameters in the " + + "context of these annotation class names; (e.g. ['Inject', 'Module', 'Suppress', 'Value']); " + + "the most common cases are for dependency injection where constructors are annotated with `@Inject` " + + "or parameters are annotated with `@Value` and should not be counted for the rule to trigger" ) private val ignoreAnnotated: List by config(listOf()) { list -> list.map { it.removePrefix("*").removeSuffix("*") } @@ -131,10 +132,11 @@ class LongParameterList(config: Config = Config.empty) : Rule(config) { } private fun KtParameterList.parameterCount(): Int { + val preFilteredParameters = parameters.filter { !it.isIgnored() } return if (ignoreDefaultParameters) { - parameters.filter { !it.hasDefaultValue() }.size + preFilteredParameters.filter { !it.hasDefaultValue() }.size } else { - parameters.size + preFilteredParameters.size } } diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt index 46edf06ca..413fae36d 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt @@ -102,7 +102,8 @@ class LongParameterListSpec : Spek({ "ignoreAnnotated" to listOf( "Generated", "kotlin.Deprecated", - "kotlin.jvm.JvmName" + "kotlin.jvm.JvmName", + "kotlin.Suppress" ), "functionThreshold" to 1, "constructorThreshold" to 1 @@ -159,6 +160,38 @@ class LongParameterListSpec : Spek({ """ assertThat(rule.compileAndLint(code)).isEmpty() } + + it("reports long parameter list for constructors if constructor parameters are annotated with annotation that is not ignored") { + val code = """ + @Target(AnnotationTarget.VALUE_PARAMETER) + annotation class CustomAnnotation + + class Data constructor(@CustomAnnotation val a: Int) + """ + assertThat(rule.compileAndLint(code)).hasSize(1) + } + + it("reports long parameter list for functions if enough function parameters are annotated with annotation that is not ignored") { + val code = """ + @Target(AnnotationTarget.VALUE_PARAMETER) + annotation class CustomAnnotation + + class Data { fun foo(@CustomAnnotation a: Int) {} } + """ + assertThat(rule.compileAndLint(code)).hasSize(1) + } + + it("does not report long parameter list for constructors if enough constructor parameters are annotated with ignored annotation") { + val code = "class Data constructor(@kotlin.Suppress(\"\") val a: Int)" + assertThat(rule.compileAndLint(code)).isEmpty() + } + + it("does not report long parameter list for functions if enough function parameters are annotated with ignored annotation") { + val code = """class Data { + fun foo(@kotlin.Suppress("") a: Int) {} } + """ + assertThat(rule.compileAndLint(code)).isEmpty() + } } } })