Skip to content

Commit

Permalink
Support variable expressions in parenthesis (#4751)
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen authored Dec 6, 2024
1 parent 45f228a commit 7013518
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 132 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ void insideParentheses() {

// NOT inside parentheses, but verifies the parser's
// test for "inside parentheses" condition
groovy("(1) + 1")
groovy("(1) + 1"),
// And combine the two cases
groovy("((1) + 1)")
);
}

Expand Down Expand Up @@ -62,6 +64,19 @@ void in() {
);
}

@Test
void withVariable() {
rewriteRun(
groovy(
"""
def foo(int a) {
60 + a
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1531")
@Test
void regexFindOperator() {
Expand Down Expand Up @@ -201,17 +216,12 @@ void stringMultipliedInParentheses() {
);
}

@Disabled
@Issue("https://github.com/openrewrite/rewrite/issues/4703")
@Test
void extraParensAroundInfixOperator() {
rewriteRun(
groovy(
"""
def foo(Map map) {
((map.containsKey("foo"))
&& ((map.get("foo")).equals("bar")))
}
def timestamp(int hours, int minutes, int seconds) {
(hours) * 60 * 60 + (minutes * 60) + seconds
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

Expand Down Expand Up @@ -79,6 +80,18 @@ void groovyCastAndInvokeMethod() {
);
}

@Test
@ExpectedToFail("Parentheses with method invocation is not yet supported")
void groovyCastAndInvokeMethodWithParentheses() {
rewriteRun(
groovy(
"""
(((((( "" as String ))))).toString())
"""
)
);
}

@Test
void javaCastAndInvokeMethod() {
rewriteRun(
Expand All @@ -89,4 +102,16 @@ void javaCastAndInvokeMethod() {
)
);
}

@ExpectedToFail("Parentheses with method invocation is not yet supported")
@Test
void javaCastAndInvokeMethodWithParentheses() {
rewriteRun(
groovy(
"""
(((((( (String) "" )).toString()))))
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

Expand Down Expand Up @@ -45,6 +46,22 @@ void gradle() {
);
}

@ExpectedToFail("Parentheses with method invocation is not yet supported")
@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4615")
void gradleWithParentheses() {
rewriteRun(
groovy(
"""
plugins {
id 'java-library'
}
def version = (rootProject.jobName.startsWith('a')) ? "latest.release" : "3.0"
"""
)
);
}

@Test
void emptyArgsWithParens() {
rewriteRun(
Expand Down Expand Up @@ -262,4 +279,33 @@ static void main(String[] args) {
)
);
}

@ExpectedToFail("Parentheses with method invocation is not yet supported")
@Issue("https://github.com/openrewrite/rewrite/issues/4703")
@Test
void insideParentheses() {
rewriteRun(
groovy(
"""
static def foo(Map map) {
((map.containsKey("foo"))
&& ((map.get("foo")).equals("bar")))
}
"""
)
);
}

@ExpectedToFail("Parentheses with method invocation is not yet supported")
@Issue("https://github.com/openrewrite/rewrite/issues/4703")
@Test
void insideParenthesesWithoutNewLineAndEscapedMethodName() {
rewriteRun(
groovy(
"""
static def foo(Map someMap) {((((((someMap.get("(bar")))) ).'equals' "baz" ) ) }
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;
Expand Down Expand Up @@ -46,4 +47,18 @@ void parenthesized() {
)
);
}

@ExpectedToFail("Parentheses with method invocation is not yet supported")
@Test
void parenthesizedAndInvokeMethodWithParentheses() {
rewriteRun(
groovy(
"""
(((( 8..19 ))).each { majorVersion ->
if (majorVersion == 9) return
})
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ void insideParentheses() {

// NOT inside parentheses, but verifies the parser's
// test for "inside parentheses" condition
groovy("(true) ? 1 : 2")
groovy("(true) ? 1 : 2"),
// And combine the two cases
groovy("((true) ? 1 : 2)")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,17 @@ public String toString() {
printedWs.append(spaces[(i - lastNewline) % 10]);
} else if (c == '\t') {
printedWs.append(tabs[(i - lastNewline) % 10]);
} else {
// should never happen (probably a bug in the parser)
printedWs.append(c);
}
}
}
String whitespaces = printedWs.toString();

return "Space(" +
"comments=<" + (comments.size() == 1 ? "1 comment" : comments.size() + " comments") +
">, whitespace='" + printedWs + "')";
"comments=<" + (comments.size() == 1 ? "1 comment" : comments.size() + " comments") + ">, " +
"whitespace=" + (whitespaces.isEmpty() ? "<empty>" : "'" + whitespaces + "'") + ")";
}

public enum Location {
Expand Down

0 comments on commit 7013518

Please sign in to comment.