Skip to content

Commit f817979

Browse files
Dealing with even more kinds of trailing comments in UnwrapElseAfterReturn (#655)
1 parent 3dc59fd commit f817979

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

src/main/java/org/openrewrite/staticanalysis/UnwrapElseAfterReturn.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
import org.openrewrite.java.JavaVisitor;
2424
import org.openrewrite.java.tree.Comment;
2525
import org.openrewrite.java.tree.J;
26+
import org.openrewrite.java.tree.Space;
2627
import org.openrewrite.java.tree.Statement;
2728

2829
import java.time.Duration;
29-
import java.util.ArrayList;
3030
import java.util.Arrays;
3131
import java.util.List;
32+
import java.util.concurrent.atomic.AtomicReference;
3233

3334
public class UnwrapElseAfterReturn extends Recipe {
3435

@@ -54,7 +55,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5455
@Override
5556
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
5657
J.Block b = visitAndCast(block, ctx, super::visitBlock);
57-
List<Comment> endComments = new ArrayList<>();
58+
AtomicReference<Space> endWhitespace = new AtomicReference<>(null);
5859
J.Block alteredBlock = b.withStatements(ListUtils.flatMap(b.getStatements(), statement -> {
5960
if (statement instanceof J.If) {
6061
J.If ifStatement = (J.If) statement;
@@ -63,7 +64,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
6364
Statement elsePart = ifStatement.getElsePart().getBody();
6465
if (elsePart instanceof J.Block) {
6566
J.Block elseBlock = (J.Block) elsePart;
66-
endComments.addAll(elseBlock.getEnd().getComments());
67+
endWhitespace.set(elseBlock.getEnd());
6768
return ListUtils.concat(newIf, ListUtils.mapFirst(elseBlock.getStatements(), elseStmt -> {
6869
// Combine comments from the else block itself and the first statement
6970
List<Comment> elseComments = elseBlock.getPrefix().getComments();
@@ -81,9 +82,9 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
8182
return statement;
8283
}));
8384

84-
if (!endComments.isEmpty()) {
85-
List<Comment> mergedComments = ListUtils.concatAll(endComments, b.getEnd().getComments());
86-
alteredBlock = alteredBlock.withEnd(b.getEnd().withComments(mergedComments));
85+
if (endWhitespace.get() != null) {
86+
List<Comment> mergedComments = ListUtils.concatAll(endWhitespace.get().getComments(), b.getEnd().getComments());
87+
alteredBlock = alteredBlock.withEnd(b.getEnd().withComments(mergedComments).withWhitespace(endWhitespace.get().getWhitespace()));
8788
}
8889

8990
return maybeAutoFormat(b, alteredBlock, ctx);

src/test/java/org/openrewrite/staticanalysis/UnwrapElseAfterReturnTest.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ void process(String data) {
597597
}
598598

599599
@Test
600-
void comments() {
600+
void commentsEverywhere() {
601601
rewriteRun(
602602
java(
603603
"""
@@ -625,4 +625,64 @@ int foo(boolean condition) {
625625
)
626626
);
627627
}
628+
629+
@Test
630+
void commentsOnlyInBlocks() {
631+
rewriteRun(
632+
java(
633+
"""
634+
class Test {
635+
int foo(boolean condition) {
636+
if (condition) {
637+
return 1; // end 1
638+
} else {
639+
return 2; // end 2
640+
}
641+
}
642+
}
643+
""",
644+
"""
645+
class Test {
646+
int foo(boolean condition) {
647+
if (condition) {
648+
return 1; // end 1
649+
}
650+
return 2; // end 2
651+
}
652+
}
653+
"""
654+
)
655+
);
656+
}
657+
658+
@Test
659+
void commentsOnlyInBlocksWithNewLine() {
660+
rewriteRun(
661+
java(
662+
"""
663+
class Test {
664+
int foo(boolean condition) {
665+
if (condition) {
666+
return 1; // end 1
667+
} else {
668+
return 2;
669+
// next line after 2
670+
}
671+
}
672+
}
673+
""",
674+
"""
675+
class Test {
676+
int foo(boolean condition) {
677+
if (condition) {
678+
return 1; // end 1
679+
}
680+
return 2;
681+
// next line after 2
682+
}
683+
}
684+
"""
685+
)
686+
);
687+
}
628688
}

0 commit comments

Comments
 (0)