forked from twitter/compose-rules
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Take out MutableStateParameter rule from MutableParameters rule (#159)
- Loading branch information
Showing
13 changed files
with
190 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
rules/common/src/main/kotlin/io/nlopez/compose/rules/MutableStateParameter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2023 Nacho Lopez | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.nlopez.compose.rules | ||
|
||
import io.nlopez.rules.core.ComposeKtConfig | ||
import io.nlopez.rules.core.ComposeKtVisitor | ||
import io.nlopez.rules.core.Emitter | ||
import io.nlopez.rules.core.report | ||
import org.jetbrains.kotlin.psi.KtFunction | ||
|
||
class MutableStateParameter : ComposeKtVisitor { | ||
|
||
override fun visitComposable( | ||
function: KtFunction, | ||
autoCorrect: Boolean, | ||
emitter: Emitter, | ||
config: ComposeKtConfig, | ||
) { | ||
function.valueParameters | ||
.filter { it.typeReference?.text?.matches(MutableStateRegex) == true } | ||
.forEach { emitter.report(it, MutableStateParameterInCompose) } | ||
} | ||
|
||
companion object { | ||
private val MutableStateRegex = "MutableState<.*>\\??".toRegex() | ||
|
||
val MutableStateParameterInCompose = """ | ||
MutableState shouldn't be used as a parameter in a @Composable function, as it promotes joint ownership over a state between a component and its user. | ||
If possible, consider making the component stateless and concede the state change to the caller. If mutation of the parent’s owned property is required in the component, consider creating a ComponentState class with the domain specific meaningful field that is backed by mutableStateOf(). | ||
See https://mrmans0n.github.io/compose-rules/rules/#do-not-use-mutablestate-as-a-parameter for more information. | ||
""".trimIndent() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
rules/detekt/src/main/kotlin/io/nlopez/compose/rules/detekt/MutableStateParameterCheck.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2023 Nacho Lopez | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.nlopez.compose.rules.detekt | ||
|
||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Debt | ||
import io.gitlab.arturbosch.detekt.api.Issue | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import io.nlopez.compose.rules.MutableStateParameter | ||
import io.nlopez.rules.core.ComposeKtVisitor | ||
import io.nlopez.rules.core.detekt.DetektRule | ||
|
||
class MutableStateParameterCheck(config: Config) : | ||
DetektRule(config), | ||
ComposeKtVisitor by MutableStateParameter() { | ||
override val issue: Issue = Issue( | ||
id = "MutableStateParam", | ||
severity = Severity.Defect, | ||
description = MutableStateParameter.MutableStateParameterInCompose, | ||
debt = Debt.TWENTY_MINS, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...s/detekt/src/test/kotlin/io/nlopez/compose/rules/detekt/MutableStateParameterCheckTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2023 Nacho Lopez | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.nlopez.compose.rules.detekt | ||
|
||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.SourceLocation | ||
import io.gitlab.arturbosch.detekt.test.assertThat | ||
import io.gitlab.arturbosch.detekt.test.lint | ||
import io.nlopez.compose.rules.MutableStateParameter | ||
import org.intellij.lang.annotations.Language | ||
import org.junit.jupiter.api.Test | ||
|
||
class MutableStateParameterCheckTest { | ||
|
||
private val rule = MutableStateParameterCheck(Config.empty) | ||
|
||
@Test | ||
fun `errors when a Composable has a MutableState parameter`() { | ||
@Language("kotlin") | ||
val code = | ||
""" | ||
@Composable | ||
fun Something(a: MutableState<String>) {} | ||
""".trimIndent() | ||
val errors = rule.lint(code) | ||
assertThat(errors) | ||
.hasStartSourceLocations( | ||
SourceLocation(2, 15), | ||
) | ||
for (error in errors) { | ||
assertThat(error).hasMessage(MutableStateParameter.MutableStateParameterInCompose) | ||
} | ||
} | ||
|
||
@Test | ||
fun `no errors when a Composable has valid parameters`() { | ||
@Language("kotlin") | ||
val code = | ||
""" | ||
@Composable | ||
fun Something(a: String, b: (Int) -> Unit) {} | ||
@Composable | ||
fun Something(a: State<String>) {} | ||
""".trimIndent() | ||
val errors = rule.lint(code) | ||
assertThat(errors).isEmpty() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
rules/ktlint/src/main/kotlin/io/nlopez/compose/rules/ktlint/MutableStateParameterCheck.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2023 Nacho Lopez | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.nlopez.compose.rules.ktlint | ||
|
||
import io.nlopez.compose.rules.MutableStateParameter | ||
import io.nlopez.rules.core.ComposeKtVisitor | ||
import io.nlopez.rules.core.ktlint.KtlintRule | ||
|
||
class MutableStateParameterCheck : | ||
KtlintRule("compose:mutable-state-param-check"), | ||
ComposeKtVisitor by MutableStateParameter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...s/ktlint/src/test/kotlin/io/nlopez/compose/rules/ktlint/MutableStateParameterCheckTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 Nacho Lopez | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.nlopez.compose.rules.ktlint | ||
|
||
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule | ||
import com.pinterest.ktlint.test.LintViolation | ||
import io.nlopez.compose.rules.MutableStateParameter | ||
import org.intellij.lang.annotations.Language | ||
import org.junit.jupiter.api.Test | ||
|
||
class MutableStateParameterCheckTest { | ||
|
||
private val mutableParamRuleAssertThat = assertThatRule { MutableStateParameterCheck() } | ||
|
||
@Test | ||
fun `errors when a Composable has a mutable parameter`() { | ||
@Language("kotlin") | ||
val code = | ||
""" | ||
@Composable | ||
fun Something(a: MutableState<String>) {} | ||
""".trimIndent() | ||
mutableParamRuleAssertThat(code).hasLintViolationsWithoutAutoCorrect( | ||
LintViolation( | ||
line = 2, | ||
col = 15, | ||
detail = MutableStateParameter.MutableStateParameterInCompose, | ||
), | ||
) | ||
} | ||
|
||
@Test | ||
fun `no errors when a Composable has valid parameters`() { | ||
@Language("kotlin") | ||
val code = | ||
""" | ||
@Composable | ||
fun Something(a: String, b: (Int) -> Unit) {} | ||
@Composable | ||
fun Something(a: State<String>) {} | ||
""".trimIndent() | ||
mutableParamRuleAssertThat(code).hasNoLintViolations() | ||
} | ||
} |