Skip to content

Commit

Permalink
additional configuration for NoCallbacksInFunctions, upgrade Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
hbmartin committed Aug 29, 2023
1 parent 7ea27f3 commit 7e73ca8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ These are my opinions. There are many like them but these are mine. 😄

Inside of your `dependencies` block add the following: (for more details see [adding more rule sets](https://github.com/detekt/detekt#adding-more-rule-sets))
```kotlin
detektPlugins("com.github.hbmartin:hbmartin-detekt-rules:0.1.0")
detektPlugins("com.github.hbmartin:hbmartin-detekt-rules:0.1.1")
```

Then add to your detekt configuration as in the section below to activate rules. Note that the AvoidFirstOrLastOnList and AvoidMutableCollections rules require [type resolution](https://detekt.dev/docs/gettingstarted/type-resolution) to be active.
Expand All @@ -41,6 +41,8 @@ HbmartinRuleSet:
NoCallbacksInFunctions:
active: true
ignoreAnnotated: ['Composable']
allowExtensions: true
allowReceivers: true
NoNotNullOperator:
active: true
NoVarsInConstructor:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "1.9.0"
kotlin("jvm") version "1.9.10"
`maven-publish`
id("io.gitlab.arturbosch.detekt").version("1.23.1")
jacoco
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.config
import org.jetbrains.kotlin.psi.KtFunctionType
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration

class NoCallbacksInFunctions(config: Config) : Rule(config) {
override val issue = Issue(
Expand All @@ -19,17 +21,34 @@ class NoCallbacksInFunctions(config: Config) : Rule(config) {
debt = Debt.TWENTY_MINS,
)

@Suppress("BooleanPropertyNaming")
private val allowReceivers: Boolean by config(true)

@Suppress("BooleanPropertyNaming")
private val allowExtensions: Boolean by config(true)

override fun visitNamedFunction(function: KtNamedFunction) {
super.visitNamedFunction(function)
function.functionTypeParameters?.takeIf { it.isNotEmpty() }?.let {
report(
CodeSmell(
issue = issue,
entity = Entity.from(function),
message = "${function.name} should not have callbacks: ${it.joinToString()}",
),
)
}
if (allowExtensions && function.isExtensionDeclaration()) return
function
.functionTypeParameters
?.mapNotNull { paramText ->
if (paramText.contains(".(") && allowReceivers) {
null
} else {
paramText
}
}
?.takeIf { it.isNotEmpty() }
?.let {
report(
CodeSmell(
issue = issue,
entity = Entity.from(function),
message = "${function.name} should not have callbacks: ${it.joinToString()}",
),
)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ HbmartinRuleSet:
NoCallbacksInFunctions:
active: true
ignoreAnnotated: ['Composable']
allowExtensions: true
allowReceivers: true

WhenBranchSingleLineOrBraces:
active: true
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,27 @@ internal class NoCallbacksInFunctionsTest(private val env: KotlinCoreEnvironment
val findings = NoCallbacksInFunctions(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 1
}

@Test
fun `does not report extension function`() {
val code = """
private fun List<T>.forEach(function: (T) -> Unit) {
for (i in 0 until length) {
function(item(i))
}
}
"""
val findings = NoCallbacksInFunctions(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `does not report receiver function`() {
val code = """
fun mydsl(function: String.() -> Unit) {
}
"""
val findings = NoCallbacksInFunctions(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}
}

0 comments on commit 7e73ca8

Please sign in to comment.