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

Supporting flexible configurations of TrailingCommaRule #1552

Closed
chao2zhang opened this issue Jul 19, 2022 · 6 comments · Fixed by #1555
Closed

Supporting flexible configurations of TrailingCommaRule #1552

chao2zhang opened this issue Jul 19, 2022 · 6 comments · Fixed by #1555

Comments

@chao2zhang
Copy link
Contributor

chao2zhang commented Jul 19, 2022

Expected Behavior

The coding convention section about trailing commas - https://kotlinlang.org/docs/coding-conventions.html#trailing-commas

Using trailing commas has several benefits:

  • It makes version-control diffs cleaner – as all the focus is on the changed value.
  • It makes it easy to add and reorder elements – there is no need to add or delete the comma if you manipulate elements.
  • It simplifies code generation, for example, for object initializers. The last element can also have a comma.

Trailing commas are entirely optional – your code will still work without them. The Kotlin style guide encourages the use of trailing commas at the declaration site and leaves it at your discretion for the call site.

I was expecting that I could configure ktlint to conform to the kotlin coding convention

  • Force comma at declaration site
  • Not doing any checks at the call site

Current Behavior

It seems that the existing implementation, as mentioned in #1032, either enforces commas or prohits commas at the call site.

Additional information

  • Current version of ktlint: 0.46.0
@paul-dingemans
Copy link
Collaborator

It seems that the existing implementation, as mentioned in #1032, either enforces commas or prohits commas at the call site.

This is entirely true. KtLint aims to provide a consistent code format throughout the entire codebase. So the trailing comma is either forced to all locations at call site or is not allowed anywhere at call site. This is indeed more strict than the Kotlin Coding conventions but it does not violate it. Leaving it to the developer discretion could lead to "endless" discussions about whether the trailing comma at call site is preferable in one example but not at another place.

@chao2zhang
Copy link
Contributor Author

Actually completely enforcing or completely prohibiting also lead to an extensive discussion among my colleagues - The previous agreement was always to follow the Kotlin coding conventions but having ktlint taking an opinion does retrigger these conversations.

@paul-dingemans
Copy link
Collaborator

I do understand that such an opinion of KtLint can lead to a debate in some teams. In other teams it will just be accepted as is and not leading to any discussion. Personally, I care more about consistency than about what specific setting is used.

What could be done is splitting the Trailing comma rule in two different rules. Both rules will work exactly the same as current rule but will only handle the declaration or the call site. In your situation, the rule for handling the call site declaration can then be disabled.

Are you willing to implement this?

@chao2zhang
Copy link
Contributor Author

Definitely. I am happy to implement this by splitting the TrailingCommaRule into TrailingCommaDeclarationSiteRule and TrailingCommaCallSiteRule

@paul-dingemans
Copy link
Collaborator

Definitely. I am happy to implement this by splitting the TrailingCommaRule into TrailingCommaDeclarationSiteRule and TrailingCommaCallSiteRule

Cool. I would advise to wait untill PR's below are merged to master to prevent merge conflicts:

@chao2zhang
Copy link
Contributor Author

Thank you for the tip - The merge conflict seems to be minimal so I am happy to wait

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants