-
Notifications
You must be signed in to change notification settings - Fork 354
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
Fix groovy multivariable declaration #4757
Conversation
rewrite-groovy/src/test/java/org/openrewrite/groovy/GroovyVisitorTest.java
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Saving others a click with the failures seen: https://ge.openrewrite.org/s/nr7naekjegaas/tests/overview?outcome=FAILED wildcardWithUpperBound() First one fails with
diff --git a/file.groovy b/file.groovy
index 3861186..ac7fbf9 100644
--- a/file.groovy
+++ b/file.groovy
@@ -1 +1 @@
-List<? extends String> l
\ No newline at end of file
+List<?< extends String> l
\ No newline at end of file
|
I simplified a little too far, reverted that partially and now they work (locally) |
rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java
Outdated
Show resolved
Hide resolved
One remaining failure it seems in org.openrewrite.groovy.tree.AssignmentTest/simpleWithFinal()
|
rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/AssignmentTest.java
Show resolved
Hide resolved
rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java
Outdated
Show resolved
Hide resolved
rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java
Outdated
Show resolved
Hide resolved
Include subpart of remainder to give an indication where the issue occurred
rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java
Outdated
Show resolved
Hide resolved
rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java
Outdated
Show resolved
Hide resolved
""" | ||
final def x = "x" | ||
def final y = "y" | ||
final z = "z" | ||
""" |
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.
Perhaps good to call out here that we now model final def
and def final
as two unique concatenated Identifiers, which seems odd to me:
----G.CompilationUnit | "G.CompilationUnit(typesInUse=org.openrewrite.java.internal.TypesInUse@745c2004, padding=org.openrewrite.groovy.tree.G..."
|---J.VariableDeclarations | "final def x = "x""
| |---J.Identifier | "final def"
| \-------J.VariableDeclarations.NamedVariable | "x = "x""
| |---J.Identifier | "x"
| \-------J.Literal | ""x""
|---J.VariableDeclarations | "final y = "y""
| |---J.Identifier | "final"
| \-------J.VariableDeclarations.NamedVariable | "y = "y""
| |---J.Identifier | "y"
| \-------J.Literal | ""y""
\---J.VariableDeclarations | "final z = "z""
|---J.Identifier | "final"
\-------J.VariableDeclarations.NamedVariable | "z = "z""
|---J.Identifier | "z"
\-------J.Literal | ""z""
Note that I don't expect this to occur often, if at all, and it still prints OK; but figured comment it here for a closer look by others reviewing as well.
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.
While this isn't perfect either, I think I would opt for mapping all of final
, def
, and var
to modifiers (J.Modifier
) and instead deprecating the RedundantDef
marker, while still supporting it in the printer to remain backwards compatible.
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.
Implemented this. I'm not sure if we can actually mark the RedundantDef
marker as deprecated though as that may also apply to declared methods, so I left that in.
As we discussed we may want to find a way to combine consecutive DeclarationExpression
s into one J.VariableDeclarations
. That could be a further improvement and I'm willing to work on that, but want to return focus to the parenthesis issue as well :)
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.
At least marking the RedundantDef
marker as @deprecated should not be a problem. If you have the option to remove the maybeRedundantDef
method as well, all the better. As long as you keep the RedundantDef
marker in printer, you are still backwards compatible.
__
Also notice the RedundantDef
marker has ben added lately (05/12/2024). So the sooner we no longer use it, the less serialised LSTs do have this marker.
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.
@sambsnyd Since you recently added that RedundantDef
, do you also want to chime in here and tell us what your thoughts are about this?
rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java
Outdated
Show resolved
Hide resolved
""" | ||
final def x = "x" | ||
def final y = "y" | ||
final z = "z" | ||
""" |
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.
While this isn't perfect either, I think I would opt for mapping all of final
, def
, and var
to modifiers (J.Modifier
) and instead deprecating the RedundantDef
marker, while still supporting it in the printer to remain backwards compatible.
rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java
Outdated
Show resolved
Hide resolved
statements.set(i, variableDeclarations.withTypeExpression(variableDeclarations.getTypeExpression().withPrefix(originalPrefix))); | ||
} | ||
} else if (!currentStatement.getPrefix().equals(originalPrefix)) { | ||
if (!currentStatement.getPrefix().equals(originalPrefix)) { |
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.
It is very unclear to me what these changes here have to do with the parsing issue. Is a comment required here?
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.
Due to the way DeclarationExpression
s were parsed into J.VariableDeclarations
we needed a specific check here. After my changes the parsing is more in-line with other types and so we no longer need that exceptional case here!
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 we should be able to proceed with this. Is it difficult to solve this without the MultiVariable
marker?
I haven't looked into it too much yet but I'd imagine somewhere after this block: rewrite/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java Lines 203 to 214 in d449490
VariableDeclarations together
|
What's changed?
When parsing multivariable declarations now takes into account that no traditional identifier is available for any variable after the first
What's your motivation?
Anything in particular you'd like reviewers to focus on?
Anyone you would like to review specifically?
@timtebeek @jevanlingen @sambsnyd @knutwannheden
Have you considered any alternatives or workarounds?
Any additional context
The Groovy AST returns multivariable declarations as separate
DeclarationExpression
s within a block. Therefore we cannot easily map them toJ.VariableDeclarations
Checklist