Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SlotReusedDetector #441

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import com.android.tools.lint.detector.api.JavaContext
import com.android.tools.lint.detector.api.Severity
import com.android.tools.lint.detector.api.SourceCodeScanner
import com.android.tools.lint.detector.api.TextFormat
import com.intellij.codeInsight.PsiEquivalenceUtil
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UMethod
import org.jetbrains.uast.toUElement
import org.jetbrains.uast.tryResolve
import slack.lint.compose.util.Priorities
import slack.lint.compose.util.findChildrenByClass
import slack.lint.compose.util.slotParameters
Expand Down Expand Up @@ -46,37 +47,22 @@ class SlotReusedDetector : ComposableFunctionDetector(), SourceCodeScanner {
val slotParameters = method.slotParameters(context.evaluator)

val callExpressions = composableBlockExpression.findChildrenByClass<KtCallExpression>().toList()
val innerLambdas = composableBlockExpression.findChildrenByClass<KtLambdaExpression>().toList()

slotParameters.forEach { slotParameter ->
// Find lambdas that shadow this parameter name, to make sure that they aren't shadowing
// the references we are looking through
val lambdasWithMatchingParameterName =
innerLambdas.filter { innerLambda ->
// Otherwise look to see if any of the parameters on the inner lambda have the
// same name
innerLambda.valueParameters
// Ignore parameters with a destructuring declaration instead of a named
// parameter
.filter { it.destructuringDeclaration == null }
.any { it.name == slotParameter.name }
}

// Count all direct calls of the slot parameter.
// NOTE: this misses cases where the slot parameter is passed as an argument to another
// method, which may or may not invoke the slot parameter, but there are cases where that is
// valid, like using the slot parameter as the key for a remember
val slotParameterCallCount =
callExpressions
.filter { reference ->
// The parameter is referenced if there is at least one reference that isn't shadowed by
// an inner lambda
lambdasWithMatchingParameterName.none { it.isAncestor(reference) }
}
.count { reference ->
(reference.calleeExpression as? KtNameReferenceExpression)?.getReferencedName() ==
slotParameter.name
}
callExpressions.count { callExpression ->
val calleeElement: PsiElement? =
callExpression.calleeExpression?.toUElement()?.tryResolve()
val slotElement: PsiElement? = slotParameter.sourceElement

calleeElement != null &&
slotElement != null &&
PsiEquivalenceUtil.areElementsEquivalent(calleeElement, slotElement)
}

// Report an issue if the slot parameter was invoked in multiple places
if (slotParameterCallCount > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,60 @@ class SlotReusedDetectorTest : BaseComposeLintTest() {

lint().files(*commonStubs, kotlin(code)).run().expectClean()
}

@Test
fun `passes when using same name is used as a different function`() {
@Language("kotlin")
val code =
"""
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
fun Something(
modifier: Modifier = Modifier,
first: @Composable () -> Unit,
) {
Box(modifier) {
first()
listOf("1").first { it == "2" }
}
}

"""
.trimIndent()

lint().files(*commonStubs, kotlin(code)).run().expectClean()
}

@Test
fun `passes when using same slot name is used with a different receiver`() {
@Language("kotlin")
val code =
"""
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

class Section(
// other stuff
val content: @Composable () -> Unit,
)

@Composable
fun Something(
modifier: Modifier = Modifier,
section: Section? = null,
content: @Composable () -> Unit,
) {
Box(modifier) {
content()
section?.content()
}
}

"""
.trimIndent()

lint().files(*commonStubs, kotlin(code)).run().expectClean()
}
}