Skip to content

Commit a46a5ac

Browse files
authored
Skip plugins block in UsePropertyAssignmentSyntax (#7337)
* Skip plugins block in UsePropertyAssignmentSyntax The recipe was incorrectly converting `version '3.19'` inside `plugins { }` blocks to `.version = '3.19'`. In the Gradle plugins DSL, `version` is part of the plugin declaration syntax, not a property assignment. Walk the cursor tree to detect and skip method invocations inside plugins blocks. * Skip plugins block by not recursing into it Instead of checking cursor ancestry on each match, skip the super.visitMethodInvocation call for plugins blocks so their children are never visited.
1 parent 3305049 commit a46a5ac

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/UsePropertyAssignmentSyntax.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5454
return Preconditions.check(new FindSourceFiles("**/*.gradle"), new JavaVisitor<ExecutionContext>() {
5555
@Override
5656
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
57+
if ("plugins".equals(method.getSimpleName())) {
58+
return method;
59+
}
60+
5761
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
5862

5963
if (m.getArguments().size() != 1 || m.getArguments().get(0) instanceof J.Empty) {

rewrite-gradle/src/test/java/org/openrewrite/gradle/UsePropertyAssignmentSyntaxTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import static org.openrewrite.gradle.Assertions.buildGradle;
2424
import static org.openrewrite.gradle.Assertions.buildGradleKts;
25+
import static org.openrewrite.gradle.Assertions.settingsGradle;
2526

2627
class UsePropertyAssignmentSyntaxTest implements RewriteTest {
2728

@@ -170,6 +171,49 @@ void kotlinDslMethodCallUnchanged() {
170171
);
171172
}
172173

174+
@Test
175+
void pluginVersionUnchangedInPluginsBlock() {
176+
rewriteRun(
177+
spec -> spec.recipe(new UsePropertyAssignmentSyntax("version")),
178+
settingsGradle(
179+
"""
180+
plugins {
181+
id 'com.gradle.develocity' version '3.19'
182+
}
183+
"""
184+
)
185+
);
186+
}
187+
188+
@Test
189+
void pluginVersionUnchangedInBuildGradlePluginsBlock() {
190+
rewriteRun(
191+
spec -> spec.recipe(new UsePropertyAssignmentSyntax("version")),
192+
buildGradle(
193+
"""
194+
plugins {
195+
id 'com.example.plugin' version '1.0'
196+
}
197+
"""
198+
)
199+
);
200+
}
201+
202+
@Test
203+
void versionOutsidePluginsBlockConverted() {
204+
rewriteRun(
205+
spec -> spec.recipe(new UsePropertyAssignmentSyntax("version")),
206+
buildGradle(
207+
"""
208+
version '1.0-SNAPSHOT'
209+
""",
210+
"""
211+
version = '1.0-SNAPSHOT'
212+
"""
213+
)
214+
);
215+
}
216+
173217
@Test
174218
void noArgMethodCallUnchanged() {
175219
rewriteRun(

0 commit comments

Comments
 (0)