Skip to content

Commit

Permalink
Adopt prefix of removed element
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Nov 15, 2024
1 parent 274bec8 commit f56de4e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
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 @@ -35,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 @@ -51,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 @@ -66,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 @@ -77,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 @@ -86,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 @@ -98,7 +101,6 @@ 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(); }"
Expand All @@ -111,10 +113,13 @@ void deleteConstructorArgument() {
void deleteFirstArgument() {
rewriteRun(
spec -> spec.recipe(new DeleteMethodArgument("B foo(int, int, int)", 0)),
java(b),
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); }}"
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +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(Space.EMPTY));
args.set(0, args.get(0).withPrefix(removed.getPrefix()));
}

m = m.withArguments(args);
Expand Down

0 comments on commit f56de4e

Please sign in to comment.