Skip to content

Commit

Permalink
Skip ending parentheses when parsing visitMethodCallExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Nov 26, 2024
1 parent 615a767 commit 8579420
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1654,19 +1654,27 @@ public void visitMapExpression(MapExpression map) {
queue.add(new G.MapLiteral(randomId(), prefix, Markers.EMPTY, entries, typeMapping.type(map.getType())));
}

// zero or more ")" AND "?." or "*." or "." AND <rest>
private final Pattern MATCH_INVOKE = Pattern.compile("^(\\)*)(\\*?\\??\\.).*", Pattern.DOTALL);

@Override
public void visitMethodCallExpression(MethodCallExpression call) {
queue.add(insideParentheses(call, fmt -> {

ImplicitDot implicitDot = null;
JRightPadded<Expression> select = null;
Markers markers = Markers.EMPTY;
if (!call.isImplicitThis()) {
Expression selectExpr = visit(call.getObjectExpression());
int saveCursor = cursor;
Space afterSelect = whitespace();
if (source.charAt(cursor) == '.' || source.charAt(cursor) == '?' || source.charAt(cursor) == '*') {
Matcher matcher = MATCH_INVOKE.matcher(source.substring(cursor));
if (matcher.matches()) {
cursor = saveCursor;
afterSelect = sourceBefore(call.isSpreadSafe() ? "*." : call.isSafe() ? "?." : ".");
afterSelect = sourceBefore(matcher.group(1) + matcher.group(2));
if (!matcher.group(1).isEmpty()) {
afterSelect.withWhitespace(matcher.group(1) + afterSelect.getWhitespace());
}
} else {
implicitDot = new ImplicitDot(randomId());
}
Expand Down Expand Up @@ -1707,7 +1715,6 @@ public void visitMethodCallExpression(MethodCallExpression call) {
name = name.withMarkers(name.getMarkers().add(implicitDot));
}
// Method invocations may have type information that can enrich the type information of its parameters
Markers markers = Markers.EMPTY;
if (call.getArguments() instanceof ArgumentListExpression) {
ArgumentListExpression args = (ArgumentListExpression) call.getArguments();
if (call.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -201,17 +200,26 @@ void stringMultipliedInParentheses() {
);
}

@Disabled
@Issue("https://github.com/openrewrite/rewrite/issues/4703")
@Test
void extraParensAroundInfixOperator() {
void extraParenthesesAroundMethodInvocationSelects() {
rewriteRun(
groovy(
"""
def foo(Map map) {
((map.containsKey("foo"))
&& ((map.get("foo")).equals("bar")))
map.containsKey("foo") && (map.get("foo")).equals("bar")
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/4703")
@Test
void extraParensAroundInfixOperator() {
rewriteRun(
groovy(
"""
def timestamp(int hours, int minutes, int seconds) {
(hours) * 60 * 60 + (minutes * 60) + seconds
}
Expand Down

0 comments on commit 8579420

Please sign in to comment.