Skip to content

Commit

Permalink
Remove redundant space for DeleteMethodArgument when argumentIndex=0 (#…
Browse files Browse the repository at this point in the history
…4677)

* Remove redundant space for DeleteMethodArgument when argumentIndex=0

Signed-off-by: kuchang <[email protected]>

* Update FindPluginsTest.java

* Adopt prefix of removed element

---------

Signed-off-by: kuchang <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
ckcd and timtebeek authored Nov 15, 2024
1 parent 58f15f7 commit 5ef0ef1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ void findPlugin() {
void settingsResolutionStrategy() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi()),
settingsGradle("""
settingsGradle(
"""
pluginManagement {
repositories {
mavenLocal()
Expand All @@ -83,7 +84,8 @@ void settingsResolutionStrategy() {
}
}
}
"""),
"""
),
buildGradle(
"""
plugins {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class DeleteMethodArgumentTest implements RewriteTest {

@Language("java")
String b = """
class B {
Expand All @@ -34,11 +37,15 @@ public B(int n) {}
}
""";

@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().dependsOn(b));
}

@Test
void deleteMiddleArgumentDeclarative() {
rewriteRun(
spec -> spec.recipes(new DeleteMethodArgument("B foo(int, int, int)", 1)),
java(b),
java(
"public class A {{ B.foo(0, 1, 2); }}",
"public class A {{ B.foo(0, 2); }}"
Expand All @@ -50,7 +57,6 @@ void deleteMiddleArgumentDeclarative() {
void deleteMiddleArgument() {
rewriteRun(
spec -> spec.recipe(new DeleteMethodArgument("B foo(int, int, int)", 1)),
java(b),
java(
"public class A {{ B.foo(0, 1, 2); }}",
"public class A {{ B.foo(0, 2); }}"
Expand All @@ -65,8 +71,8 @@ void deleteArgumentsConsecutively() {
new DeleteMethodArgument("B foo(int, int, int)", 1),
new DeleteMethodArgument("B foo(int, int)", 1)
),
java(b),
java("public class A {{ B.foo(0, 1, 2); }}",
java(
"public class A {{ B.foo(0, 1, 2); }}",
"public class A {{ B.foo(0); }}"
)
);
Expand All @@ -76,7 +82,6 @@ void deleteArgumentsConsecutively() {
void doNotDeleteEmptyContainingFormatting() {
rewriteRun(
spec -> spec.recipe(new DeleteMethodArgument("B foo(..)", 0)),
java(b),
java("public class A {{ B.foo( ); }}")
);
}
Expand All @@ -85,7 +90,6 @@ void doNotDeleteEmptyContainingFormatting() {
void insertEmptyWhenLastArgumentIsDeleted() {
rewriteRun(
spec -> spec.recipe(new DeleteMethodArgument("B foo(..)", 0)),
java(b),
java(
"public class A {{ B.foo(1); }}",
"public class A {{ B.foo(); }}"
Expand All @@ -97,11 +101,26 @@ void insertEmptyWhenLastArgumentIsDeleted() {
void deleteConstructorArgument() {
rewriteRun(
spec -> spec.recipe(new DeleteMethodArgument("B <constructor>(int)", 0)),
java(b),
java(
"public class A { B b = new B(0); }",
"public class A { B b = new B(); }"
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/4676")
@Test
void deleteFirstArgument() {
rewriteRun(
spec -> spec.recipe(new DeleteMethodArgument("B foo(int, int, int)", 0)),
java(
"public class A {{ B.foo(0, 1, 2); }}",
"public class A {{ B.foo(1, 2); }}"
),
java(
"public class C {{ B.foo(\n\t\t0, 1, 2); }}",
"public class C {{ B.foo(\n\t\t1, 2); }}"
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ private MethodCall visitMethodCall(MethodCall methodCall) {
.count() >= argumentIndex + 1) {
List<Expression> args = new ArrayList<>(originalArgs);

args.remove(argumentIndex);
Expression removed = args.remove(argumentIndex);
if (args.isEmpty()) {
args = singletonList(new J.Empty(randomId(), Space.EMPTY, Markers.EMPTY));
} else if (argumentIndex == 0) {
args.set(0, args.get(0).withPrefix(removed.getPrefix()));
}

m = m.withArguments(args);
Expand Down

0 comments on commit 5ef0ef1

Please sign in to comment.