diff --git a/detekt-core/src/main/resources/default-detekt-config.yml b/detekt-core/src/main/resources/default-detekt-config.yml index 38c7d4b3a13..ca229b4b698 100644 --- a/detekt-core/src/main/resources/default-detekt-config.yml +++ b/detekt-core/src/main/resources/default-detekt-config.yml @@ -62,14 +62,14 @@ comments: EndOfSentenceFormat: active: false endOfSentenceFormat: '([.?!][ \t\n\r\f<])|([.?!:]$)' + KDocReferencesNonPublicProperty: + active: false + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**'] OutdatedDocumentation: active: false matchTypeParameters: true matchDeclarationsOrder: true allowParamOnConstructorProperties: false - ReferencedEncapsulatedProperty: - active: false - excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**'] UndocumentedPublicClass: active: false excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**'] diff --git a/detekt-generator/src/main/kotlin/io/gitlab/arturbosch/detekt/generator/printer/defaultconfig/Exclusion.kt b/detekt-generator/src/main/kotlin/io/gitlab/arturbosch/detekt/generator/printer/defaultconfig/Exclusion.kt index 31082623265..380dfa87258 100644 --- a/detekt-generator/src/main/kotlin/io/gitlab/arturbosch/detekt/generator/printer/defaultconfig/Exclusion.kt +++ b/detekt-generator/src/main/kotlin/io/gitlab/arturbosch/detekt/generator/printer/defaultconfig/Exclusion.kt @@ -41,7 +41,7 @@ private object TestExclusions : Exclusions() { "UndocumentedPublicFunction", "UndocumentedPublicProperty", "UnsafeCallOnNullableType", - "ReferencedEncapsulatedProperty", + "KDocReferencesNonPublicProperty", ) } diff --git a/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/CommentSmellProvider.kt b/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/CommentSmellProvider.kt index 7d580a5b4eb..860996b59ca 100644 --- a/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/CommentSmellProvider.kt +++ b/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/CommentSmellProvider.kt @@ -25,7 +25,7 @@ class CommentSmellProvider : DefaultRuleSetProvider { UndocumentedPublicFunction(config), UndocumentedPublicProperty(config), AbsentOrWrongFileLicense(config), - ReferencedEncapsulatedProperty(config) + KDocReferencesNonPublicProperty(config) ) ) } diff --git a/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/ReferencedEncapsulatedProperty.kt b/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicProperty.kt similarity index 73% rename from detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/ReferencedEncapsulatedProperty.kt rename to detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicProperty.kt index 2cd084cbddf..7e2571187b7 100644 --- a/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/ReferencedEncapsulatedProperty.kt +++ b/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicProperty.kt @@ -16,31 +16,56 @@ import org.jetbrains.kotlin.psi.psiUtil.getTopmostParentOfType import org.jetbrains.kotlin.psi.psiUtil.isPublic /** - * This rule will report any KDoc comments that refer to encapsulated properties of a class. + * This rule will report any KDoc comments that refer to non-public properties of a class. * Clients do not need to know the implementation details. + * * See [Encapsulation](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)) + * + * + * /** + * * Comment + * * [prop1] - non-public property + * * [prop2] - public property + * */ + * class Test { + * private val prop1 = 0 + * val prop2 = 0 + * } + * + * + * + * /** + * * Comment + * * [prop2] - public property + * */ + * class Test { + * private val prop1 = 0 + * val prop2 = 0 + * } + * + * */ -class ReferencedEncapsulatedProperty(config: Config = Config.empty) : Rule(config) { +class KDocReferencesNonPublicProperty(config: Config = Config.empty) : Rule(config) { override val issue = Issue( javaClass.simpleName, Severity.Maintainability, - "KDoc comments should not refer to encapsulated properties.", + "KDoc comments should not refer to non-public properties.", Debt.FIVE_MINS ) override fun visitProperty(property: KtProperty) { + super.visitProperty(property) + val enclosingClass = property.getTopmostParentOfType() val comment = enclosingClass?.docComment?.text ?: return - if (property.isEncapsulatedInherited() && property.isReferencedInherited(comment)) { + if (property.isNonPublicInherited() && property.isReferencedInherited(comment)) { report(property) } - - super.visitProperty(property) } - private fun KtProperty.isEncapsulatedInherited(): Boolean { + private fun KtProperty.isNonPublicInherited(): Boolean { if (!isPublic) { return true } @@ -70,7 +95,7 @@ class ReferencedEncapsulatedProperty(config: Config = Config.empty) : Rule(confi issue, Entity.atName(property), "The property ${property.nameAsSafeName} " + - "is encapsulated and should not be referenced from KDoc comments." + "is non-public and should not be referenced from KDoc comments." ) ) } diff --git a/detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/ReferencedEncapsulatedPropertySpec.kt b/detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicPropertySpec.kt similarity index 69% rename from detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/ReferencedEncapsulatedPropertySpec.kt rename to detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicPropertySpec.kt index 9eeadd7059c..9d340dc9ec5 100644 --- a/detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/ReferencedEncapsulatedPropertySpec.kt +++ b/detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicPropertySpec.kt @@ -2,56 +2,54 @@ package io.gitlab.arturbosch.detekt.rules.documentation import io.gitlab.arturbosch.detekt.test.compileAndLint import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test -class ReferencedEncapsulatedPropertySpec { - val subject = ReferencedEncapsulatedProperty() +class KDocReferencesNonPublicPropertySpec { + val subject = KDocReferencesNonPublicProperty() - @Nested - inner class `ReferencedEncapsulatedProperty rule` { + inner class `KDocReferencesNonPublicProperty rule` { @Test - fun `reports referenced encapsulated properties`() { + fun `reports referenced non-public properties`() { val code = """ /** * Comment - * [prop1] - encapsulated property + * [prop1] - non-public property * [prop2] - public property */ class Test { - private val prop1 = 0 // report - val prop2 = 0 // do not report + private val prop1 = 0 + val prop2 = 0 } """.trimIndent() assertThat(subject.compileAndLint(code)).hasSize(1) } @Test - fun `reports referenced encapsulated properties in private class`() { + fun `reports referenced non-public properties in private class`() { val code = """ /** * Comment - * [prop1] - encapsulated property + * [prop1] - non-public property * [prop2] - public property */ private class Test { - private val prop1 = 0 // report - val prop2 = 0 // do not report + private val prop1 = 0 + val prop2 = 0 } """.trimIndent() assertThat(subject.compileAndLint(code)).hasSize(1) } @Test - fun `reports referenced encapsulated properties in nested objects`() { + fun `reports referenced non-public properties in nested objects`() { val code = """ /** * Comment - * [prop1] - encapsulated property - * [A.prop2] - encapsulated property - * [A.B.prop3] - encapsulated property - * [A.C.prop4] - encapsulated property + * [prop1] - non-public property + * [A.prop2] - non-public property + * [A.B.prop3] - non-public property + * [A.C.prop4] - non-public property */ class Test { private val prop1 = 0