Skip to content

Commit

Permalink
Allow nullable types for trailing lambdas in ComposeParameterOrder (#134
Browse files Browse the repository at this point in the history
)

Fixed upstream in mrmans0n/compose-rules#61

Functional types weren't being taken into account when computing the desired order of parameters in composables when the function was nullable. This fix addressed that by, in that case, targeting the innerType of the KtNullableType.
  • Loading branch information
mrmans0n authored Jun 9, 2023
1 parent 3564200 commit eeb3989
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.android.tools.lint.detector.api.Severity
import com.android.tools.lint.detector.api.SourceCodeScanner
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtFunctionType
import org.jetbrains.kotlin.psi.KtNullableType
import org.jetbrains.kotlin.psi.KtParameter
import slack.lint.compose.util.Priorities
import slack.lint.compose.util.isModifier
Expand Down Expand Up @@ -97,5 +98,10 @@ class ParameterOrderDetector : ComposableFunctionDetector(), SourceCodeScanner {
}

private val KtFunction.hasTrailingFunction: Boolean
get() = valueParameters.lastOrNull()?.typeReference?.typeElement is KtFunctionType
get() =
when (val outerType = valueParameters.lastOrNull()?.typeReference?.typeElement) {
is KtFunctionType -> true
is KtNullableType -> outerType.innerType is KtFunctionType
else -> false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class ParameterOrderDetectorTest : BaseSlackLintTest() {
@Composable
fun MyComposable(text1: String, modifier: Modifier = Modifier, m2: Modifier = Modifier, trailing: () -> Unit) { }
@Composable
fun MyComposable(text1: String, modifier: Modifier = Modifier, m2: Modifier = Modifier, trailing: (() -> Unit)?) { }
"""
.trimIndent()
lint().files(kotlin(code)).allowCompilationErrors().run().expectClean()
Expand Down

0 comments on commit eeb3989

Please sign in to comment.