https://kotlinlang.org logo
#konsist
Title
# konsist
f

Filip Rozanski

03/29/2024, 3:06 PM
Does in version
0.14.0
it is possible to propagate from some scope to their parents? In version
0.13.0
it was possible by using
KoContainingDeclarationProvider.containingDeclaration: KoContainingDeclarationProvider?
, but in version
0.14.0
returns
KoContainingDeclarationProvider.containingDeclaration: KoBaseDeclaration
Example: In version
0.13.0
I write extension function to collects all annotations from given scope and their parents. Let's assume I have a variable with some annotation. Calling
annotationsAndParentAnnotations
on that variable will collect annotations on this variable, then add annotations on function (if variable is defined inside function), then add annotations on class (if function is inside class), then add annotation on file
Copy code
fun KoContainingDeclarationProvider.annotationsAndParentAnnotations(): List<KoAnnotationDeclaration> =
    this.annotations() + (this.containingDeclaration?.annotationsAndParentAnnotations() ?: emptyList())
I was using mechanism of annotations for exclusions
Copy code
val excludeRuleSuppressValue = "VarKeyword"
val varProperties = allCodeScope.files
    .properties()
    .filter { it.hasVarModifier }
    .filterNot {
        it.annotationsAndParentAnnotations().hasSuppressAnnotationWith(excludeRuleSuppressValue)
    }