# detekt

Paolo Rotolo

12/06/2023, 12:11 PM
Hey! Sorry, I need a bit of help with this PR: My idea is: - Take a function (f) annotated with
(org.junit.Test) - Check if there are coroutine launches outside runTest block - Collect all the functions of the same codebase called in (f) and perform the same check on them and so on. I’m new to PSI APIs and I didn’t find a lot of documentation online, but I’m very curious to learn more. I think I’ve got the first two steps in my draft PR, but I’ve no idea which API to use to retrieve all the function calls in the scope of the
function that could launch a coroutine internally. I’ve also explored the inverse path: visiting all the functions using Visitor’s
and finding if there’s a
function parent, but it would be a less optimized solution I guess?
👀 1


12/07/2023, 12:18 PM
I’m new to PSI APIs and I didn’t find a lot of documentation online
That’s sadly true for all the PSI related questions 😞
🫠 1
The best would be to search for similar rules inside the Detekt codebase that achieve a similar result

Paolo Rotolo

12/07/2023, 1:33 PM
yeah, I've found some rules that do the inverse (from function, take parent)... I'll try to investigate more later 😕
After experimenting with PSI, I think I've found a way of solving it with:
Copy code
function.collectDescendantsOfType<KtExpression>().mapNotNull {
                    ?.getPsi() as? KtNamedFunction 
Will update the PR soon
❤️ 1

Atul Gupta

12/12/2023, 9:04 PM
I would try to traverse in and see if any function is calls a function which launches coroutine(maybe based on signature) something like below
Copy code
expression.anyDescendantOfType<KtExpression>(::shouldTraverseInside) { descendant ->
}.ifTrue { report(expression) }
You can refer
which finds the call to
then checks each child function for possible violations
👍 1