-
Notifications
You must be signed in to change notification settings - Fork 89
Add ReplaceIfElseWithTernary #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
timtebeek
commented
Aug 6, 2025
- Fixes Recipe to replace trivial if statements with ternary operator #683
J.Assignment result = template.apply(getCursor(), i.getCoordinates().replace(), | ||
thenAssign.getVariable(), | ||
i.getIfCondition().getTree(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
J.Assignment result = template.apply(getCursor(), i.getCoordinates().replace(), | |
thenAssign.getVariable(), | |
i.getIfCondition().getTree(), | |
return template.apply(getCursor(), i.getCoordinates().replace(), |
J.Identifier id1 = (J.Identifier) a1.getVariable(); | ||
J.Identifier id2 = (J.Identifier) a2.getVariable(); | ||
return id1.getSimpleName().equals(id2.getSimpleName()); | ||
} else if (a1.getVariable() instanceof J.FieldAccess && a2.getVariable() instanceof J.FieldAccess) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else if (a1.getVariable() instanceof J.FieldAccess && a2.getVariable() instanceof J.FieldAccess) { | |
} | |
if (a1.getVariable() instanceof J.FieldAccess && a2.getVariable() instanceof J.FieldAccess) { |
Reordered examples in examples.yml and added ReplaceIfElseWithTernary example 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
J.Assignment result = template.apply(getCursor(), i.getCoordinates().replace(), | ||
thenAssign.getVariable(), | ||
i.getIfCondition().getTree(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
J.Assignment result = template.apply(getCursor(), i.getCoordinates().replace(), | |
thenAssign.getVariable(), | |
i.getIfCondition().getTree(), | |
return template.apply(getCursor(), i.getCoordinates().replace(), |
J.Identifier id1 = (J.Identifier) a1.getVariable(); | ||
J.Identifier id2 = (J.Identifier) a2.getVariable(); | ||
return id1.getSimpleName().equals(id2.getSimpleName()); | ||
} else if (a1.getVariable() instanceof J.FieldAccess && a2.getVariable() instanceof J.FieldAccess) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else if (a1.getVariable() instanceof J.FieldAccess && a2.getVariable() instanceof J.FieldAccess) { | |
} | |
if (a1.getVariable() instanceof J.FieldAccess && a2.getVariable() instanceof J.FieldAccess) { |
// Check if both statements are assignments to the same variable | ||
if (!(thenStatement instanceof J.Assignment) || !(elseStatement instanceof J.Assignment)) { | ||
return i; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could possibly also handle return
statements.
Could be done in a separate PR though.
} else if (a1.getVariable() instanceof J.FieldAccess && a2.getVariable() instanceof J.FieldAccess) { | ||
J.FieldAccess fa1 = (J.FieldAccess) a1.getVariable(); | ||
J.FieldAccess fa2 = (J.FieldAccess) a2.getVariable(); | ||
return fa1.toString().equals(fa2.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we should be comparing toStrings.
} | ||
|
||
// Check if expression contains a ternary operator | ||
boolean[] hasTernary = {false}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The array seems like a hack. AtomicBoolean
would be clearer.