Skip to content

Commit

Permalink
Add recipe RemoveTrailingComma
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Dec 11, 2023
1 parent c37be7b commit 4a573ae
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.kotlin.cleanup;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.kotlin.format.TrailingCommaVisitor;

@Value
@EqualsAndHashCode(callSuper = true)
public class RemoveTrailingComma extends Recipe {
@Override
public String getDisplayName() {
return "Remove trailing comma in Kotlin";
}

@Override
public String getDescription() {
return "Remove trailing commas in variable, parameter, and class property lists.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TrailingCommaVisitor<>(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.kotlin.cleanup;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.kotlin.Assertions.kotlin;

class RemoveTrailingCommaTest implements RewriteTest {


@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RemoveTrailingComma());
}

@DocumentExample
@Test
void methodArgs() {
rewriteRun(
kotlin(
"""
fun method(arg1: String,
arg2: String,
) {}
""",
"""
fun method(arg1: String,
arg2: String
) {}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class TrailingCommaTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(toRecipe(() -> new TrailingCommaVisitor(IntelliJ.other().getUseTrailingComma())));
spec.recipe(toRecipe(() -> new TrailingCommaVisitor<>(IntelliJ.other().getUseTrailingComma())));
}

private static Consumer<RecipeSpec> trailingCommaStyle(UnaryOperator<OtherStyle> with) {
return spec -> spec.recipe(toRecipe(() -> new TrailingCommaVisitor(with.apply(IntelliJ.other()).getUseTrailingComma())));
return spec -> spec.recipe(toRecipe(() -> new TrailingCommaVisitor<>(with.apply(IntelliJ.other()).getUseTrailingComma())));
}

@Test
Expand Down

0 comments on commit 4a573ae

Please sign in to comment.