pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.06k stars 504 forks source link

[1.3.0] Data class comments break indentation formatting #2689

Closed Bencodes closed 5 days ago

Bencodes commented 3 weeks ago

Expected Behavior

Formatting data classes with // comments should produce valid Kotlin code.

Observed Behavior

Auto fixing produces invalid Kotlin code that does not compile.

Steps to Reproduce

Run ktlint -f against the following Kotlin data class:

data class Foo(
    // Foo
    val foo: String,
    val bar: String,
)

Results in:

data class Foo(
    // Fooval foo: String, val bar: String,)

KtLint does not fail if you replace the // Foo comment with /* Foo */.

Your Environment

paul-dingemans commented 3 weeks ago

Tnx for reporting. Next time please enclose your .editorconfig settings as well. It turns out that this problem only occurs with non-default code style android_studio.

paul-dingemans commented 5 days ago
  • Relevant parts of the .editorconfig settings: An empty configuration reproduces this error.

This is not correct. With an empty .editorconfig you're using ktlint_official code style which does not reproduces this problem. The .editorconfig below does reproduce the problem:

root = true

[*.{kt,kts}]
ktlint_code_style = android_studio
paul-dingemans commented 5 days ago

The problem could also reproduced with the other code styles given that additional .editorconfig properties are set. The fix is to explicitly check that the primary constructory does not contain any EOL-comment in the parameter list.