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

Use name of parameter if text is not available #274

Merged
merged 6 commits into from
Feb 8, 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 @@ -27,10 +27,14 @@ class ParameterOrderDetector : ComposableFunctionDetector(), SourceCodeScanner {
companion object {
fun createErrorMessage(currentOrder: List<UParameter>, properOrder: List<UParameter>): 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.
Expand Down Expand Up @@ -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(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class ParameterOrderDetectorTest : BaseComposeLintTest() {

@Composable
fun MyComposable(text1: String, modifier: Modifier = Modifier, m2: Modifier = Modifier, trailing: (() -> Unit)?) { }

@Composable
inline fun <reified T> MyComposable(modifier: Modifier = Modifier, text: String = "123", lambda: () -> Unit) : T { }
"""
.trimIndent()
lint().files(*commonStubs, kotlin(code)).run().expectClean()
Expand Down Expand Up @@ -62,6 +65,9 @@ class ParameterOrderDetectorTest : BaseComposeLintTest() {

@Composable
fun MyComposable(text1: String, m2: Modifier = Modifier, modifier: Modifier = Modifier, trailing: () -> Unit) { }

@Composable
inline fun <reified T> MyComposable(text: String = "123", modifier: Modifier = Modifier, lambda: () -> Unit) : T { }
"""
.trimIndent()
lint()
Expand Down Expand Up @@ -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 <reified T> MyComposable(text: String = "123", modifier: Modifier = Modifier, lambda: () -> Unit) : T { }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 errors, 0 warnings
"""
.trimIndent()
)
Expand All @@ -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 <reified T> MyComposable(text: String = "123", modifier: Modifier = Modifier, lambda: () -> Unit) : T { }
+ inline fun <reified T> MyComposable(modifier: Modifier = Modifier, text: String = "123", lambda: () -> Unit) : T { }
"""
.trimIndent()
)
}
Expand Down