Skip to content

Commit

Permalink
rewrite escaped groovy method names, add gradle dependencies with the…
Browse files Browse the repository at this point in the history
… default scope using the escaped method name syntax (#3563)

Co-authored-by: Michael Nielson <[email protected]>
  • Loading branch information
safetytrick and Michael Nielson authored Sep 25, 2023
1 parent 0800a08 commit a301363
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.gradle;

import lombok.RequiredArgsConstructor;

import org.openrewrite.*;
import org.openrewrite.gradle.internal.InsertDependencyComparator;
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
Expand All @@ -37,7 +38,6 @@
import org.openrewrite.semver.*;
import org.openrewrite.tree.ParseError;

import java.text.ParseException;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -239,11 +239,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
DependencyStyle style = autodetectDependencyStyle(body.getStatements());
if (style == DependencyStyle.String) {
codeTemplate = "dependencies {\n" +
configuration + " \"" + groupId + ":" + artifactId + (resolvedVersion == null ? "" : ":" + resolvedVersion) + (resolvedVersion == null || classifier == null ? "" : ":" + classifier) + (extension == null ? "" : "@" + extension) + "\"" +
escapeIfNecessary(configuration) + " \"" + groupId + ":" + artifactId + (resolvedVersion == null ? "" : ":" + resolvedVersion) + (resolvedVersion == null || classifier == null ? "" : ":" + classifier) + (extension == null ? "" : "@" + extension) + "\"" +
"\n}";
} else {
codeTemplate = "dependencies {\n" +
configuration + " group: \"" + groupId + "\", name: \"" + artifactId + "\"" + (resolvedVersion == null ? "" : ", version: \"" + resolvedVersion + "\"") + (classifier == null ? "" : ", classifier: \"" + classifier + "\"") + (extension == null ? "" : ", ext: \"" + extension + "\"") +
escapeIfNecessary(configuration) + " group: \"" + groupId + "\", name: \"" + artifactId + "\"" + (resolvedVersion == null ? "" : ", version: \"" + resolvedVersion + "\"") + (classifier == null ? "" : ", classifier: \"" + classifier + "\"") + (extension == null ? "" : ", ext: \"" + extension + "\"") +
"\n}";
}

Expand Down Expand Up @@ -333,6 +333,12 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}
}

private String escapeIfNecessary(String configurationName) {
// default is a gradle configuration created by the base plugin and a groovy keyword if
// it is used it needs to be escaped
return configurationName.equals("default") ? "'" + configurationName + "'" : configurationName;
}

enum DependencyStyle {
Map, String
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,44 @@ void addDependencyWithVariable() {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/3559")
void defaultConfigurationEscaped() {
String onlyIfUsing = "com.google.common.math.IntMath";
rewriteRun(
spec -> spec.recipe(addDependency("com.google.guava:guava:29.0-jre", onlyIfUsing, "default")),
mavenProject("project",
srcTestJava(
java(usingGuavaIntMath)
),
buildGradle(
"""
plugins {
id 'java'
}
repositories {
mavenCentral()
}
""",
"""
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
'default' "com.google.guava:guava:29.0-jre"
}
"""
)
)
);
}

private AddDependency addDependency(String gav, String onlyIfUsing) {
return addDependency(gav, onlyIfUsing, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1469,9 +1469,18 @@ public void visitMethodCallExpression(MethodCallExpression call) {
// closure() has implicitThis set to false
// So the "select" that was just parsed _may_ have actually been the method name
J.Identifier name;
if (call.getMethodAsString().equals(source.substring(cursor, cursor + call.getMethodAsString().length()))) {
name = new J.Identifier(randomId(), sourceBefore(call.getMethodAsString()), Markers.EMPTY,
emptyList(), call.getMethodAsString(), null, null);

String methodNameExpression = call.getMethodAsString();
if (source.charAt(cursor) == '"' || source.charAt(cursor) == '\'') {
// we have an escaped groovy method name, commonly used for test `def 'some scenario description'() {}`
// or to workaround names that are also keywords in groovy
methodNameExpression = source.charAt(cursor) + methodNameExpression + source.charAt(cursor);
}


if (methodNameExpression.equals(source.substring(cursor, cursor + methodNameExpression.length()))) {
name = new J.Identifier(randomId(), sourceBefore(methodNameExpression), Markers.EMPTY,
emptyList(), methodNameExpression, null, null);

} else if (select != null && select.getElement() instanceof J.Identifier) {
name = (J.Identifier) select.getElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.openrewrite.groovy.Assertions.groovy;

class MethodInvocationTest implements RewriteTest {

@Test
void gradle() {
rewriteRun(
Expand Down Expand Up @@ -263,4 +262,29 @@ static void main(String[] args) {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/3559")
void escapedMethodNameTest() {
rewriteRun(
groovy(
"""
def 'default'() {}
'default'()
"""
)
);
}

@Test
void escapedMethodNameWithSpacesTest() {
rewriteRun(
groovy(
"""
def 'some test scenario description'() {}
'some test scenario description'()
"""
)
);
}
}

0 comments on commit a301363

Please sign in to comment.