-
Notifications
You must be signed in to change notification settings - Fork 73
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
Add a ScopedVariable
trait
#604
base: main
Are you sure you want to change the base?
Conversation
…e.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
import org.openrewrite.trait.Trait; | ||
|
||
@Value | ||
public class ScopedVariable implements Trait<J.VariableDeclarations.NamedVariable> { |
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.
Shouldn't we instead create this trait in the rewrite repo?
protected @Nullable ScopedVariable test(Cursor cursor) { | ||
if (cursor.getValue() instanceof J.VariableDeclarations.NamedVariable) { | ||
J.VariableDeclarations.NamedVariable variable = cursor.getValue(); | ||
return new ScopedVariable(cursor, variable.getDeclaringScope(cursor), variable.getName()); |
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 getDeclaringScope()
utility method is probably a good starting point. But to me it looks like it misses cases like try-with-resources and exception handlers, for example. Also note that the flow scoping of instanceof patterns is quite different and probably a bad fit. Maybe worth thinking about at least.
|
||
@Override | ||
protected @Nullable ScopedVariable test(Cursor cursor) { | ||
if (cursor.getValue() instanceof J.VariableDeclarations.NamedVariable) { |
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.
We can likely just use variable.getDeclaringScope(cursor) to obtain the variable's scope; using a trait might be overkill.
A scenario where a trait could be useful, suppose I'm at an identifier and need to determine the declaring scope of the variable that this identifier references.
void foo() {
int x = 5;
if (true) {
print(x) // find the declaring scope of x at this point,
}
}
What's changed?
Added a trait to pull together the scope and identifier of
NamedVariable
s and added testcases to showcase its usageAnyone you would like to review specifically?
@sambsnyd @timtebeek
Checklist