-
Notifications
You must be signed in to change notification settings - Fork 510
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
Rule to mandate/forbid trailing commas #709
Comments
Ideally this rule could be as flexible as something like Edit: Perhaps supporting that feature set would better live elsewhere? |
Probably related to #701 to control |
Any update? |
Is breaking so many things this , |
Is this on ktlint's roadmap? If not, we could implement in detekt instead. I don't think it makes sense to do that though if ktlint will implement the rule so we can avoid duplication of effort, but I think there's demand for it (I'm sure this is my most 👍 issue ever!) |
Hopefully is or it will be 🤞, for now I'm trying to implement my own rule from the link above. I can confirm there is a high demand. Will be nice to autocorrect with ktlint... |
@3flex you are more than welcome to contribute this to ktlint directly and utilize it in detekt afterwards 👀 it is certainly on the roadmap, just the maintainers were sidetracked by the day jobs and we had other priorities in ktlint, but I see this one indeed has a high demand so we'll re-prioritize |
Is this is getting released soon? |
Maybe try using your own ruleset |
Oui oumême iii
Le jeu. 11 mars 2021 à 09:40, Christian Dehning ***@***.***>
a écrit :
… Is this is getting released soon?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#709 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AR6FQKW2WTD3JDHQ4AAU2DLTDB6V7ANCNFSM4LBDHE4Q>
.
|
Is there a guideline on integrating this rule into local version of local ktlint? |
any news? |
We are currently using our own rule for mandating (and automatically fixing) missing trailing commas. Probably it is not ready yet to be added to the ktlint project, but I am happy to have a discussion about it: class MandateTrailingComma : Rule("mandate-trailing-comma") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
) {
if (node.isArgumentOrParameterList() && node.isMultiLine()) {
val lastArgument = node.lastArgumentOrParameter()
if (lastArgument != null && lastArgument.treeNext.elementType != COMMA) {
emit(
lastArgument.startOffset + lastArgument.textLength,
"Trailing comma is missing",
true,
)
if (autoCorrect) {
node.addChild(LeafPsiElement(COMMA, ","), lastArgument.treeNext)
}
}
}
}
private fun ASTNode.isArgumentOrParameterList(): Boolean =
elementType == VALUE_ARGUMENT_LIST || elementType == VALUE_PARAMETER_LIST
private fun ASTNode.isMultiLine(): Boolean = children().any { it is PsiWhiteSpace && it.textContains('\n') }
private fun ASTNode.lastArgumentOrParameter() =
children().findLast { it.elementType == VALUE_ARGUMENT || it.elementType == VALUE_PARAMETER }
} |
Fantastic stuff! Just to be clear, “allowing” trailing commas here means requiring trailing commas, right? Will fun foo(
param1: Bar,
param2: Bar
) be converted into fun foo(
param1: Bar,
param2: Bar, // <-- added trailing comma
) when either running |
Yes, this means requiring, that's correct (and that's what IJ does as well, when ticking the "Allow trailing comma" checkbox in settings). I will update README with a better description of the behavior 👍 |
I might be doing something wrong, and I don't see ktlint -F is putting missing trailing commas, Here is my setup: I have the following in .editorconfig
And I have code like this
When I run command
I see the following error
I don't see that ktlint -F has put the trailing comma.
|
@snijsure which version are you using? |
@romtsn I am using version 0.42.1
|
yeah, as I mentioned it's only available in the snapshot version for now. |
When is it going to be live? |
A new release containing this was just created https://github.com/pinterest/ktlint/releases/tag/0.43.0 |
|
Is there a way to just enable this rule, out of the experimental ruleset? If I turn on experimental rules it turns up lots of other warnings that we don't care at this moment |
@romtsn Thanks for the hint, I executed this and it works fine: Looking forward to seeing the final rule. |
@snijsure nope, the best you can do is to enable the experimental ruleset and disable the rules you don't care about via |
Since this is an experimental rule, should we report issues here or create a new ticket case by case? For instance in this case Android Studio works correctly and adds a comma, but Ktlint doesn't and even removes the comma added by Android Studio! |
@mohsenoid please file a new issue |
how can I enable this rule via .editorconfig? I used:
but seems to have no effect |
It does not answer how I can enable it via .editorconfig. |
well, your question was about the trailing comma rule, not about the experimental ruleset. you can't enable experimental via editorconfig |
Can this feature be used with spotless? |
Not as far as I can tell, since experimental ktlint flags can't be enabled. (diffplug/spotless#409) |
This feature seems to break |
I have same I run |
I created a failing test-case and new issue: #1367 |
In Ktlint v0.46.0, [*.{kt,kts}]
ij_kotlin_allow_trailing_comma_on_call_site=true
ij_kotlin_allow_trailing_comma=true If you use the ktlint-maven-plugin, you must use $ mvn com.github.gantsign.maven:ktlint-maven-plugin:1.14.0:format |
I enabled both of the settings for trailing commas. I'm using Ktlint Gradle plugin version 11.0.0. When I run ./gradlew ktlintcheck For example here the trailing comma is missing and ktlint didn't throw any errors
|
I am not familiar with Ktlint Gradle plugin, but it seems that the newest version of that plugin, v11.0.0, is using Ktlint v0.43.2. Between Ktlint [v0.43.0, v0.46.0), the trailing comma rule was only present in the experimental ruleset. Skimming trough the issue list of Ktlint Gradle plugin it is at least two issues for upgrading Ktlint, and problems with that, e.g.:
You should follow those issue to get notified when Ktlint Gradle plugin is compatible with newer versions of Ktlint. |
Expected Behavior
ktlint offers a rule that enforces a particular code style regarding trailing commas, which are now permitted by the Kotlin compiler as of 1.3.70, see https://youtrack.jetbrains.com/issue/KT-34743 for more details.
This may not be a good fit for ktlint as it's too opinionated and no conventions exist yet, but if it's suitable it's a better fit here than in detekt where it was originally proposed.
Observed Behavior
No such rule exists.
Steps to Reproduce
N/A, new rule required.
Your Environment
The text was updated successfully, but these errors were encountered: