Skip to content

Commit

Permalink
UndocumentedPublicProperty and UndocumentedPublicFunction should incl…
Browse files Browse the repository at this point in the history
…ude objects (#3940)

* Add test cases for objects with an undocumented public function

* Check the containing class or object instead of just the class

The class returned was null for functions inside an object, resulting in a false negative, so we instead use containingClassOrObject to determine if the containing object is public or not.

* Add test cases for objects with an undocumented public property

* Check the containing class or object instead of just the class when determining eligibility for UndocumentedPublicProperty check

containingClass() returns null when checking an object not nested in another class, making it ineligible to check. Switching to containingClassOrObject allows properties in an obect to be checked.

* Add test cases for interfaces to property and function documentation rules

* Fixing invalid kotlin in interface test
  • Loading branch information
anthonycr committed Jul 7, 2021
1 parent 40abc3f commit b2aba98
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.rules.isPublicNotOverridden
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.isPublic

/**
Expand Down Expand Up @@ -41,5 +41,5 @@ class UndocumentedPublicFunction(config: Config = Config.empty) : Rule(config) {
}

private fun KtNamedFunction.shouldBeDocumented() =
(isTopLevel || containingClass()?.isPublic == true) && isPublicNotOverridden()
(isTopLevel || containingClassOrObject?.isPublic == true) && isPublicNotOverridden()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.isPublic

Expand Down Expand Up @@ -61,7 +60,7 @@ class UndocumentedPublicProperty(config: Config = Config.empty) : Rule(config) {
private fun KtProperty.shouldBeDocumented() =
docComment == null && isTopLevelOrInPublicClass() && isPublicNotOverridden()

private fun KtProperty.isTopLevelOrInPublicClass() = isTopLevel || containingClass()?.isPublic == true
private fun KtProperty.isTopLevelOrInPublicClass() = isTopLevel || containingClassOrObject?.isPublic == true

private fun report(property: KtNamedDeclaration) {
report(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports undocumented public function in object") {
val code = """
object Test {
fun noComment1() {}
}
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports undocumented public function in nested object") {
val code = """
class Test {
object Test2 {
fun noComment1() {}
}
}
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports undocumented public functions in companion object") {
val code = """
class Test {
Expand All @@ -29,6 +49,15 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(2)
}

it("reports undocumented public function in an interface") {
val code = """
interface Test {
fun noComment1()
}
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("does not report documented public function") {
val code = """
/**
Expand Down Expand Up @@ -92,5 +121,14 @@ class UndocumentedPublicFunctionSpec : Spek({
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report public functions in private object") {
val code = """
private object Test {
fun noComment1() {}
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports undocumented public property in objects") {
val code = """
object Test {
val a = 1
}
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports undocumented public property in nested objects") {
val code = """
class Test {
object NestedTest {
val a = 1
}
}
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports undocumented public properties in companion object") {
val code = """
class Test {
Expand All @@ -27,6 +47,15 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(2)
}

it("reports undocumented public property in an interface") {
val code = """
interface Test {
val a: Int
}
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports undocumented public properties in a primary constructor") {
val code = "class Test(val a: Int)"
assertThat(subject.compileAndLint(code)).hasSize(1)
Expand Down Expand Up @@ -158,6 +187,15 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report undocumented public properties in private object") {
val code = """
private object Test {
val a = 1
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}

describe("public properties in nested classes") {

it("reports undocumented public properties in nested classes") {
Expand Down Expand Up @@ -186,7 +224,7 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports undocumented public properties inside objects") {
it("reports undocumented public properties in classes nested in objects") {
val code = """
object Outer {
class Inner {
Expand Down Expand Up @@ -242,7 +280,7 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(2)
}

it("reports undocumented public properties inside objects") {
it("reports undocumented public properties in classes nested in objects") {
val code = """
object Outer {
class Inner(val a: Int)
Expand Down

0 comments on commit b2aba98

Please sign in to comment.