GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Add RemovePrivateFieldUnderscores recipe by jhl221123 · Pull Request #643 · openrewrite/rewrite-static-analysis Conversation
|
|
Let us know if you'd need any guidance @jhl221123 ! Perhaps this session could help you get started: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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.
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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…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 an error while loading. Please reload this page.
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Another case: In this case, I would expect no change. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
- 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, something went wrong.
There was an error while loading. Please reload this page.
|
Sorry. I keep stumbling upon use cases. Be aware of JEP 456 which allows an |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
- 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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I defer to @timtebeek for approval.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return id; | ||
| } | ||
|
|
||
| private boolean isAmbiguous() { |
There was an error while loading. Please reload this page.
There was a problem 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:
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Nice work on the recipe. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 90 days with no activity. Remove |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Recipe to remove underscores in private field names
There was an error while loading. Please reload this page.