Skip to content

Commit b1af2c3

Browse files
JAVA-9433 > added ktlint working rule.
1 parent 47dfdcf commit b1af2c3

File tree

7 files changed

+167
-28
lines changed

7 files changed

+167
-28
lines changed

Diff for: ktlint-custom/pom.xml

+70-5
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,87 @@
99
<groupId>com.baeldung</groupId>
1010
<artifactId>ktlint-custom</artifactId>
1111

12+
<packaging>jar</packaging>
13+
1214
<dependencies>
1315

14-
<!-- https://mvnrepository.com/artifact/com.pinterest.ktlint/ktlint-core -->
1516
<dependency>
1617
<groupId>com.pinterest.ktlint</groupId>
17-
<artifactId>ktlint-core</artifactId>
18-
<version>0.43.2</version>
18+
<artifactId>ktlint-rule-engine-core</artifactId>
19+
<version>1.0.1</version>
1920
</dependency>
2021

21-
<!-- https://mvnrepository.com/artifact/com.pinterest.ktlint/ktlint-cli-reporter-core -->
2222
<dependency>
2323
<groupId>com.pinterest.ktlint</groupId>
2424
<artifactId>ktlint-cli-ruleset-core</artifactId>
2525
<version>1.0.1</version>
26-
<scope>runtime</scope>
2726
</dependency>
2827

2928
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.jetbrains.kotlin</groupId>
34+
<artifactId>kotlin-maven-plugin</artifactId>
35+
<version>${kotlin.version}</version>
36+
<executions>
37+
<execution>
38+
<id>compile</id>
39+
<goals>
40+
<goal>compile</goal>
41+
</goals>
42+
<configuration>
43+
<sourceDirs>
44+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
45+
<sourceDir>${project.basedir}/src/main/java</sourceDir>
46+
</sourceDirs>
47+
<jvmTarget>${java.version}</jvmTarget>
48+
<args>
49+
<arg>-Xjvm-default=all-compatibility</arg>
50+
</args>
51+
</configuration>
52+
</execution>
53+
<execution>
54+
<id>test-compile</id>
55+
<goals>
56+
<goal>test-compile</goal>
57+
</goals>
58+
<configuration>
59+
<sourceDirs>
60+
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
61+
<sourceDir>${project.basedir}/src/test/java</sourceDir>
62+
</sourceDirs>
63+
<jvmTarget>${java.version}</jvmTarget>
64+
</configuration>
65+
</execution>
66+
</executions>
67+
<configuration>
68+
<compilerPlugins>
69+
<plugin>spring</plugin>
70+
</compilerPlugins>
71+
</configuration>
72+
<dependencies>
73+
<dependency>
74+
<groupId>org.jetbrains.kotlin</groupId>
75+
<artifactId>kotlin-maven-allopen</artifactId>
76+
<version>${kotlin.version}</version>
77+
</dependency>
78+
</dependencies>
79+
</plugin>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-compiler-plugin</artifactId>
83+
<configuration>
84+
<source>8</source>
85+
<target>8</target>
86+
</configuration>
87+
</plugin>
88+
</plugins>
89+
</build>
90+
91+
<properties>
92+
<kotlin.version>1.9.10</kotlin.version>
93+
<java.version>11</java.version>
94+
</properties>
3095
</project>

Diff for: ktlint-custom/src/main/kotlin/CustomRuleSetProvider.kt

-7
This file was deleted.

Diff for: ktlint-custom/src/main/kotlin/TempRule.kt

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.ktlintcustomrule
2+
3+
import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3
4+
import com.pinterest.ktlint.rule.engine.core.api.RuleProvider
5+
import com.pinterest.ktlint.rule.engine.core.api.RuleSetId
6+
7+
internal const val CUSTOM_RULE_SET_ID = "custom-rule-set-id"
8+
class CustomRuleSetProvider : RuleSetProviderV3(RuleSetId(CUSTOM_RULE_SET_ID)) {
9+
override fun getRuleProviders(): Set<RuleProvider> =
10+
setOf(
11+
RuleProvider { TestNamingRule() },
12+
)
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.ktlintcustomrule
2+
3+
import com.pinterest.ktlint.rule.engine.core.api.ElementType
4+
import com.pinterest.ktlint.rule.engine.core.api.Rule
5+
import com.pinterest.ktlint.rule.engine.core.api.RuleId
6+
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
7+
8+
class TestNamingRule :
9+
Rule(
10+
ruleId = RuleId("$CUSTOM_RULE_SET_ID:unit-test-convention"),
11+
about = About(maintainer = "Gaetano Piazzolla"),
12+
) {
13+
14+
override fun beforeVisitChildNodes(
15+
node: ASTNode,
16+
autoCorrect: Boolean,
17+
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
18+
) {
19+
node
20+
.takeIf { node.elementType == ElementType.CLASS }
21+
?.findChildByType(ElementType.IDENTIFIER)
22+
?.takeIf { it.isTest() && !it.isValidName(allowedEndings) }
23+
?.let {
24+
emit(it.startOffset, "Unit test class names need to end in 'UnitTest', integration tests with 'IntegrationTest', etc", false)
25+
}
26+
}
27+
28+
private fun ASTNode.isTest() = text.endsWith("Test")
29+
private fun ASTNode.isValidName(endings: List<String>): Boolean {
30+
for (ending in endings) {
31+
if (text.endsWith(ending, false)) return true
32+
}
33+
return false
34+
}
35+
36+
private companion object {
37+
val allowedEndings: List<String> = mutableListOf(
38+
"IntegrationTest",
39+
"IntTest",
40+
"ManualTest",
41+
"JdbcTest",
42+
"LiveTest",
43+
"UnitTest",
44+
"jmhTest"
45+
)
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.baeldung.ktlintcustomrule.CustomRuleSetProvider

Diff for: pom.xml

+35-4
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,55 @@
129129
<build>
130130
<plugins>
131131
<plugin>
132-
<groupId>com.github.z3d1k</groupId>
133-
<artifactId>ktlint-maven-plugin</artifactId>
134-
<version>0.10.0</version>
132+
<groupId>org.apache.maven.plugins</groupId>
133+
<artifactId>maven-antrun-plugin</artifactId>
134+
<version>3.1.0</version>
135135
<executions>
136136
<execution>
137+
<id>ktlint</id>
138+
<phase>verify</phase>
139+
<configuration>
140+
<target name="ktlint">
141+
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="false"
142+
classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
143+
<arg value="**/*.kt"/>
144+
</java>
145+
</target>
146+
</configuration>
147+
<goals>
148+
<goal>run</goal>
149+
</goals>
150+
</execution>
151+
<execution>
152+
<id>ktlint-format</id>
153+
<configuration>
154+
<target name="ktlint">
155+
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="false"
156+
classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
157+
<arg value="-F"/>
158+
<arg value="**/*.kt"/>
159+
</java>
160+
</target>
161+
</configuration>
137162
<goals>
138-
<goal>lint</goal>
163+
<goal>run</goal>
139164
</goals>
140165
</execution>
141166
</executions>
142167
<dependencies>
168+
<dependency>
169+
<groupId>com.pinterest.ktlint</groupId>
170+
<artifactId>ktlint-cli</artifactId>
171+
<version>1.0.1</version>
172+
</dependency>
143173
<dependency>
144174
<groupId>com.baeldung</groupId>
145175
<artifactId>ktlint-custom</artifactId>
146176
<version>1.0.0-SNAPSHOT</version>
147177
</dependency>
148178
</dependencies>
149179
</plugin>
180+
150181
<plugin>
151182
<groupId>org.jetbrains.kotlin</groupId>
152183
<artifactId>kotlin-maven-plugin</artifactId>

0 commit comments

Comments
 (0)