forked from moderneinc/rewrite-recipe-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: style to reorder imports by Jackson code style
- Loading branch information
Showing
2 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/io/github/timoa/misc/JacksonImportStyle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.github.timoa.misc; | ||
|
||
import org.openrewrite.java.style.ImportLayoutStyle; | ||
import org.openrewrite.java.style.TabsAndIndentsStyle; | ||
import org.openrewrite.style.NamedStyles; | ||
|
||
import java.util.Arrays; | ||
|
||
import static java.util.Collections.emptySet; | ||
import static org.openrewrite.Tree.randomId; | ||
|
||
public class JacksonImportStyle extends NamedStyles { | ||
public JacksonImportStyle() { | ||
super(randomId(), | ||
"io.github.timoa.misc.JacksonImportStyle", | ||
"custom style", | ||
"custom style for the Jackson project", | ||
emptySet(), | ||
Arrays.asList( | ||
importLayout(), | ||
//needed so continuation indent, e.g. an `extends` on th next line is not modified. | ||
new TabsAndIndentsStyle(false, -2, -2, -2, false, null | ||
) | ||
|
||
)); | ||
} | ||
|
||
private static ImportLayoutStyle importLayout() { | ||
return ImportLayoutStyle.builder() | ||
.importPackage("java.*") | ||
.blankLine() | ||
.importAllOthers() | ||
.blankLine() | ||
.importPackage("com.fasterxml.jackson.annotation.*") | ||
.blankLine() | ||
.importPackage("com.fasterxml.jackson.core.*") | ||
.importPackage("com.fasterxml.jackson.databind.*") | ||
.importPackage("tools.jackson.*")//needed for master | ||
.blankLine() | ||
.importPackage("com.fasterxml.jackson.other.modules.*") | ||
.blankLine() | ||
.importStaticAllOthers() | ||
.classCountToUseStarImport(Integer.MAX_VALUE)//disable collapsing imports to * | ||
.build(); | ||
} | ||
|
||
} |
157 changes: 157 additions & 0 deletions
157
src/test/java/io/github/timoa/misc/JacksonImportStyleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package io.github.timoa.misc; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.Issue; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.OrderImports; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class JacksonImportStyleTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new OrderImports(false)); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void simpleExample() { | ||
rewriteRun( | ||
spec -> spec.parser(JavaParser.fromJavaVersion().styles( | ||
singletonList( | ||
new JacksonImportStyle()) | ||
)), | ||
java( | ||
""" | ||
package com.fasterxml.jackson.core.write; | ||
import com.fasterxml.jackson.core.*; | ||
import java.io.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.junit.jupiter.api.Test; | ||
class Test { | ||
} | ||
""", | ||
""" | ||
package com.fasterxml.jackson.core.write; | ||
import java.io.*; | ||
import org.junit.jupiter.api.Test; | ||
import com.fasterxml.jackson.core.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
class Test { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void leaveNoGaps() { | ||
rewriteRun( | ||
spec -> spec.parser(JavaParser.fromJavaVersion().styles( | ||
singletonList( | ||
new JacksonImportStyle()) | ||
)), | ||
java( | ||
""" | ||
package com.fasterxml.jackson.core.write; | ||
import com.fasterxml.jackson.core.*; | ||
import java.io.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import org.junit.jupiter.api.Test; | ||
class GeneratorBasicDerivedTest { | ||
} | ||
""", | ||
""" | ||
package com.fasterxml.jackson.core.write; | ||
import java.io.*; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import org.junit.jupiter.api.Test; | ||
import com.fasterxml.jackson.core.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
class GeneratorBasicDerivedTest { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Issue("https://github.com/openrewrite/rewrite/issues/4165") | ||
@Test | ||
// todo reactivate once the issue above is resolved | ||
void shouldNotIndentExtends() { | ||
rewriteRun( | ||
spec -> spec.parser(JavaParser.fromJavaVersion().styles( | ||
singletonList( | ||
new JacksonImportStyle()) | ||
)), | ||
java("class BaseClass {}"), | ||
java( | ||
""" | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import java.io.*; | ||
class Test | ||
extends BaseClass { | ||
} | ||
""", | ||
""" | ||
import java.io.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
class Test | ||
extends BaseClass { | ||
} | ||
""" | ||
), | ||
java(//different indentation here, just to be sure | ||
""" | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import java.io.*; | ||
class Test2 | ||
extends BaseClass { | ||
} | ||
""", | ||
""" | ||
import java.io.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
class Test2 | ||
extends BaseClass { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |