Skip to content

Commit

Permalink
Add ignoreAnnotatedFunctions to TooManyFunctions (#6875)
Browse files Browse the repository at this point in the history
* Add ignoreAnnotatedFunctions to TooManyFunctions

* Add TooManyFunctions recommendation to compose docs

* Ensure Preview annotation in tests is declared

* Simplify hasAnnotation check

* Revert "Simplify hasAnnotation check"

This reverts commit 3d16e57.
  • Loading branch information
matejdro authored and cortinico committed Jan 31, 2024
1 parent 742518d commit e44aff7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions detekt-core/src/main/resources/default-detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ complexity:
ignoreDeprecated: false
ignorePrivate: false
ignoreOverridden: false
ignoreAnnotatedFunctions: []

coroutines:
active: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class TooManyFunctions(config: Config = Config.empty) : Rule(config) {
@Configuration("ignore overridden functions")
private val ignoreOverridden: Boolean by config(false)

@Configuration("ignore functions annotated with these annotations")
private val ignoreAnnotatedFunctions: List<String> by config(emptyList())

private var amountOfTopLevelFunctions: Int = 0

override fun visitKtFile(file: KtFile) {
Expand Down Expand Up @@ -163,6 +166,7 @@ class TooManyFunctions(config: Config = Config.empty) : Rule(config) {
ignoreDeprecated && function.hasAnnotation(DEPRECATED) -> true
ignorePrivate && function.isPrivate() -> true
ignoreOverridden && function.isOverride() -> true
ignoreAnnotatedFunctions.any { function.hasAnnotation(it) } -> true
else -> false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ private const val THRESHOLD_IN_ENUMS = "thresholdInEnums"
private const val IGNORE_DEPRECATED = "ignoreDeprecated"
private const val IGNORE_PRIVATE = "ignorePrivate"
private const val IGNORE_OVERRIDDEN = "ignoreOverridden"
private const val IGNORE_ANNOTATED_FUNCTIONS = "ignoreAnnotatedFunctions"

class TooManyFunctionsSpec {
val rule = TooManyFunctions(
Expand Down Expand Up @@ -250,4 +251,61 @@ class TooManyFunctionsSpec {
assertThat(configuredRule.compileAndLint(code)).hasSize(1)
}
}

@Test
fun `should not count functions included in ignoreAnnotatedFunctions`() {
val rule = TooManyFunctions(
TestConfig(
THRESHOLD_IN_CLASSES to "2",
THRESHOLD_IN_ENUMS to "2",
THRESHOLD_IN_FILES to "2",
THRESHOLD_IN_INTERFACES to "2",
THRESHOLD_IN_OBJECTS to "2",
IGNORE_ANNOTATED_FUNCTIONS to listOf("Preview"),
)
)

val code = """
annotation class Preview
class A {
fun a() = Unit
@Preview
fun b() = Unit
@Preview
fun c() = Unit
}
""".trimIndent()

val findings = rule.compileAndLint(code)
assertThat(findings).isEmpty()
}

@Test
fun `should trigger when there are too many non-annotated functions`() {
val rule = TooManyFunctions(
TestConfig(
THRESHOLD_IN_CLASSES to "2",
THRESHOLD_IN_ENUMS to "2",
THRESHOLD_IN_FILES to "2",
THRESHOLD_IN_INTERFACES to "2",
THRESHOLD_IN_OBJECTS to "2",
IGNORE_ANNOTATED_FUNCTIONS to listOf("Preview"),
)
)

val code = """
annotation class Preview
class A {
fun a() = Unit
fun b() = Unit
@Preview
fun c() = Unit
@Preview
fun d() = Unit
}
""".trimIndent()

val findings = rule.compileAndLint(code)
assertThat(findings).hasSize(1)
}
}
12 changes: 12 additions & 0 deletions website/docs/introduction/compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ private fun FooLazyColumnPreview() { // Violation for FooLazyColumnPreview()
#### Recommended configuration
* Set `ignoreAnnotated` to `['Preview']`
*
### TooManyFunctions for Compose
See [TooManyFunctions](/docs/rules/complexity/#toomanyfunctions).
detekt may flag files with many composable preview functions, i.e. those marked with `@Preview`,
as having too many functions. Since preview functions do not contribute to complexity, this might not be
desired.
#### Recommended configuration
* Set `ignoreAnnotatedFunctions` to `['Preview']`

0 comments on commit e44aff7

Please sign in to comment.