Skip to content

Commit

Permalink
Rename rule to 'KDocReferencesNonPublicProperty' and fix naming
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyVPinchuk committed Apr 26, 2022
1 parent 501473b commit b8700b6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
6 changes: 3 additions & 3 deletions detekt-core/src/main/resources/default-detekt-config.yml
Expand Up @@ -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/**']
Expand Down
Expand Up @@ -41,7 +41,7 @@ private object TestExclusions : Exclusions() {
"UndocumentedPublicFunction",
"UndocumentedPublicProperty",
"UnsafeCallOnNullableType",
"ReferencedEncapsulatedProperty",
"KDocReferencesNonPublicProperty",
)
}

Expand Down
Expand Up @@ -25,7 +25,7 @@ class CommentSmellProvider : DefaultRuleSetProvider {
UndocumentedPublicFunction(config),
UndocumentedPublicProperty(config),
AbsentOrWrongFileLicense(config),
ReferencedEncapsulatedProperty(config)
KDocReferencesNonPublicProperty(config)
)
)
}
Expand Up @@ -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))
*
* <noncompliant>
* /**
* * Comment
* * [prop1] - non-public property
* * [prop2] - public property
* */
* class Test {
* private val prop1 = 0
* val prop2 = 0
* }
* </noncompliant>
*
* <compliant>
* /**
* * Comment
* * [prop2] - public property
* */
* class Test {
* private val prop1 = 0
* val prop2 = 0
* }
* </compliant>
*
*/
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<KtClass>()
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
}
Expand Down Expand Up @@ -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."
)
)
}
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit b8700b6

Please sign in to comment.