mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
Improve LongParameterList rule by supporting ignoring annotated parameters (#3879)
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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<String> by config(listOf<String>()) { 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user