Repository navigation
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.
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.
Sorry for the delay. I've added the test case!
…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.
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.
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. |
dsgrieve
left a comment
There was a problem hiding this comment.
LGTM, but I defer to @timtebeek for approval.
greg-at-moderne
left a comment
There was a problem hiding this comment.
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.
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. |
|
This PR is stale because it has been open for 90 days with no activity. Remove |
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