From a301363dcb75b70cb2cb09a6333eb2742857ddd2 Mon Sep 17 00:00:00 2001 From: Michael Nielson Date: Mon, 25 Sep 2023 13:01:34 -0600 Subject: [PATCH] rewrite escaped groovy method names, add gradle dependencies with the default scope using the escaped method name syntax (#3563) Co-authored-by: Michael Nielson --- .../gradle/AddDependencyVisitor.java | 12 ++++-- .../openrewrite/gradle/AddDependencyTest.java | 38 +++++++++++++++++++ .../groovy/GroovyParserVisitor.java | 15 ++++++-- .../groovy/tree/MethodInvocationTest.java | 26 ++++++++++++- 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java index dba4be114a7..a2393902b84 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java @@ -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; @@ -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; @@ -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}"; } @@ -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 } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java index 6116fb771bb..1615f9dfed6 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java @@ -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); } diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java index 2e6b59f5975..7617e6fd7ed 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java @@ -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(); diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/MethodInvocationTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/MethodInvocationTest.java index 1b01abe825c..3df4b41bf33 100644 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/MethodInvocationTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/MethodInvocationTest.java @@ -22,7 +22,6 @@ import static org.openrewrite.groovy.Assertions.groovy; class MethodInvocationTest implements RewriteTest { - @Test void gradle() { rewriteRun( @@ -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'() + """ + ) + ); + } }