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 e3c9d20b..c869e558 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 @@ -27,10 +27,14 @@ class ParameterOrderDetector : ComposableFunctionDetector(), SourceCodeScanner { companion object { fun createErrorMessage(currentOrder: List, properOrder: List): String = createErrorMessage( - currentOrder.joinToString { it.text }, - properOrder.joinToString { it.text }, + currentOrder.joinToString { getText(it) }, + properOrder.joinToString { getText(it) }, ) + fun getText(uParameter: UParameter): String { + return uParameter.sourcePsi?.text ?: "${uParameter.name}: ${uParameter.type.presentableText}" + } + private fun createErrorMessage(currentOrder: String, properOrder: String): String = """ Parameters in a composable function should be ordered following this pattern: params without defaults, modifiers, params with defaults and optionally, a trailing function that might not have a default param. @@ -99,7 +103,7 @@ class ParameterOrderDetector : ComposableFunctionDetector(), SourceCodeScanner { fix() .replace() .range(errorLocation) - .with(properOrder.joinToString(prefix = "(", postfix = ")") { it.text }) + .with(properOrder.joinToString(prefix = "(", postfix = ")") { getText(it) }) .reformat(true) .build(), ) 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 dabc3551..6bd963df 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 @@ -35,6 +35,9 @@ class ParameterOrderDetectorTest : BaseComposeLintTest() { @Composable fun MyComposable(text1: String, modifier: Modifier = Modifier, m2: Modifier = Modifier, trailing: (() -> Unit)?) { } + + @Composable + inline fun MyComposable(modifier: Modifier = Modifier, text: String = "123", lambda: () -> Unit) : T { } """ .trimIndent() lint().files(*commonStubs, kotlin(code)).run().expectClean() @@ -62,6 +65,9 @@ class ParameterOrderDetectorTest : BaseComposeLintTest() { @Composable fun MyComposable(text1: String, m2: Modifier = Modifier, modifier: Modifier = Modifier, trailing: () -> Unit) { } + + @Composable + inline fun MyComposable(text: String = "123", modifier: Modifier = Modifier, lambda: () -> Unit) : T { } """ .trimIndent() lint() @@ -94,7 +100,12 @@ class ParameterOrderDetectorTest : BaseComposeLintTest() { See https://slackhq.github.io/compose-lints/rules/#ordering-composable-parameters-properly for more information. [ComposeParameterOrder] fun MyComposable(text1: String, m2: Modifier = Modifier, modifier: Modifier = Modifier, trailing: () -> Unit) { } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 5 errors, 0 warnings + src/test.kt:20: Error: Parameters in a composable function should be ordered following this pattern: params without defaults, modifiers, params with defaults and optionally, a trailing function that might not have a default param. + Current params are: [text: String = "123", modifier: Modifier = Modifier, lambda: () -> Unit] but should be [modifier: Modifier = Modifier, text: String = "123", lambda: () -> Unit]. + See https://slackhq.github.io/compose-lints/rules/#ordering-composable-parameters-properly for more information. [ComposeParameterOrder] + inline fun MyComposable(text: String = "123", modifier: Modifier = Modifier, lambda: () -> Unit) : T { } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 6 errors, 0 warnings """ .trimIndent() ) @@ -120,7 +131,11 @@ class ParameterOrderDetectorTest : BaseComposeLintTest() { @@ -17 +17 - fun MyComposable(text1: String, m2: Modifier = Modifier, modifier: Modifier = Modifier, trailing: () -> Unit) { } + fun MyComposable(text1: String, modifier: Modifier = Modifier, m2: Modifier = Modifier, trailing: () -> Unit) { } - """ + Fix for src/test.kt line 20: Replace with (modifier: Modifier = Modifier, text: String = "123", lambda: () -> Unit): + @@ -20 +20 + - inline fun MyComposable(text: String = "123", modifier: Modifier = Modifier, lambda: () -> Unit) : T { } + + inline fun MyComposable(modifier: Modifier = Modifier, text: String = "123", lambda: () -> Unit) : T { } + """ .trimIndent() ) }