Skip to content

Commit

Permalink
Improve SlotReusedDetector
Browse files Browse the repository at this point in the history
By resolving the call references to check against the parameter, this allows removing the custom shadowing logic, and also fixes cases where a different reference with the same name as the slot was causing false positives.

Fixes #424 and fixes #434
  • Loading branch information
alexvanyo committed Oct 21, 2024
1 parent 6ffb669 commit 5919083
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import com.android.tools.lint.detector.api.SourceCodeScanner
import com.android.tools.lint.detector.api.TextFormat
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 +45,16 @@ 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 { reference ->
reference.calleeExpression?.toUElement()?.tryResolve() == slotParameter.sourceElement
}

// 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()
}
}

0 comments on commit 5919083

Please sign in to comment.