-
Notifications
You must be signed in to change notification settings - Fork 88
Add RemovePrivateFieldUnderscores recipe #643
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?
Add RemovePrivateFieldUnderscores recipe #643
Conversation
Let us know if you'd need any guidance @jhl221123 ! Perhaps this session could help you get started: |
This recipe removes prefix or suffix underscores from private class field names. This helps enforce modern Java coding standards. For clarity and consistency, the recipe also adds a qualifier to all updated field accesses. This prevents ambiguity with local variables and ensures a uniform style throughout the class. Fixes openrewrite#630
264ca2a
to
0e9ede6
Compare
java( | ||
""" | ||
public class ParseLocation { | ||
private String _ruleName; |
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.
I think it would be good to add a test showing either class_
or _class
to show we do not end up renaming to restricted keywords.
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.
Sorry for the delay. I've added the test case!
src/main/java/org/openrewrite/staticanalysis/RemovePrivateFieldUnderscores.java
Outdated
Show resolved
Hide resolved
…dUnderscores.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
private String ruleName; | ||
|
||
public String getRuleName() { | ||
return this.ruleName; |
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
is unnecessary where there is no ambiguity. I think a recipe should do one thing which, in this case, is remove the underscore. IMO, this behavior should be removed from the visitor.
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.
Thanks for the feedback! Your point about recipes having a single responsibility makes a lot of sense. I'll get this updated shortly.
Another case:
In this case, I would expect no change. |
- Qualify with this. only when a field is shadowed by a local variable or parameter. - Align tests with the new behavior and improve their clarity.
Thanks for the detailed feedback, @dsgrieve. I believe I've addressed everything now. Ready for another look when you have a moment. |
Sorry. I keep stumbling upon use cases. Be aware of JEP 456 which allows an |
- Skips renaming if the resulting name would be empty, _, or still start/end with an underscore.
@dsgrieve No problem at all, and thank you for catching these important edge cases! I've pushed an update to handle it. Please let me know if you spot anything else. |
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.
LGTM, but I defer to @timtebeek for approval.
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.
Another test case worth adding is using underscore in the middle of the name like:
String first_name;
. No change expected then.
return id; | ||
} | ||
|
||
private boolean isAmbiguous() { |
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.
I am wondering if it wouldn't be better to re-use the existing logic of org.openrewrite.java.VariableNameUtils#generateVariableName
. I'd call it with the newName
as the baseName
and refrain from any changes if the result is different.
This way:
- the code is shorter here
- and likely more thoroughly tested / better coverage of the naming logic
Nice work on the recipe. |
What's changed?
This recipe removes prefix or suffix underscores from private class field names. This helps enforce modern Java coding standards.
For clarity and consistency, the recipe also adds a qualifier to all updated field accesses. This prevents ambiguity with local variables and ensures a uniform style throughout the class.
What's your motivation?
Checklist