Hey! I hope you're doing well. I'm working on a Ko...
# konsist
d
Hey! I hope you're doing well. I'm working on a Kotlin project and using your Konsist library. I've gone through the documentation and I see that the library supports generic types, but I'm still a bit confused as I'm new to this. My goal is to create a static analysis rule that ensure all classes extended from
BaseViewModel
use a generic type whose name ends with "ScreenState". Could you please provide some guidance or an example of how to achieve this? Thank you for your help! Example: class HomeViewModel : BaseViewModel<HomeScreenState>() // Correct class ProfileViewModel : BaseViewModel<ProfileState>() // Incorrect
p
I think this is not possible currently. You can only use a workaround like below that is asserting the text representation. But this is probably very error prone:
Copy code
Konsist.scopeFromProduction()
     .classes()
     .withParentClassOf(BaseViewModel::class)
     .assertTrue {
         it.text.contains(Regex(": BaseViewModel<[^>]+ScreenState>"))
     }
👍 1
i
For now
text
cna be used, but it like API could be improved here. I have created ticket https://lemonappdev.atlassian.net/browse/KON-626 BTW this is called
generic type argument
(as opppose to
class generic type parameter
defined in
BaseViewModel
lcass, usuallynamed
T
)
👍 2
n
Konsist 0.17.0 has been released! This release includes this improvement 🙂 The following test should be helpful in your case:
Copy code
Konsist
    .scopeFromProduction()
    .classes()
    .withParentClassOf(BaseViewModel::class)
    .parents()
    .assertTrue {
        it.typeArguments
            ?.firstOrNull()
            ?.hasNameEndingWith("ScreenState")
    }
👍 1