Format a class body that holds nothing but semicolons - #68
Merged
Merged
Conversation
"class A { ; }" failed with "expected token: ';'; generated } instead".
javac drops a stray semicolon from the member list, so the body looked
empty and the formatter wrote "{}" without ever emitting the ";" token
that was still in the input. A semicolon between real members was
already handled; only a body with no member at all lost it.
This ports google/google-java-format#1104 by Liam Miller-Cushon: an
empty body whose next token is ";" writes those semicolons one per
line at the member indent before the closing brace, as the member list
does. The golden SemicolonInClass comes from upstream, with the
expected output in this project's style.
The 15,747 files of the JDK 21 sources format exactly as before.
Interfaces, enums, records, annotation types and anonymous classes go through the same member-list code as classes, so the fix covers them too; the golden pins that, together with two semicolons in a row, a comment after the opening brace, and a semicolon in front of a real member, which took the existing path all along.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Brings over google/google-java-format#1104, a fix from google-java-format 1.23.
The bug
class A { ; }failed witherror: expected token: ';'; generated } instead. javac drops a stray semicolon from the member list, so the body looked empty and the formatter wrote{}without ever emitting the;that was still in the input. A semicolon between real members was already handled; only a body with no member at all lost it. The same happened tointerface B { ; },record R(int a) { ; },@interface H { ; }andnew Object() { ; }.From upstream, with the author kept
;writes those semicolons one per line at the member indent before the closing brace, as the member list does. The upstream goldenSemicolonInClasscomes with it, with the expected output in this project's style.Added here
ojf-semicolon-only-body: an interface, an enum, a record, an annotation type and an anonymous class with only semicolons in the body, two semicolons in a row, a comment after the opening brace, and a semicolon in front of a real member, which took the existing path all along.Checked
./gradlew :open-java-format:teston JDK 21: 1505 tests, all green.