jhipster / prettier-java

Prettier Java Plugin
http://www.jhipster.tech/prettier-java/
Apache License 2.0
1.06k stars 103 forks source link

checkstyle inconsistent with prettier #631

Closed xenoterracide closed 5 months ago

xenoterracide commented 5 months ago
[ant:checkstyle] [ERROR] Merge.java:40:80: Only one statement per line allowed. [OneStatementPerLine]
npx prettier --version
3.1.1
2.5.0
plugins:
  - prettier-plugin-java
  - prettier-plugin-properties
  - prettier-plugin-sh
  - prettier-plugin-toml
  - "@prettier/plugin-xml"

checkstyle, I copied pasta-ed from https://github.com/jhipster/prettier-java/commit/deda6ca37214420c37b5981ca23119ef49743620#diff-6c435908fe54a62733ee357cc3ef828f242045ba31fa59d6d3f763b5352d694e which is just a link to the version in master at the time of this writing

Input:

        if (first == null) return second;
        else if (second == null) return first;

Output:

        if (first == null) return second; else if (second == null) return first;

Expected behavior:

no error, results should be consistent, however I'm not certain if prettier is creating the wrong output or if checkstyle is configured wrong. I'm kind of wondering if prettier can/should be adding the braces here in the first place.

jtkiesel commented 5 months ago

Prettier JavaScript seems to leave the input as it is in this case (i.e. it wants a newline separating the if and else if, and doesn't automatically add braces.) We do have #459 that considers forcing braces around conditional bodies. That change would probably require more discussion than simply adding/keeping the newline, so I would suggest that the action item for this ticket is to add/keep the newline between the if and else if to mimic Prettier JavaScript's behavior (and to avoid violating the Checkstyle config we provide.)

xenoterracide commented 5 months ago

I would be fine with leaving the input as is. I'm not sure how I feel about this particular style of code in general but reducing it to one line seems like the wrong answer. I temporarily fixed this code by adding braces around the first one