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

ceedee

09/21/2023, 9:59 AM
Another sample not working is this one:
Copy code
@Test
fun `companion object is last declaration in the class`() {
    Konsist
        .scopeFromProject()
        .classes()
        .assert {
            val companionObject = it.objects().lastOrNull { obj ->
                obj.hasModifiers(KoModifier.COMPANION)
            }

            companionObject != null && it.declarations().last() == companionObject
        }
}
It catches variables in the Companion object in
it.declarations().last()
. Will that be reworked?
n

Natalia Peterwas

09/21/2023, 10:51 AM
As above - does not work due to changing the default values of the
includeNested
and
includeLocal
parameters. However, I found another error, so the correct test is below:
Copy code
@Test
    fun `companion object is last declaration in the class`() {
        Konsist
            .scopeFromProject()
            .classes()
            .assert {
                val companionObject = it.objects(includeNested = false).lastOrNull { obj ->
                    obj.hasModifier(KoModifier.COMPANION)
                }

                if (companionObject != null) {
                    it.declarations(includeNested = false, includeLocal = false).last() == companionObject
                } else {
                    true
                }
            }
    }
c

ceedee

09/21/2023, 12:38 PM
This also works for me, thank you very much! (also ran into the issue with failing for objects which have no companion, thanks for that fix!)
❤️ 1