All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gitlab.arturbosch.detekt.rules.Traversing.kt Maven / Gradle / Ivy

There is a newer version: 1.23.7
Show newest version
package io.gitlab.arturbosch.detekt.rules

import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.isPublic

/**
 * Returns a list of all parents of type [T] before first occurrence of [S].
 */
inline fun  KtElement.parentsOfTypeUntil(strict: Boolean = true) =
    sequence {
        var current: PsiElement? = if (strict) [email protected] else this@parentsOfTypeUntil
        while (current != null && current !is S) {
            if (current is T) {
                yield(current)
            }
            current = current.parent
        }
    }

fun KtNamedDeclaration.isPublicInherited(): Boolean = isPublicInherited(false)

fun KtNamedDeclaration.isPublicInherited(considerProtectedAsPublic: Boolean): Boolean {
    var classOrObject = containingClassOrObject
    while (classOrObject != null) {
        if (!classOrObject.isPublic && !(considerProtectedAsPublic && classOrObject.isProtected())) {
            return false
        }
        classOrObject = classOrObject.containingClassOrObject
    }
    return true
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy