Skip to content

Commit

Permalink
Fix for #3759 (#3760)
Browse files Browse the repository at this point in the history
* #3759
Allow empty statement to support case-fallthrough.

* Formatting and add case with comment

---------

Co-authored-by: Matthias Hanisch <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Dec 3, 2023
1 parent eccdbd4 commit 80df083
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -970,17 +970,16 @@ Markers.EMPTY, emptyList(), statement.getLabel(), null, null))

@Override
public void visitCaseStatement(CaseStatement statement) {
queue.add(
new J.Case(randomId(),
sourceBefore("case"),
Markers.EMPTY,
J.Case.Type.Statement,
null,
JContainer.build(singletonList(JRightPadded.build(visit(statement.getExpression())))),
JContainer.build(sourceBefore(":"),
convertStatements(((BlockStatement) statement.getCode()).getStatements(), t -> Space.EMPTY), Markers.EMPTY),
null
)
queue.add(new J.Case(randomId(),
sourceBefore("case"),
Markers.EMPTY,
J.Case.Type.Statement,
null,
JContainer.build(singletonList(JRightPadded.build(visit(statement.getExpression())))),
statement.getCode() instanceof EmptyStatement
? JContainer.build(sourceBefore(":"), convertStatements(emptyList(), t -> Space.EMPTY), Markers.EMPTY)
: JContainer.build(sourceBefore(":"), convertStatements(((BlockStatement) statement.getCode()).getStatements(), t -> Space.EMPTY), Markers.EMPTY)
, null)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,22 @@ void switchWithCommas() {
)
);
}

@Test
void fallthroughCase() {
rewriteRun(
groovy(
"""
switch("foo") {
case "foo":
case "bar":
// fallthrough
case "quz": {
break
}
}
"""
)
);
}
}

0 comments on commit 80df083

Please sign in to comment.