Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant space for DeleteMethodArgument when argumentIndex=0 #4677

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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