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

Formatting enum class with member definition and missing trailing commas causes rule violations on first run #1786

Closed
karljohansson opened this issue Jan 23, 2023 · 4 comments · Fixed by #1796
Milestone

Comments

@karljohansson
Copy link

Expected Behavior

When formatting and validating the following enum class, the ktlint command should exit successfully (status 0):

enum class TestEnum() {
    A,
    B;

    companion object {
        const val A_NUMBER = 1L
    }
}

Observed Behavior

When running ktlint on the above enum class, it is formatted correctly, but the run fails with rule violations (exit status 1):

$ ktlint --format 
17:25:00.694 [main] INFO com.pinterest.ktlint.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
/home/karl/tasks/ktlint-issue-example/TestEnum.kt:1:1: Missing trailing comma before "}" (trailing-comma-on-declaration-site)
/home/karl/tasks/ktlint-issue-example/TestEnum.kt:5:4: Unnecessary semicolon (no-semi)

Summary error count (descending) by rule:
  no-semi: 1
  trailing-comma-on-declaration-site: 1
Exit status: 1 

I note that the line number for the no-semi violation is the line number after formatting.

A second run on the (now properly formatted) code runs correctly (exit status 0):

$ ktlint --format
17:25:05.397 [main] INFO com.pinterest.ktlint.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
Exit status: 0

Steps to Reproduce

Run command on above enum class:
ktlint --format

Your Environment

  • Version of ktlint used: 0.48.1
  • Operating System and version: Arch Linux
@hoc081098
Copy link

I have the same issue

@karljohansson karljohansson changed the title Formatting enum class with member definition and missing trailing commas cause rule violations on first run Formatting enum class with member definition and missing trailing commas causes rule violations on first run Jan 25, 2023
@paul-dingemans
Copy link
Collaborator

See release notes of 0.48.0:

  • The default value for trailing comma's on call site is changed to true unless the android codestyle is enabled. Note that KtLint from a consistency viewpoint enforces the trailing comma on call site while default IntelliJ IDEA formatting only allows the trailing comma but leaves it up to the developer's discretion. (#1670)

@karljohansson
Copy link
Author

karljohansson commented Jan 27, 2023

@paul-dingemans I don't quite follow. My issue is that after doing formatting, ktlint reports invalid no-semi and trailing-comma-on-declaration-site rule violations. A subsequent run of ktlint (correctly) passes the same code.

@paul-dingemans
Copy link
Collaborator

Ah now I see. Although it is not visual in the textual representation of the code, the trailing comma is not inserted at the correct place in the AST representation of the code. I will try to fix it.

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.

3 participants