Closed paul-dingemans closed 7 months ago
Thanks for the thorough writeup!
I am going to update the ktlint version in this library 1.1.0 if I can, just in case it helps.
FWIW I am currently AFK for a few days, can't do much coding with just the phone. If this can't wait, feel free to submit the changes, and I can approve + the snapshot will be generated automatically + the unit tests would run. If it ends up not being that, I can always easily revert from the phone 🥲
There is no harm in waiting. It is just an inconvenience I would say. Enjoy your time off.
I think I'm going to try to use the type of the PsiElement for the when instead (I'm doing a cast immediately after in all code paths so it should be fine), that should be a harmless change I hope (and might fix this?).
Snapshot should be already up with this patch in, if KtNodeStubElementTypes were the problem they shouldn't be anymore. https://github.com/mrmans0n/compose-rules/actions/runs/7450793445/job/20270545604
If the ktlint IJ plugin requires the fat jars, you'll need to download the current master and compile it manually, something like: ./gradlew clean :rules:ktlint:shadowJar -PuberJar
Coool, will have a look soon. Can you point to the locations of the published snapshot?
https://oss.sonatype.org/service/local/repositories/snapshots/content/io/nlopez/compose/rules/ktlint/maven-metadata.xml this is in the base directory, it should be the 0.3.9-SNAPSHOT one.
I have tested the fat-jar of 0.3.9.-SNAPSHOT
in ktlint-intellij-plugin
.
If .editorconfig
contains setting below:
ktlint_function_naming_ignore_when_annotated_with=Composable
then the Post
function is not being flagged as having an improper name as it is annotated with @Composable
. Function Text
is still flagged as it is not annotated.
Without the configuration setting above, both functions are flagged:
As far as I am concerned, this issue can be closed, provided that it will be released ;-)
But there is still the issue, that the compose ktlint rule for the modifier
does not show up.
in the screenshot above, Post
is a public composable that should show an lint issue that a modifier should be provided as mentioned in the start of the thread.
the function naming issue was actually solved with the ktlint-inellij-plugin update
in the screenshot above,
Post
is a public composable that should show an lint issue that a modifier should be provided as mentioned in the start of the thread.
This screenshot is a kind of faked and may not present what is happening with a real code example. So, I have not used the actual Composable imports but just a custom annotation class. Same for the functions Post and Text. Please validate with an actual example of a Composable.
I validated in my original code and it's not working.
so this
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
@Composable
fun Post() {
Text("foo")
}
does not show
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
Feel free to open an issue, and if you can provide a sample project it would be useful 😁
But being transparent here, given that it seems that detekt cli, detekt gradle, detekt intellij, and ktlint cli and ktlint gradle pick the rule it up correctly, I guess the issue might lie elsewhere. I hope it can be solved somehow 🤞
In https://github.com/nbadal/ktlint-intellij-plugin/issues/425 it was reported that the compose rules are not working in the new ktlint-intellij-plugin while they do work in Ktlint CLI.
Post.kt:
.editorconfig:
Output with ktlint CLI
1.1.0
:The lint violation is however not reported by the
ktlint-intellij-plugin
.After investigation, I have found that the usages of
KtStubElementTypes
inKtlintRule
is the most likely cause of the problem.Here the
node.elementType
is checked againstKtStubElementTypes
instead of usingElementType
. This might also be used in other classes.To proof that above assumption is the cause of the problem, I have build a new simple rule in a new custom ruleset in ktlint:
Given code sample below:
and ktlint CLI
1.1.0
this produces:The problem is reported twice, as was to be expected. The embeddable kotlin compiler in Ktlint CLI can match the element type of the node both to 'ElementType.FUN
and
KtStubElementTypes.FUNCTION`.When using the same ruleset jar in
As you can see, the problem is now only reported as it matches with
ktlint-intellij-plugin
, following problems are reported:ElementType.FUN
. I am not 100% sure about the reason, but I believe that theKtStubElementTypes
are not compatible with the IDEA kotlin compiler.Can you provide a snapshot version of the compose ruleset that uses
ElementType
instead ofKtStubElementTypes
so that we can verify whether this resolves the problem?