Skip to content
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

Groovy parser does not support multiple closure arguments without parentheses #4766

Closed
jevanlingen opened this issue Dec 10, 2024 · 0 comments · Fixed by #4772
Closed

Groovy parser does not support multiple closure arguments without parentheses #4766

jevanlingen opened this issue Dec 10, 2024 · 0 comments · Fixed by #4772
Assignees
Labels
bug Something isn't working parser-groovy

Comments

@jevanlingen
Copy link
Contributor

jevanlingen commented Dec 10, 2024

Problem

Groovy let you omit the parentheses for function arguments. This does not work if you use multiple closures.

What is the smallest, simplest way to reproduce the problem?

@Test
void multipleClosureArguments() {
    rewriteRun(
        groovy(
           """
           def acceptsClosure(Closure a, Closure b) {}
           acceptsClosure { println(it) } {
               println(it)
            }
           """
        )
     );
 }

What did you expect to see?

A test that succeeds.

What did you see instead?

expected: 
  "def acceptsClosure(Closure a, Closure b) {}
  acceptsClosure { println(it) } {
      println(it)
  }"
 but was: 
  "def acceptsClosure(Closure a, Closure b) {}
  acceptsClosure { println(it) } ,
      {println(it)
  }"

Note

The groovy language is rather picky with omitting parentheses. For closures no comma is allowed, except when you join them with other types. So you need to define it like this, or it won't compile:

def acceptsClosure(Closure a, Closure b) {}
acceptsClosure { println(it) } { println(it) }

def functionX(String a, String b, String c, String d) {}
functionX "a", "b", "c", "d"

def combination(String a, Closure b, Closure c, String d) {}
combination "a", { println(it) }, { println(it) }, "d"

def combination2(Closure a, String d) {}
combination2({ println(it) }, "d") // starting with a closure seems not be possible without parentheses!
@jevanlingen jevanlingen added the bug Something isn't working label Dec 10, 2024
@jevanlingen jevanlingen self-assigned this Dec 11, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Dec 11, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenRewrite Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser-groovy
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant