From 5c98d42ec08ca1413f920e4f5dde28d330e8837a Mon Sep 17 00:00:00 2001 From: Vadim Mishenev Date: Thu, 23 Dec 2021 12:05:59 +0300 Subject: Allow filtering of property setter (#2220) * Allow filtering of property setter * Improve code style --- .../DocumentableVisibilityFilterTransformer.kt | 48 +++++++++++++++++++--- ...ssedByConditionDocumentableFilterTransformer.kt | 19 +++++++-- .../src/test/kotlin/filter/VisibilityFilterTest.kt | 31 ++++++++++++++ .../kotlin/transformers/SuppressTagFilterTest.kt | 20 +++++++++ 4 files changed, 110 insertions(+), 8 deletions(-) (limited to 'plugins/base/src') diff --git a/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt index 8b3b4304..bd39b040 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt @@ -81,8 +81,12 @@ class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMe return Pair(packagesListChanged, filteredPackages) } + @Suppress("UNUSED_PARAMETER") private fun alwaysTrue(a: T, p: DokkaSourceSet) = true + @Suppress("UNUSED_PARAMETER") private fun alwaysFalse(a: T, p: DokkaSourceSet) = false + @Suppress("UNUSED_PARAMETER") + private fun alwaysNoModify(a: T, sourceSets: Set) = false to a private fun WithVisibility.visibilityForPlatform(data: DokkaSourceSet): Visibility? = visibility[data] @@ -99,13 +103,18 @@ class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMe private fun List.transform( additionalCondition: (T, DokkaSourceSet) -> Boolean = ::alwaysTrue, alternativeCondition: (T, DokkaSourceSet) -> Boolean = ::alwaysFalse, - recreate: (T, Set) -> T + modify: (T, Set) -> Pair = ::alwaysNoModify, + recreate: (T, Set) -> T, ): Pair> where T : Documentable, T : WithVisibility { var changed = false val values = mapNotNull { t -> val filteredPlatforms = t.filterPlatforms(additionalCondition, alternativeCondition) when (filteredPlatforms.size) { - t.visibility.size -> t + t.visibility.size -> { + val (wasChanged, element) = modify(t, filteredPlatforms) + changed = changed || wasChanged + element + } 0 -> { changed = true null @@ -142,9 +151,35 @@ class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMe private fun filterProperties( properties: List, - additionalCondition: (DProperty, DokkaSourceSet) -> Boolean = ::alwaysTrue - ): Pair> = - properties.transform(additionalCondition, ::hasVisibleAccessorsForPlatform) { original, filteredPlatforms -> + additionalCondition: (DProperty, DokkaSourceSet) -> Boolean = ::alwaysTrue, + additionalConditionAccessors: (DFunction, DokkaSourceSet) -> Boolean = ::alwaysTrue + ): Pair> { + + val modifier: (DProperty, Set) -> Pair = + { original, filteredPlatforms -> + val setter = original.setter?.let { filterFunctions(listOf(it), additionalConditionAccessors) } + val getter = original.getter?.let { filterFunctions(listOf(it), additionalConditionAccessors) } + + val modified = setter?.first == true || getter?.first == true + + val property = + if (modified) + original.copy( + setter = setter?.second?.firstOrNull(), + getter = getter?.second?.firstOrNull() + ) + else original + modified to property + } + + return properties.transform( + additionalCondition, + ::hasVisibleAccessorsForPlatform, + modifier + ) { original, filteredPlatforms -> + val setter = original.setter?.let { filterFunctions(listOf(it), additionalConditionAccessors) } + val getter = original.getter?.let { filterFunctions(listOf(it), additionalConditionAccessors) } + with(original) { copy( documentation = documentation.filtered(filteredPlatforms), @@ -153,9 +188,12 @@ class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMe visibility = visibility.filtered(filteredPlatforms), sourceSets = filteredPlatforms, generics = generics.mapNotNull { it.filter(filteredPlatforms) }, + setter = setter?.second?.firstOrNull(), + getter = getter?.second?.firstOrNull() ) } } + } private fun filterEnumEntries(entries: List, filteredPlatforms: Set): Pair> = entries.foldRight(Pair(false, emptyList())) { entry, acc -> diff --git a/plugins/base/src/main/kotlin/transformers/documentables/SuppressedByConditionDocumentableFilterTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/SuppressedByConditionDocumentableFilterTransformer.kt index 0bcb6fd5..918ff1bf 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/SuppressedByConditionDocumentableFilterTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/SuppressedByConditionDocumentableFilterTransformer.kt @@ -27,7 +27,7 @@ abstract class SuppressedByConditionDocumentableFilterTransformer(val context: D val classlikes = dPackage.classlikes.map { processClassLike(it) } val typeAliases = dPackage.typealiases.map { processMember(it) } val functions = dPackage.functions.map { processMember(it) } - val properies = dPackage.properties.map { processMember(it) } + val properies = dPackage.properties.map { processProperty(it) } val wasChanged = (classlikes + typeAliases + functions + properies).any { it.changed } return (dPackage.takeIf { !wasChanged } ?: dPackage.copy( @@ -43,7 +43,7 @@ abstract class SuppressedByConditionDocumentableFilterTransformer(val context: D val functions = classlike.functions.map { processMember(it) } val classlikes = classlike.classlikes.map { processClassLike(it) } - val properties = classlike.properties.map { processMember(it) } + val properties = classlike.properties.map { processProperty(it) } val companion = (classlike as? WithCompanion)?.companion?.let { processClassLike(it) } val wasClasslikeChanged = (functions + classlikes + properties).any { it.changed } || companion?.changed == true @@ -104,7 +104,7 @@ abstract class SuppressedByConditionDocumentableFilterTransformer(val context: D if (shouldBeSuppressed(dEnumEntry)) return DocumentableWithChanges.filteredDocumentable() val functions = dEnumEntry.functions.map { processMember(it) } - val properties = dEnumEntry.properties.map { processMember(it) } + val properties = dEnumEntry.properties.map { processProperty(it) } val classlikes = dEnumEntry.classlikes.map { processClassLike(it) } val wasChanged = (functions + properties + classlikes).any { it.changed } @@ -115,6 +115,19 @@ abstract class SuppressedByConditionDocumentableFilterTransformer(val context: D )).let { DocumentableWithChanges(it, wasChanged) } } + private fun processProperty(dProperty: DProperty): DocumentableWithChanges { + if (shouldBeSuppressed(dProperty)) return DocumentableWithChanges.filteredDocumentable() + + val getter = dProperty.getter?.let { processMember(it) } ?: DocumentableWithChanges(null, false) + val setter = dProperty.setter?.let { processMember(it) } ?: DocumentableWithChanges(null, false) + + val wasChanged = getter.changed || setter.changed + return (dProperty.takeIf { !wasChanged } ?: dProperty.copy( + getter = getter.documentable, + setter = setter.documentable + )).let { DocumentableWithChanges(it, wasChanged) } + } + private fun processMember(member: T): DocumentableWithChanges = if (shouldBeSuppressed(member)) DocumentableWithChanges.filteredDocumentable() else DocumentableWithChanges(member, false) diff --git a/plugins/base/src/test/kotlin/filter/VisibilityFilterTest.kt b/plugins/base/src/test/kotlin/filter/VisibilityFilterTest.kt index ffefaaea..1bb2d5d0 100644 --- a/plugins/base/src/test/kotlin/filter/VisibilityFilterTest.kt +++ b/plugins/base/src/test/kotlin/filter/VisibilityFilterTest.kt @@ -104,6 +104,37 @@ class VisibilityFilterTest : BaseAbstractTest() { } } + @Test + fun `private setter with false global includeNonPublic`() { + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + includeNonPublic = false + sourceRoots = listOf("src/main/kotlin/basic/Test.kt") + } + } + } + + testInline( + """ + |/src/main/kotlin/basic/Test.kt + |package example + | + |var property: Int = 0 + |private set + | + | + """.trimMargin(), + configuration + ) { + preMergeDocumentablesTransformationStage = { + Assertions.assertNull( + it.first().packages.first().properties.first().setter + ) + } + } + } + @Test fun `private function with false global true package includeNonPublic`() { val configuration = dokkaConfiguration { diff --git a/plugins/base/src/test/kotlin/transformers/SuppressTagFilterTest.kt b/plugins/base/src/test/kotlin/transformers/SuppressTagFilterTest.kt index fb8f520e..de1ee46c 100644 --- a/plugins/base/src/test/kotlin/transformers/SuppressTagFilterTest.kt +++ b/plugins/base/src/test/kotlin/transformers/SuppressTagFilterTest.kt @@ -88,6 +88,26 @@ class SuppressTagFilterTest : BaseAbstractTest() { } } + @Test + fun `should filter setter`() { + testInline( + """ + |/src/suppressed/Suppressed.kt + |var property: Int + |/** @suppress */ + |private set + """.trimIndent(), configuration + ) { + preMergeDocumentablesTransformationStage = { modules -> + val prop = modules.flatMap { it.packages }.flatMap { it.properties } + .find { it.name == "property" } + assertNotNull(prop) + assertNotNull(prop.getter) + assertNull(prop.setter) + } + } + } + @Test fun `should filter top level type aliases`() { testInline( -- cgit