Skip to content

Conversation

timtebeek
Copy link
Member

Comment on lines +95 to +97
J.Assignment result = template.apply(getCursor(), i.getCoordinates().replace(),
thenAssign.getVariable(),
i.getIfCondition().getTree(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} 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]>
Comment on lines +95 to +97
J.Assignment result = template.apply(getCursor(), i.getCoordinates().replace(),
thenAssign.getVariable(),
i.getIfCondition().getTree(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (a1.getVariable() instanceof J.FieldAccess && a2.getVariable() instanceof J.FieldAccess) {
}
if (a1.getVariable() instanceof J.FieldAccess && a2.getVariable() instanceof J.FieldAccess) {

Comment on lines +73 to +76
// Check if both statements are assignments to the same variable
if (!(thenStatement instanceof J.Assignment) || !(elseStatement instanceof J.Assignment)) {
return i;
}
Copy link
Contributor

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());
Copy link
Contributor

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};
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

Recipe to replace trivial if statements with ternary operator
2 participants