From eeb3989345dadb69a4240702a2be85b716280a9a Mon Sep 17 00:00:00 2001 From: Nacho Lopez Date: Fri, 9 Jun 2023 12:44:32 +0200 Subject: [PATCH] Allow nullable types for trailing lambdas in ComposeParameterOrder (#134) Fixed upstream in https://github.com/mrmans0n/compose-rules/pull/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. --- .../java/slack/lint/compose/ParameterOrderDetector.kt | 8 +++++++- .../java/slack/lint/compose/ParameterOrderDetectorTest.kt | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/compose-lint-checks/src/main/java/slack/lint/compose/ParameterOrderDetector.kt b/compose-lint-checks/src/main/java/slack/lint/compose/ParameterOrderDetector.kt index 0a6e1eec..20eb0bc3 100644 --- a/compose-lint-checks/src/main/java/slack/lint/compose/ParameterOrderDetector.kt +++ b/compose-lint-checks/src/main/java/slack/lint/compose/ParameterOrderDetector.kt @@ -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 @@ -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 + } } diff --git a/compose-lint-checks/src/test/java/slack/lint/compose/ParameterOrderDetectorTest.kt b/compose-lint-checks/src/test/java/slack/lint/compose/ParameterOrderDetectorTest.kt index 8fa83992..c5e7d44f 100644 --- a/compose-lint-checks/src/test/java/slack/lint/compose/ParameterOrderDetectorTest.kt +++ b/compose-lint-checks/src/test/java/slack/lint/compose/ParameterOrderDetectorTest.kt @@ -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()