Skip to content

Commit

Permalink
Fix handling of short-ternary operator when it is contained within pa…
Browse files Browse the repository at this point in the history
…rentheses.
  • Loading branch information
sambsnyd committed Jul 12, 2024
1 parent 92a2ba6 commit 78ca6ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1785,15 +1785,15 @@ public void visitReturnStatement(ReturnStatement return_) {

@Override
public void visitShortTernaryExpression(ElvisOperatorExpression ternary) {
Space fmt = whitespace();
Expression trueExpr = visit(ternary.getBooleanExpression());
J.Ternary elvis = new J.Ternary(randomId(), fmt, Markers.EMPTY,
trueExpr,
padLeft(sourceBefore("?"), trueExpr),
padLeft(sourceBefore(":"), visit(ternary.getFalseExpression())),
typeMapping.type(staticType(ternary)));
elvis = elvis.withMarkers(elvis.getMarkers().add(new Elvis(randomId())));
queue.add(elvis);
queue.add(insideParentheses(ternary, fmt -> {
Expression trueExpr = visit(ternary.getBooleanExpression());
J.Ternary elvis = new J.Ternary(randomId(), fmt, Markers.EMPTY,
trueExpr,
padLeft(sourceBefore("?"), trueExpr),
padLeft(sourceBefore(":"), visit(ternary.getFalseExpression())),
typeMapping.type(staticType(ternary)));
return elvis.withMarkers(elvis.getMarkers().add(new Elvis(randomId())));
}));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ void insideParentheses() {
);
}

@Test
void elvisInParens() {
rewriteRun(
groovy(" ( System.getProperty(\"foo\") ?: false ) ")
);
}

@Test
void ternary() {
rewriteRun(
Expand Down

0 comments on commit 78ca6ad

Please sign in to comment.