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

Kotlin - Enable trailing comma #3959

Merged
merged 1 commit into from
Sep 8, 2023
Merged

Conversation

tinsukE
Copy link
Contributor

@tinsukE tinsukE commented Aug 16, 2023

Trailing commas on Kotlin sources has many advantages:

  • 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.

This PR doesn't go as far as require it, but tweaks KtLint to at least allow it.

The two .kt files prove that the KtLint rules have been properly disabled.

.editorconfig Show resolved Hide resolved
@tinsukE tinsukE marked this pull request as ready for review August 16, 2023 18:43
Copy link
Contributor

@nikclayton nikclayton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I've understood this correctly (I've tried it locally to confirm) this will allow a mix of trailing and non-trailing commas in future code, with no clean way to migrate naturally as the relevant files get modified over time.

Can this be done so that existing non-trailing-comma usage is grandfathered in, but new usage is forbidden?

E.g., in .editorconfig have:

ktlint_code_style = intellij_idea
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true

and then create a baseline that includes all the lint "violations" that this will introduce (per the discussion of the baseline file, in https://pinterest.github.io/ktlint/0.50.0/install/cli/#violation-reporting).

Over time violations would shrink, and the baseline file will get smaller (every time it's regenerated) until there are none (or only a handful) to clean up.

For this to work ktlintFormat would need to ignore violations in the baseline file too. I haven't tested that.

.editorconfig Show resolved Hide resolved
.editorconfig Show resolved Hide resolved
@Tak
Copy link
Collaborator

Tak commented Aug 17, 2023

IMO it should be situation dependent - neither mandated nor forbidden.
For example, trailing commas are pretty awkward in single-line usage.

@nikclayton
Copy link
Contributor

IMO it should be situation dependent - neither mandated nor forbidden. For example, trailing commas are pretty awkward in single-line usage.

These changes don't affect single-line usage, the rule / format change is only triggered on multi-line call sites and declarations.

// No trailing comma required, formatter does not add one
data class Foo(val x: String, val y: String)
// Trailing comma would be required, lint would fail, formatter adds one
data class Foo(
    val x: String,
    val y: String  // <-- currently allowed, would require trailing comma
)

@tinsukE
Copy link
Contributor Author

tinsukE commented Sep 7, 2023

@Tak since this is approved, shall we merge it?

Or would you prefer I look into merging/improving this other one? #3968

@Tak
Copy link
Collaborator

Tak commented Sep 8, 2023

Yes, let's merge this one, thank you.

@Tak Tak merged commit 40d771d into tuskyapp:develop Sep 8, 2023
3 checks passed
@tinsukE tinsukE deleted the allow-trailing-comma branch September 8, 2023 08:09
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 this pull request may close these issues.

4 participants