Skip to content

Commit

Permalink
Suppress RedundantVisibilityModifierRule if explicit API mode enabled (
Browse files Browse the repository at this point in the history
…#3175)

* Suppress RedundantVisibilityModifierRule if explicit API mode enabled

Explicit API mode was added in Kotlin 1.4
It prevents libraries' authors from making APIs public unintentionally.
In this mode, the visibility modifier should be defined explicitly even if it is public.
See: https://kotlinlang.org/docs/reference/whatsnew14.html#explicit-api-mode-for-library-authors

Closes #3125

* Add explicit API mode to rule documentation
  • Loading branch information
schalkms committed Oct 27, 2020
1 parent b5a423f commit 7b4fc73
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Expand Up @@ -10,6 +10,8 @@ import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.rules.isInternal
import io.gitlab.arturbosch.detekt.rules.isOverride
import org.jetbrains.kotlin.config.AnalysisFlags
import org.jetbrains.kotlin.config.ExplicitApiMode
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtDeclaration
Expand All @@ -22,6 +24,11 @@ import org.jetbrains.kotlin.psi.psiUtil.isPrivate

/**
* This rule checks for redundant visibility modifiers.
* One exemption is the
* [explicit API mode](https://kotlinlang.org/docs/reference/whatsnew14.html#explicit-api-mode-for-library-authors)
* In this mode, the visibility modifier should be defined explicitly even if it is public.
* Hence, the rule ignores the visibility modifiers in explicit API mode.
*
* <noncompliant>
* public interface Foo { // public per default
Expand Down Expand Up @@ -55,11 +62,21 @@ class RedundantVisibilityModifierRule(config: Config = Config.empty) : Rule(conf

private fun KtModifierListOwner.isExplicitlyPublic() = this.hasModifier(KtTokens.PUBLIC_KEYWORD)

/**
* Explicit API mode was added in Kotlin 1.4
* It prevents libraries' authors from making APIs public unintentionally.
* In this mode, the visibility modifier should be defined explicitly even if it is public.
* See: https://kotlinlang.org/docs/reference/whatsnew14.html#explicit-api-mode-for-library-authors
*/
private fun isExplicitApiModeActive() = AnalysisFlags.explicitApiMode.defaultValue == ExplicitApiMode.STRICT

override fun visitKtFile(file: KtFile) {
super.visitKtFile(file)
file.declarations.forEach {
it.accept(classVisitor)
it.acceptChildren(childrenVisitor)
if (!isExplicitApiModeActive()) {
file.declarations.forEach {
it.accept(classVisitor)
it.acceptChildren(childrenVisitor)
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions docs/pages/documentation/style.md
Expand Up @@ -1006,6 +1006,11 @@ val x = "string"

This rule checks for redundant visibility modifiers.

One exemption is the
[explicit API mode](https://kotlinlang.org/docs/reference/whatsnew14.html#explicit-api-mode-for-library-authors)
In this mode, the visibility modifier should be defined explicitly even if it is public.
Hence, the rule ignores the visibility modifiers in explicit API mode.

**Severity**: Style

**Debt**: 5min
Expand Down

0 comments on commit 7b4fc73

Please sign in to comment.