Skip to content

Commit

Permalink
Ensure argument list is never *completely* empty after running String…
Browse files Browse the repository at this point in the history
…Formatted
  • Loading branch information
sambsnyd committed Mar 14, 2024
1 parent ac54fc3 commit 3b324fa
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import java.time.Duration;
import java.util.List;

import static java.util.Collections.singletonList;
import static org.openrewrite.Tree.randomId;

@Value
@EqualsAndHashCode(callSuper = false)
public class StringFormatted extends Recipe {
Expand Down Expand Up @@ -72,9 +75,14 @@ public J visitMethodInvocation(J.MethodInvocation m, ExecutionContext ctx) {
mi = mi.withName(mi.getName().withType(mi.getMethodType()));
}
Expression select = wrapperNotNeeded ? arguments.get(0) :
new J.Parentheses<>(Tree.randomId(), Space.EMPTY, Markers.EMPTY, JRightPadded.build(arguments.get(0)));
new J.Parentheses<>(randomId(), Space.EMPTY, Markers.EMPTY, JRightPadded.build(arguments.get(0)));
mi = mi.withSelect(select);
mi = mi.withArguments(arguments.subList(1, arguments.size()));
if(mi.getArguments().isEmpty()) {
// To store spaces between the parenthesis of a method invocation argument list
// Ensures formatting recipes chained together with this one will still work as expected
mi = mi.withArguments(singletonList(new J.Empty(randomId(), Space.EMPTY, Markers.EMPTY)));
}
return maybeAutoFormat(m, mi, ctx);
}

Expand Down

0 comments on commit 3b324fa

Please sign in to comment.