Skip to content

Commit 232339c

Browse files
olwispetimtebeek
andauthored
Simplify else branch if it only has a single if (#666) (#726)
* Simplify `else` branch if it only has a single `if` (#666) * Update License to MSAL Co-authored-by: Tim te Beek <[email protected]> * Update License to MSAL Co-authored-by: Tim te Beek <[email protected]> * Polish * Verify handling of somewhat exotic newline placement * Add placeholder to enable recipe once we've seen it at scale --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent cec848f commit 232339c

File tree

3 files changed

+414
-0
lines changed

3 files changed

+414
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.staticanalysis;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Recipe;
20+
import org.openrewrite.internal.ListUtils;
21+
import org.openrewrite.java.JavaIsoVisitor;
22+
import org.openrewrite.java.tree.J;
23+
import org.openrewrite.java.tree.Space;
24+
import org.openrewrite.java.tree.Statement;
25+
26+
import static org.openrewrite.java.format.ShiftFormat.indent;
27+
28+
public class SimplifyElseBranch extends Recipe {
29+
30+
@Override
31+
public String getDisplayName() {
32+
// language=markdown
33+
return "Simplify `else` branch if it only has a single `if`";
34+
}
35+
36+
@Override
37+
public String getDescription() {
38+
// language=markdown
39+
return "Simplify `else` branch if it only has a single `if`.";
40+
}
41+
42+
@Override
43+
public JavaIsoVisitor<ExecutionContext> getVisitor() {
44+
return new JavaIsoVisitor<ExecutionContext>() {
45+
@Override
46+
public J.If.Else visitElse(J.If.Else else_, ExecutionContext ctx) {
47+
J.If.Else elseStatement = super.visitElse(else_, ctx);
48+
Statement body = elseStatement.getBody();
49+
if (body instanceof J.Block) {
50+
J.Block block = (J.Block) body;
51+
if (block.getStatements().size() == 1) {
52+
Statement firstStatement = block.getStatements().get(0);
53+
if (firstStatement instanceof J.If) {
54+
// Combine comments from the block and the if statement
55+
J.If ifStatement = firstStatement
56+
.withPrefix(Space.SINGLE_SPACE)
57+
.withComments(ListUtils.concatAll(block.getComments(), firstStatement.getComments()));
58+
return elseStatement.withBody(indent(ifStatement, getCursor(), -1));
59+
}
60+
}
61+
}
62+
return elseStatement;
63+
}
64+
};
65+
}
66+
}

src/main/resources/META-INF/rewrite/common-static-analysis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ recipeList:
8484
- org.openrewrite.staticanalysis.SimplifyArraysAsList
8585
- org.openrewrite.staticanalysis.SimplifyBooleanExpression
8686
- org.openrewrite.staticanalysis.SimplifyBooleanReturn
87+
# - org.openrewrite.staticanalysis.SimplifyElseBranch # TODO Add once we've tried this out at scale
8788
# - org.openrewrite.staticanalysis.SimplifyTernaryRecipes
8889
- org.openrewrite.staticanalysis.StaticMethodNotFinal
8990
- org.openrewrite.staticanalysis.StringLiteralEquality

0 commit comments

Comments
 (0)