Skip to content

Commit 30c091a

Browse files
Define the DataClassFunctions rule
1 parent 9c2d873 commit 30c091a

File tree

6 files changed

+91
-65
lines changed

6 files changed

+91
-65
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.ivy.explicit
2+
3+
import io.gitlab.arturbosch.detekt.api.*
4+
import org.jetbrains.kotlin.psi.KtClass
5+
import org.jetbrains.kotlin.psi.KtNamedFunction
6+
7+
class DataClassFunctionsRule(config: Config) : Rule(config) {
8+
override val issue = Issue(
9+
id = "DataClassFunctions",
10+
severity = Severity.Maintainability,
11+
description = "Data classes should not define behavior. " +
12+
"Their purpose is to model data.",
13+
debt = Debt.TWENTY_MINS,
14+
)
15+
16+
override fun visitClass(klass: KtClass) {
17+
super.visitClass(klass)
18+
if (klass.isData()) {
19+
klass.body?.declarations
20+
?.filterIsInstance<KtNamedFunction>()
21+
?.forEach { function ->
22+
report(
23+
CodeSmell(
24+
issue,
25+
Entity.from(function),
26+
message = "Data class '${klass.name}' should not contain functions. " +
27+
"Found: function ${function.name}()."
28+
)
29+
)
30+
}
31+
}
32+
}
33+
}

src/main/kotlin/com/github/ivy/explicit/MyRuleSetProvider.kt src/main/kotlin/com/github/ivy/explicit/IvyExplicitRuleSetProvider.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import io.gitlab.arturbosch.detekt.api.Config
44
import io.gitlab.arturbosch.detekt.api.RuleSet
55
import io.gitlab.arturbosch.detekt.api.RuleSetProvider
66

7-
class MyRuleSetProvider : RuleSetProvider {
8-
override val ruleSetId: String = "MyRuleSet"
7+
class IvyExplicitRuleSetProvider : RuleSetProvider {
8+
override val ruleSetId: String = "IvyExplicit"
99

1010
override fun instance(config: Config): RuleSet {
1111
return RuleSet(
1212
ruleSetId,
1313
listOf(
14-
MyRule(config),
14+
DataClassFunctionsRule(config),
1515
),
1616
)
1717
}

src/main/kotlin/com/github/ivy/explicit/MyRule.kt

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
com.github.ivy.explicit.MyRuleSetProvider
1+
com.github.ivy.explicit.IvyExplicitRuleSetProvider
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.github.ivy.explicit
2+
3+
import io.gitlab.arturbosch.detekt.api.Config
4+
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
5+
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
6+
import io.kotest.matchers.collections.shouldHaveSize
7+
import io.kotest.matchers.string.shouldNotBeBlank
8+
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
9+
import org.junit.jupiter.api.Test
10+
11+
@KotlinCoreEnvironmentTest
12+
internal class DataClassFunctionsRuleTest(private val env: KotlinCoreEnvironment) {
13+
14+
@Test
15+
fun `reports data class having functions`() {
16+
val code = """
17+
data class A(
18+
val x: Int
19+
) {
20+
fun a() = 42
21+
}
22+
"""
23+
val findings = DataClassFunctionsRule(Config.empty).compileAndLintWithContext(env, code)
24+
findings shouldHaveSize 1
25+
val message = findings.first().message
26+
message.shouldNotBeBlank()
27+
}
28+
29+
@Test
30+
fun `doesn't report data class without functions`() {
31+
val code = """
32+
data class A(
33+
val x: Int
34+
)
35+
"""
36+
val findings = DataClassFunctionsRule(Config.empty).compileAndLintWithContext(env, code)
37+
findings shouldHaveSize 0
38+
}
39+
40+
@Test
41+
fun `doesn't report data class with companion object`() {
42+
val code = """
43+
data class A(
44+
val x: Int
45+
) {
46+
companion object {
47+
fun a() = 42
48+
}
49+
}
50+
"""
51+
val findings = DataClassFunctionsRule(Config.empty).compileAndLintWithContext(env, code)
52+
findings shouldHaveSize 0
53+
}
54+
}

src/test/kotlin/com/github/ivy/explicit/MyRuleTest.kt

-34
This file was deleted.

0 commit comments

Comments
 (0)