ceedee
09/21/2023, 9:48 AM@Test
fun `properties are declared before functions`() {
Konsist
.scopeFromProject()
.classes()
.assert {
val lastKoPropertyDeclarationIndex = it
.declarations()
.indexOfLastInstance<KoPropertyDeclaration>()
val firstKoFunctionDeclarationIndex = it
.declarations()
.indexOfFirstInstance<KoFunctionDeclaration>()
if (lastKoPropertyDeclarationIndex != -1 && firstKoFunctionDeclarationIndex != -1) {
lastKoPropertyDeclarationIndex < firstKoFunctionDeclarationIndex
} else {
true
}
}
}
It wanted to include it but found that I get a lot a false positives, e.g. for local variables in methods. Am I doing something wrong? Is there a revised example which is working?Natalia Peterwas
09/21/2023, 10:48 AMincludeNested
and includeLocal
parameters from false
to true
.
This test should work when you change .declarations()
to .declarations(includeNested = false, includeLocal = false)
:
@Test
fun `properties are declared before functions`() {
Konsist
.scopeFromProject()
.classes()
.assert {
val lastKoPropertyDeclarationIndex = it
.declarations(includeNested = false, includeLocal = false)
.indexOfLastInstance<KoPropertyDeclaration>()
val firstKoFunctionDeclarationIndex = it
.declarations(includeNested = false, includeLocal = false)
.indexOfFirstInstance<KoFunctionDeclaration>()
if (lastKoPropertyDeclarationIndex != -1 && firstKoFunctionDeclarationIndex != -1) {
lastKoPropertyDeclarationIndex < firstKoFunctionDeclarationIndex
} else {
true
}
}
}
Let me know if it works now 🙂 I will also correct this mistake in the documentation 🙂
Thanks for the contribution 🙏ceedee
09/21/2023, 12:29 PM