Skip to content

Groovy support for multiple assignment/tuple unpacking #5293

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

sudouser777
Copy link
Contributor

  1. Updated the groovy parser to support tuple unpacking.
  2. closes Groovy parser fails with parentheses on the left-hand side of the assignment #5283 .

@sudouser777
Copy link
Contributor Author

I have tried to hanlde the spaces situation, but it's tricky spaces are not being handled properly

    @Test
    void destructuringWithSpaces() {
        rewriteRun(
          groovy(
            """
              def ( key, value ) = "a1:b2".split(":")
              println(key)
              println(value)
              """
          )
        );
    }

Also static type also not working

  @Test
  void destructuringWithType() {
      rewriteRun(
        groovy(
          """
            def (String key, String value) = "a1:b2".split(":")
            println(key)
            println(value)
            """
        )
      );
  }

@greg-at-moderne I'm not sure if my approach is correct—open to any suggestions.

Comment on lines 1376 to 1377
namedVariables.set(first, namedVariables.get(first).withPrefix(prefixBeforeOpenParentheses).withMarkers(Markers.build(singletonList(new OpenParentheses(randomId())))));
namedVariables.set(last, namedVariables.get(last).withMarkers(Markers.build(singletonList(new CloseParentheses(randomId())))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is usually a JContainer that holds the named variables, then wrapped inside of a J.Parentheses instead. This then means that the markers wouldn't be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's not including parenthesis, it's just returning JRightPadded<J.Identifier>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was more in general. The way to add parentheses around an element is with J.Parentheses and when that element is a list it is typically wrapped in a JContainer.

I'm not sure what elements you currently have in scope from the Groovy AST, but I'd expect that something similar would be the case here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's using the same J.VariableDeclarations for normal variable declarations. This will require changes to the J.VariableDeclarations class. I'm not entirely sure about this approach, though.

github-actions[bot]

This comment was marked as outdated.

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite May 5, 2025
@greg-at-moderne greg-at-moderne self-assigned this May 5, 2025
github-actions[bot]

This comment was marked as outdated.

@timtebeek
Copy link
Member

Thanks a lot for the continued improvements to the Groovy parser @sudouser777 ! Great to see steady progress being made.

Any reason you'd held off on a merge here @greg-at-moderne ?

Comment on lines +1427 to +1428
namedVariables = ListUtils.mapFirst(namedVariables, first -> first.withPrefix(prefixBeforeOpenParentheses).withMarkers(Markers.build(singletonList(new OpenParentheses(randomId())))));
namedVariables = ListUtils.mapLast(namedVariables, last -> last.withMarkers(Markers.build(singletonList(new CloseParentheses(randomId())))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than adding new markers, is there any way we can make this work with regular J.Parentheses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's using J.VariableDeclarations, which is also used for normal variable declarations. If we add J.Parentheses, I'm not sure how many changes it would require in the existing code, and I'm also not sure whether it's possible to make the parentheses optional.

I'm open to suggestions—let me know if there's a better way we can handle this.

@greg-at-moderne
Copy link
Contributor

Any reason you'd held off on a merge here @greg-at-moderne ?

No. I am good. I think this is good to be merged now.
Your comment on the PR is the only outstanding (minor) thing in my perspective.

@greg-at-moderne greg-at-moderne self-requested a review May 8, 2025 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request parser-groovy
Projects
Status: Ready to Review
Development

Successfully merging this pull request may close these issues.

Groovy parser fails with parentheses on the left-hand side of the assignment
4 participants