Skip to content

Commit

Permalink
refactor: OpenRewrite best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne committed Feb 11, 2024
1 parent b26d6fa commit b91e4ab
Show file tree
Hide file tree
Showing 31 changed files with 65 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.openrewrite.text.PlainText;

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class FindKotlinSources extends Recipe {
transient KotlinSourceFile kotlinSourceFile = new KotlinSourceFile(this);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/openrewrite/kotlin/RenameTypeAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.openrewrite.kotlin.tree.K;

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class RenameTypeAlias extends Recipe {

@Option(displayName = "Old alias name",
Expand Down Expand Up @@ -56,7 +56,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new KotlinIsoVisitor<ExecutionContext>() {
@Override
public K.TypeAlias visitTypeAlias(K.TypeAlias typeAlias, ExecutionContext executionContext) {
public K.TypeAlias visitTypeAlias(K.TypeAlias typeAlias, ExecutionContext ctx) {
if (!aliasName.equals(typeAlias.getSimpleName()) || !TypeUtils.isOfClassType(typeAlias.getType(), fullyQualifiedAliasedType)) {
return typeAlias;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import static org.openrewrite.Tree.randomId;

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class EqualsMethodUsage extends Recipe {
@Nullable
private static J.Binary equalsBinaryTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class ImplicitParameterInLambda extends Recipe {
@Override
public String getDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.openrewrite.kotlin.format.TrailingCommaVisitor;

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class RemoveTrailingComma extends Recipe {
@Override
public String getDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@


@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class RemoveTrailingSemicolon extends Recipe {
@Override
public String getDisplayName() {
Expand Down Expand Up @@ -72,7 +72,7 @@ public <M extends Marker> M visitMarker(Marker marker, ExecutionContext ctx) {
}

@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
private static class CollectSemicolonRemovableElements extends KotlinPrinter<Set<Marker>> {
Pattern WS = Pattern.compile("^\\s+");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class ReplaceCharToIntWithCode extends Recipe {
private static final MethodMatcher CHAR_TO_INT_METHOD_MATCHER = new MethodMatcher("kotlin.Char toInt()");
@Nullable
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/openrewrite/kotlin/AddImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.Recipe;
Expand All @@ -34,7 +35,7 @@
import static org.openrewrite.kotlin.Assertions.kotlin;
import static org.openrewrite.test.RewriteTest.toRecipe;

public class AddImportTest implements RewriteTest {
class AddImportTest implements RewriteTest {

@Test
void normalClass() {
Expand Down Expand Up @@ -70,6 +71,7 @@ class A {
);
}

@DocumentExample
@Test
void jvmStaticMember() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.kotlin.Assertions.kotlin;

public class AnnotationMatcherTest implements RewriteTest {
class AnnotationMatcherTest implements RewriteTest {

@Test
void matchAnnotation() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/openrewrite/kotlin/AssertionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import static org.openrewrite.kotlin.Assertions.kotlin;
import static org.openrewrite.test.RewriteTest.toRecipe;

public class AssertionsTest implements RewriteTest {
class AssertionsTest implements RewriteTest {

@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/30")
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
package org.openrewrite.kotlin;

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

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

class ChangeAnnotationAttributeNameTest implements RewriteTest {

@DocumentExample
@Test
public void changeAnnotationAttributes() {
void changeAnnotationAttributes() {
rewriteRun(
spec -> spec.recipe(new ChangeAnnotationAttributeName(
"org.junit.jupiter.api.Tag",
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/openrewrite/kotlin/FindFieldsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.search.FindFields;
import org.openrewrite.test.RewriteTest;

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

public class FindFieldsTest implements RewriteTest {
class FindFieldsTest implements RewriteTest {

@DocumentExample
@Test
void jvmStaticField() {
rewriteRun(
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/openrewrite/kotlin/FindMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.search.FindMethods;
import org.openrewrite.test.RewriteTest;

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

public class FindMethodsTest implements RewriteTest {
class FindMethodsTest implements RewriteTest {

@DocumentExample
@Test
void jvmStaticMethod() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static org.openrewrite.ExecutionContext.REQUIRE_PRINT_EQUALS_INPUT;
import static org.openrewrite.kotlin.Assertions.kotlin;

public class KotlinTypeGoatTest implements RewriteTest {
class KotlinTypeGoatTest implements RewriteTest {
@Language("kotlin")
private static final String goat = StringUtils.readFully(KotlinTypeMappingTest.class.getResourceAsStream("/KotlinTypeGoat.kt"));

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/openrewrite/kotlin/MethodMatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.ExecutionContext;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;
Expand All @@ -27,6 +28,7 @@

class MethodMatcherTest implements RewriteTest {

@DocumentExample
@Test
void matchesTopLevelFunction() {
rewriteRun(
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/openrewrite/kotlin/RemoveImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.kotlin.tree.K;
Expand All @@ -25,8 +26,9 @@
import static org.openrewrite.kotlin.Assertions.kotlin;
import static org.openrewrite.test.RewriteTest.toRecipe;

public class RemoveImportTest implements RewriteTest {
class RemoveImportTest implements RewriteTest {

@DocumentExample
@Test
void jvmStaticMember() {
rewriteRun(
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/openrewrite/kotlin/RenameTypeAliasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -54,6 +55,7 @@ class Test
);
}

@DocumentExample
@Test
void declaration() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package org.openrewrite.kotlin;

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

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

public class UseStaticImportTest implements RewriteTest {
class UseStaticImportTest implements RewriteTest {

@DocumentExample
@Test
void noPriorImports() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static org.openrewrite.kotlin.Assertions.kotlin;


public class ReplaceCharToIntWithCodeTest implements RewriteTest {
class ReplaceCharToIntWithCodeTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ReplaceCharToIntWithCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin.format;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.tree.J;
Expand All @@ -40,6 +41,7 @@ public void defaults(RecipeSpec spec) {
spec.recipe(new AutoFormat());
}

@DocumentExample
@Test
void keepMaximumBetweenHeaderAndPackage() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin.format;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaIsoVisitor;
Expand Down Expand Up @@ -46,6 +47,7 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
);
}

@DocumentExample
@Test
void classDeclaration() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin.format;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.Tree;
import org.openrewrite.kotlin.KotlinParser;
Expand Down Expand Up @@ -47,6 +48,7 @@ private static Consumer<RecipeSpec> tabsAndIndents(UnaryOperator<org.openrewrite
)));
}

@DocumentExample
@Test
void mixedToTabs() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static org.openrewrite.kotlin.Assertions.kotlin;
import static org.openrewrite.test.RewriteTest.toRecipe;

public class RemoveTrailingWhitespaceTest implements RewriteTest {
class RemoveTrailingWhitespaceTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,8 @@ public fun methodTwo(value: Int): Int {
@Test
void useContinuationIndentExtendsOnNewLine() {
rewriteRun(
kotlin("""
kotlin(
"""
package org.a
open class A {}
Expand Down Expand Up @@ -2424,7 +2425,8 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ct
return autoFormat(a, ctx);
}
})).parser(KotlinParser.builder().logCompilationWarningsAndErrors(true)),
kotlin("""
kotlin(
"""
package org.sample
@SafeVarargs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin.format;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.kotlin.style.IntelliJ;
import org.openrewrite.kotlin.style.OtherStyle;
import org.openrewrite.test.RecipeSpec;
Expand All @@ -38,6 +39,7 @@ private static Consumer<RecipeSpec> trailingCommaStyle(UnaryOperator<OtherStyle>
return spec -> spec.recipe(toRecipe(() -> new TrailingCommaVisitor<>(with.apply(IntelliJ.other()).getUseTrailingComma())));
}

@DocumentExample
@Test
void classPropertiesWithTrailingCommaOff() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin.format;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.Tree;
import org.openrewrite.kotlin.KotlinParser;
Expand Down Expand Up @@ -77,6 +78,7 @@ class A (
);
}

@DocumentExample
@SuppressWarnings({"ClassInitializerMayBeStatic", "ReassignedVariable", "UnusedAssignment"})
@Test
void blockLevelStatements() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ sealed interface InvalidField {
@Test
void init() {
rewriteRun(
kotlin("""
kotlin(
"""
class Test {
init {
println ( "Hello, world!" )
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/openrewrite/kotlin/tree/ContinueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ void continueFromWhileLoop() {
rewriteRun(
kotlin(
"""
class Test {
fun test ( ) {
while ( true ) continue
}
}
class Test {
fun test ( ) {
while ( true ) continue
}
}
"""
)
);
Expand Down
Loading

0 comments on commit b91e4ab

Please sign in to comment.