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

Correct indentation not applied to when expressions with trailing commas enabled #5174

Closed
jkmckay opened this issue Aug 3, 2022 · 2 comments
Labels

Comments

@jkmckay
Copy link

jkmckay commented Aug 3, 2022

Expected Behavior

When running detekt with both trailing commas and indentation enabled, i.e.

  Indentation:
    active: true
    autoCorrect: true
    indentSize: 4
    continuationIndentSize: 4
/* ... */
  TrailingComma:
    active: true
    autoCorrect: true
    allowTrailingComma: true
    allowTrailingCommaOnCallSite: true

Trailing commas should be added as appropriate and indentation should be applied.

For example something like this:

enum class TestEnum { EXAMPLE_A, EXAMPLE_B, EXAMPLE_C }

fun exampleFunction(enum: TestEnum) {
    when (enum) {
        TestEnum.EXAMPLE_A,
        TestEnum.EXAMPLE_B -> enum.toString()
        else -> {}
    }
}

should be formatted as this:

enum class TestEnum { EXAMPLE_A, EXAMPLE_B, EXAMPLE_C }

fun exampleFunction(enum: TestEnum) {
    when (enum) {
        TestEnum.EXAMPLE_A,
        TestEnum.EXAMPLE_B,
        -> enum.toString()
        else -> {}
    }
}

Observed Behavior

What happens is this:
(until detekt is ran for a second time and then the expected output is produced.)

enum class TestEnum { EXAMPLE_A, EXAMPLE_B, EXAMPLE_C }

fun exampleFunction(enum: TestEnum) {
    when (enum) {
        TestEnum.EXAMPLE_A,
        TestEnum.EXAMPLE_B,
-> enum.toString()
        else -> {}
    }
}

Steps to Reproduce

Enable trailing commas and observe when expresssions formatting incorrectly on first run of detekt.

Context

Preventing team from agreeing to enable the trailing commas option and manually formatting.

Your Environment

Tested on Detekt 1.19.0 & 1.21.0

@jkmckay jkmckay added the bug label Aug 3, 2022
@schalkms
Copy link
Member

schalkms commented Aug 3, 2022

Both mentioned rules are native ktlint rules. Detekt just provides a wrapper around them.
Could you please try to reproduce this with plain ktlint and potentially report to this to their repo?

@schalkms schalkms added support and removed bug labels Aug 3, 2022
@jkmckay
Copy link
Author

jkmckay commented Aug 3, 2022

Thanks, I've checked the Ktlint repo and an open issue for this already exists. Will close this one.

@jkmckay jkmckay closed this as completed Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants