File tree 6 files changed +91
-65
lines changed
kotlin/com/github/ivy/explicit
resources/META-INF/services
test/kotlin/com/github/ivy/explicit
6 files changed +91
-65
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -4,14 +4,14 @@ import io.gitlab.arturbosch.detekt.api.Config
4
4
import io.gitlab.arturbosch.detekt.api.RuleSet
5
5
import io.gitlab.arturbosch.detekt.api.RuleSetProvider
6
6
7
- class MyRuleSetProvider : RuleSetProvider {
8
- override val ruleSetId: String = " MyRuleSet "
7
+ class IvyExplicitRuleSetProvider : RuleSetProvider {
8
+ override val ruleSetId: String = " IvyExplicit "
9
9
10
10
override fun instance (config : Config ): RuleSet {
11
11
return RuleSet (
12
12
ruleSetId,
13
13
listOf (
14
- MyRule (config),
14
+ DataClassFunctionsRule (config),
15
15
),
16
16
)
17
17
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- com.github.ivy.explicit.MyRuleSetProvider
1
+ com.github.ivy.explicit.IvyExplicitRuleSetProvider
Original file line number Diff line number Diff line change
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
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments