Skip to content

Commit ddf0c03

Browse files
authored
Merge pull request #892 from square/rick/ktlint_kotlinter
update KtLint to 0.47.1 and switch KtLint-gradle to Kotlinter
2 parents bedebdc + 5747676 commit ddf0c03

File tree

126 files changed

+705
-364
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+705
-364
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ ij_groovy_while_on_new_line = false
484484
ij_groovy_wrap_long_lines = false
485485

486486
[{*.gradle.kts,*.kt,*.kts,*.main.kts}]
487+
# noinspection EditorConfigKeyCorrectness
488+
ktlint_disabled_rules = trailing-comma-on-declaration-site,trailing-comma-on-call-site
487489
ij_continuation_indent_size = 2
488490
ij_kotlin_align_in_columns_case_branch = false
489491
ij_kotlin_align_multiline_binary_operation = false

.github/workflows/kotlin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ jobs :
9090
name : Check with Gradle
9191
with :
9292
arguments : |
93-
allTests test apiCheck checkVersionIsSnapshot dependencyGuard lint ktlintCheck jvmWorkflowNodeBenchmarkJar --stacktrace --continue
93+
allTests test apiCheck checkVersionIsSnapshot dependencyGuard lint lintKotlin jvmWorkflowNodeBenchmarkJar --stacktrace --continue
9494
cache-read-only: false
9595

9696
# Report as Github Pull Request Check.

benchmarks/performance-poetry/complex-poetry/src/main/java/com/squareup/benchmarks/performance/complex/poetry/PerformancePoemWorkflow.kt

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class PerformancePoemWorkflow(
6464

6565
sealed class State {
6666
val isLoading: Boolean = false
67+
6768
// N.B. This state is a smell. We include it to be able to mimic smells
6869
// we encounter in real life. Best practice would be to fold it
6970
// into [Selected(NO_SELECTED_STANZA)] at the very least.
@@ -85,9 +86,13 @@ class PerformancePoemWorkflow(
8586
props: Poem,
8687
snapshot: Snapshot?
8788
): State {
88-
return if (simulatedPerfConfig.useInitializingState) Initializing else Selected(
89-
NO_SELECTED_STANZA
90-
)
89+
return if (simulatedPerfConfig.useInitializingState) {
90+
Initializing
91+
} else {
92+
Selected(
93+
NO_SELECTED_STANZA
94+
)
95+
}
9196
}
9297

9398
@OptIn(WorkflowUiExperimentalApi::class)
@@ -174,23 +179,26 @@ class PerformancePoemWorkflow(
174179
}
175180

176181
val previousStanzas: List<StanzaScreen> =
177-
if (stanzaIndex == NO_SELECTED_STANZA) emptyList()
178-
else renderProps.stanzas.subList(0, stanzaIndex)
179-
.mapIndexed { index, _ ->
180-
context.renderChild(
181-
StanzaWorkflow,
182-
Props(
183-
poem = renderProps,
184-
index = index,
185-
eventHandlerTag = ActionHandlingTracingInterceptor::keyForTrace
186-
),
187-
key = "$index"
188-
) {
189-
noAction()
182+
if (stanzaIndex == NO_SELECTED_STANZA) {
183+
emptyList()
184+
} else {
185+
renderProps.stanzas.subList(0, stanzaIndex)
186+
.mapIndexed { index, _ ->
187+
context.renderChild(
188+
StanzaWorkflow,
189+
Props(
190+
poem = renderProps,
191+
index = index,
192+
eventHandlerTag = ActionHandlingTracingInterceptor::keyForTrace
193+
),
194+
key = "$index"
195+
) {
196+
noAction()
197+
}
198+
}.map { originalStanzaScreen ->
199+
originalStanzaScreen
190200
}
191-
}.map { originalStanzaScreen ->
192-
originalStanzaScreen
193-
}
201+
}
194202

195203
val visibleStanza =
196204
if (stanzaIndex == NO_SELECTED_STANZA) {

benchmarks/performance-poetry/complex-poetry/src/main/java/com/squareup/benchmarks/performance/complex/poetry/PerformancePoemsBrowserWorkflow.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ class PerformancePoemsBrowserWorkflow(
102102
// Don't try this at home.
103103
is Initializing -> {
104104
context.runningWorker(TraceableWorker.from("BrowserInitializing") { Unit }, "init") {
105-
isLoading.value = true
106-
action {
107-
isLoading.value = false
108-
state = NoSelection
105+
isLoading.value = true
106+
action {
107+
isLoading.value = false
108+
state = NoSelection
109+
}
109110
}
110-
}
111111
return OverviewDetailScreen(overviewRendering = BackStackScreen(BlankScreen))
112112
}
113113
is NoSelection -> {

benchmarks/performance-poetry/complex-poetry/src/main/java/com/squareup/benchmarks/performance/complex/poetry/PerformancePoetryActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ class PoetryModel(
261261
interceptor: WorkflowInterceptor?,
262262
runtimeConfig: RuntimeConfig
263263
) : ViewModel() {
264-
@OptIn(WorkflowUiExperimentalApi::class) val renderings: StateFlow<Screen> by lazy {
264+
@OptIn(WorkflowUiExperimentalApi::class)
265+
val renderings: StateFlow<Screen> by lazy {
265266
renderWorkflowIn(
266267
workflow = workflow,
267268
scope = viewModelScope,

build.gradle.kts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
import com.squareup.workflow1.buildsrc.applyKtLint
12
import org.jetbrains.dokka.gradle.DokkaTask
2-
import org.jlleitschuh.gradle.ktlint.KtlintExtension
3-
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
43

54
buildscript {
65
dependencies {
@@ -11,7 +10,6 @@ buildscript {
1110
classpath(libs.kotlinx.binaryCompatibility.gradle.plugin)
1211
classpath(libs.kotlin.gradle.plugin)
1312
classpath(libs.google.ksp)
14-
classpath(libs.ktlint.gradle)
1513
classpath(libs.vanniktech.publish)
1614
}
1715

@@ -32,26 +30,17 @@ plugins {
3230

3331
subprojects {
3432

35-
apply(plugin = "org.jlleitschuh.gradle.ktlint")
3633
afterEvaluate {
3734
configurations.configureEach {
3835
// There could be transitive dependencies in tests with a lower version. This could cause
3936
// problems with a newer Kotlin version that we use.
4037
resolutionStrategy.force(libs.kotlin.reflect)
4138
}
4239
}
43-
44-
// Configuration documentation: https://github.com/JLLeitschuh/ktlint-gradle#configuration
45-
configure<KtlintExtension> {
46-
// Prints the name of failed rules.
47-
verbose.set(true)
48-
reporters {
49-
// Default "plain" reporter is actually harder to read.
50-
reporter(ReporterType.JSON)
51-
}
52-
}
5340
}
5441

42+
applyKtLint()
43+
5544
apply(from = rootProject.file(".buildscript/binary-validation.gradle"))
5645

5746
// This plugin needs to be applied to the root projects for the dokkaGfmCollector task we use to

buildSrc/build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@Suppress("UnstableApiUsage")
1+
@Suppress("DSL_SCOPE_VIOLATION")
22
plugins {
33
`kotlin-dsl`
44
alias(libs.plugins.google.ksp)
@@ -7,6 +7,7 @@ plugins {
77
repositories {
88
mavenCentral()
99
google()
10+
maven("https://plugins.gradle.org/m2/")
1011
}
1112

1213
dependencies {
@@ -19,6 +20,8 @@ dependencies {
1920
implementation(libs.dokka.gradle.plugin)
2021
implementation(libs.dropbox.dependencyGuard)
2122
implementation(libs.kotlin.gradle.plugin)
23+
implementation(libs.ktlint.core)
24+
implementation(libs.kotlinter)
2225
implementation(libs.squareup.moshi)
2326
implementation(libs.squareup.moshi.adapters)
2427
implementation(libs.vanniktech.publish)

buildSrc/src/main/java/com/squareup/workflow1/buildsrc/KotlinCommonSettings.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ fun Project.kotlinCommonSettings(
1515
bomConfigurationName: String
1616
) {
1717

18+
applyKtLint()
19+
1820
// force the same Kotlin version everywhere, including transitive dependencies
1921
dependencies {
2022
bomConfigurationName(platform(kotlin("bom")))
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.squareup.workflow1.buildsrc
2+
3+
import org.gradle.api.Project
4+
import org.gradle.api.tasks.TaskContainer
5+
import org.jmailen.gradle.kotlinter.tasks.ConfigurableKtLintTask
6+
import org.jmailen.gradle.kotlinter.tasks.FormatTask
7+
import org.jmailen.gradle.kotlinter.tasks.LintTask
8+
9+
/**
10+
* Applies Kotlinter settings to any project, with additional tasks for the root project.
11+
*
12+
* @since 0.10.0
13+
*/
14+
fun Project.applyKtLint() {
15+
pluginManager.apply("org.jmailen.kotlinter")
16+
17+
tasks.withType(LintTask::class.java) {
18+
ignoreFailures.set(false)
19+
mustRunAfter(tasks.withType(FormatTask::class.java))
20+
}
21+
22+
if (project == rootProject) {
23+
tasks.register("lintKotlin")
24+
tasks.register("formatKotlin")
25+
afterEvaluate {
26+
addGradleScriptTasks(tasks, taskNameQualifier = "")
27+
}
28+
}
29+
30+
// dummy ktlint-gradle plugin task names which just delegate to the Kotlinter ones
31+
tasks.register("ktlintCheck") {
32+
dependsOn(tasks.withType(LintTask::class.java))
33+
}
34+
tasks.register("ktlintFormat") {
35+
dependsOn(tasks.withType(FormatTask::class.java))
36+
}
37+
}
38+
39+
// Add check/format tasks to each root project (including child root projects) which target every
40+
// `build.gradle.kts` and `settings.gradle.kts` file within that project group.
41+
private fun Project.addGradleScriptTasks(
42+
taskContainer: TaskContainer,
43+
taskNameQualifier: String = ""
44+
) {
45+
val includedProjectScriptFiles = allprojects
46+
.flatMap { included ->
47+
listOfNotNull(
48+
included.buildFile,
49+
included.file("settings.gradle.kts").takeIf { it.exists() }
50+
)
51+
}
52+
53+
val lintKotlinBuildLogic = taskContainer.register(
54+
"lintKotlin${taskNameQualifier}BuildScripts",
55+
LintTask::class.java
56+
) {
57+
group = "Formatting"
58+
description = "Runs lint on the build and settings files"
59+
source(includedProjectScriptFiles)
60+
}
61+
62+
tasks.named("lintKotlin") {
63+
dependsOn(lintKotlinBuildLogic)
64+
}
65+
66+
val formatKotlinBuildLogic = taskContainer.register(
67+
"formatKotlin${taskNameQualifier}BuildScripts",
68+
FormatTask::class.java
69+
) {
70+
group = "Formatting"
71+
description = "Formats the build and settings files"
72+
source(includedProjectScriptFiles)
73+
}
74+
tasks.named("formatKotlin") {
75+
dependsOn(formatKotlinBuildLogic)
76+
}
77+
}

dependencies/classpath.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.5.0
166166
org.jetbrains:annotations:13.0
167167
org.jetbrains:markdown-jvm:0.2.4
168168
org.jetbrains:markdown:0.2.4
169-
org.jlleitschuh.gradle:ktlint-gradle:10.3.0
170169
org.json:json:20180813
171170
org.jsoup:jsoup:1.13.1
172171
org.jvnet.staxex:stax-ex:1.8.1

0 commit comments

Comments
 (0)