mrmans0n / compose-rules

Lint rules for ktlint/detekt aimed to contribute to a healthier usage of Compose. Actively maintained and evolved fork of the Twitter Compose rules.
https://mrmans0n.github.io/compose-rules
Other
548 stars 21 forks source link

Compose rules not working in ktlint-intellij-plugin (follow-up on #176) #190

Closed paul-dingemans closed 7 months ago

paul-dingemans commented 7 months ago

As reported in #176 (Originally posted by @chrimaeon in https://github.com/mrmans0n/compose-rules/issues/176#issuecomment-1894258671), the fix for running the compose rules with the ktlint-intellij-plugin only partially works. Given code sample below:

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable

@Composable
fun Post() {
    Text("foo")
}

does not show violation below which is shown by Ktlint CLI"

Post.kt:11:5: This @Composable function emits content but doesn't have a modifier parameter.

See https://mrmans0n.github.io/compose-rules/rules/#when-should-i-expose-modifier-parameters for more information. (compose:modifier-missing-check)

Summary error count (descending) by rule:
  compose:modifier-missing-check: 1

Process finished with exit code 1

with ktlint-compose 0.3.9 and intellij-ktlint-plugin 0.20.0

After investigation in ktlint and ktlint-intellij-plugin, I have found the cause of the problem. Like in #176 the cause is the embedded kotlin compiler which conflicts with packages provided by Intellij IDEA itself. In the ktlint-intellij-plugin those problems are resolved with relocating packages.

I have tried to relocate the packages in ktlint to prevent that third party supplier of rules are not affected by this but it didn't work out as expected. For now, I see no other solution than that each third party supplier of rules applies relocation of packages similar as is done in ktlint-intellij-plugin. For compose rules that would mean that configuration below has to be added to build file of ktlint-rules:

shadowJar {
    // Relocate packages that may conflict with ktlint-intellij-plugin. The rule set won't work when relocations below
    // are not kept in sync with https://github.com/nbadal/ktlint-intellij-plugin/blob/main/lib/build.gradle.kts
    relocate("org.jetbrains.concurrency", "shadow.org.jetbrains.concurrency")
    relocate("org.jetbrains.kotlin.psi.KtPsiFactory", "shadow.org.jetbrains.kotlin.psi.KtPsiFactory")
    relocate("org.jetbrains.kotlin.psi.psiUtil", "shadow.org.jetbrains.kotlin.psi.psiUtil")
    relocate("org.jetbrains.org", "shadow.org.jetbrains.org")
}

On my local machine, I have build the compose rules for ktlint with changes above. Both ktlint CLI and ktlint-intelli-plugin now report the violation as requested by OP.

Screenshot 2024-01-23 at 12 00 32

Screenshot 2024-01-23 at 12 00 47

mrmans0n commented 7 months ago

Thanks (as always) for the thorough debugging and solution. Will get to it as soon as I can 😁

paul-dingemans commented 7 months ago

Ah cool. Thanks for fixing.