Skip to content

Commit

Permalink
do not remove NOT if inside binary operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Viladrosa committed Dec 13, 2023
1 parent e57dac0 commit 9d2d86e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (enumEquals.matches(m) && m.getSelect() != null) {
Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Unary || is instanceof J.Block);
Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Unary || is instanceof J.Block || is instanceof J.Binary);
boolean isNot = parent.getValue() instanceof J.Unary && ((J.Unary) parent.getValue()).getOperator() == J.Unary.Type.Not;
if (isNot) {
parent.putMessage("REMOVE_UNARY_NOT", parent.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,34 @@ boolean isFoo() {
""")
);
}

@SuppressWarnings("StatementWithEmptyBody")
@Issue("https://github.com/moderneinc/customer-requests/issues/190")
@Test
void changeEnumInsideBooleanExpression() {
rewriteRun(
enumA,
//language=java
java(
"""
import a.A;
class Test {
void method(A arg0) {
if (!(A.FOO.equals(arg0) || A.BAR.equals(arg0))) {
}
}
}
""",
"""
import a.A;
class Test {
void method(A arg0) {
if (!(A.FOO == arg0 || A.BAR == arg0)) {
}
}
}
"""
)
);
}
}

0 comments on commit 9d2d86e

Please sign in to comment.