Add options to detect private / internal API with implicit type
This commit is contained in:
@@ -5,15 +5,21 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.inspections
|
package org.jetbrains.kotlin.idea.inspections
|
||||||
|
|
||||||
|
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel
|
||||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||||
import org.jetbrains.kotlin.descriptors.effectiveVisibility
|
import org.jetbrains.kotlin.descriptors.effectiveVisibility
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||||
import org.jetbrains.kotlin.psi.KtFunction
|
import org.jetbrains.kotlin.psi.KtFunction
|
||||||
import org.jetbrains.kotlin.psi.KtProperty
|
import org.jetbrains.kotlin.psi.KtProperty
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
||||||
|
import javax.swing.JComponent
|
||||||
|
|
||||||
class PublicApiImplicitTypeInspection : AbstractImplicitTypeInspection(
|
class PublicApiImplicitTypeInspection(
|
||||||
{ element, _ ->
|
@JvmField var reportInternal: Boolean = false,
|
||||||
|
@JvmField var reportPrivate: Boolean = false
|
||||||
|
) : AbstractImplicitTypeInspection(
|
||||||
|
{ element, inspection ->
|
||||||
element.containingClassOrObject?.isLocal != true &&
|
element.containingClassOrObject?.isLocal != true &&
|
||||||
when (element) {
|
when (element) {
|
||||||
is KtFunction -> !element.isLocal
|
is KtFunction -> !element.isLocal
|
||||||
@@ -21,9 +27,28 @@ class PublicApiImplicitTypeInspection : AbstractImplicitTypeInspection(
|
|||||||
else -> false
|
else -> false
|
||||||
} && run {
|
} && run {
|
||||||
val callableMemberDescriptor = element.resolveToDescriptorIfAny() as? CallableMemberDescriptor
|
val callableMemberDescriptor = element.resolveToDescriptorIfAny() as? CallableMemberDescriptor
|
||||||
callableMemberDescriptor?.effectiveVisibility()?.toVisibility()?.isPublicAPI == true
|
val forInternal = (inspection as PublicApiImplicitTypeInspection).reportInternal
|
||||||
|
val forPrivate = inspection.reportPrivate
|
||||||
|
|
||||||
|
val visibility = callableMemberDescriptor?.effectiveVisibility()?.toVisibility()
|
||||||
|
visibility?.isPublicAPI == true || (forInternal && visibility == Visibilities.INTERNAL) ||
|
||||||
|
(forPrivate && visibility == Visibilities.PRIVATE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
override val problemText = "For API stability, it's recommended to specify explicitly public & protected declaration types"
|
|
||||||
|
override val problemText: String
|
||||||
|
get() {
|
||||||
|
return if (!reportInternal && !reportPrivate)
|
||||||
|
"For API stability, it's recommended to specify explicitly public & protected declaration types"
|
||||||
|
else
|
||||||
|
"For API stability, it's recommended to specify explicitly declaration types"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun createOptionsPanel(): JComponent? {
|
||||||
|
val panel = MultipleCheckboxOptionsPanel(this)
|
||||||
|
panel.addCheckbox("Apply also to internal members", "reportInternal")
|
||||||
|
panel.addCheckbox("Apply also to private members", "reportPrivate")
|
||||||
|
return panel
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user