Emanuel Moecklin
10/16/2023, 6:17 PMviewLifecycleOwner.lifecycleScope
over lifecycleScope
to launch a Coroutine so
// BAD
lifecycleScope.launch {
// here goes my code
}
// GOOD
viewLifecycleOwner.lifecycleScope.launch {
// here goes my code
}
Unfortunately both add the same import so I can't check for imports. Any ideas how this could be done?Yonatan Karp-Rudin
10/16/2023, 8:19 PM@Test
fun `coroutine scope usage of 'lifecycleScope.launch' in file is forbiden`() {
Konsist
.scopeFromProject()
.files
.assertFalse { it.text.contains("lifecycleScope.launch") }
}
You can see the original example here.Emanuel Moecklin
10/16/2023, 8:49 PM// matches `lifecycleScope.launch` but doesn't match `viewLifecycleOwner.lifecycleScope.launch`
private val launchRegex = "[^\\.]+lifecycleScope\\.launch".toRegex()
Konsist
.scopeFromProject()
.classes()
.withAllParentsOf(Fragment::class)
.assertFalse {
it.text.contains(launchRegex)
}
igor.wojda
10/17/2023, 8:55 AM