From 9754f5fca464f6c9ff7b829abaf5369bcc73d43a Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Mon, 26 Jun 2023 21:24:45 +0200 Subject: [PATCH] Inform user about using `--format` option of KtLint CLI when finding a violation that can be autocorrected (#2091) Closes #1071 --- CHANGELOG.md | 2 +- .../ktlint/cli/internal/KtlintCommandLine.kt | 13 +++++++++++++ .../com/pinterest/ktlint/cli/SimpleCLITest.kt | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d644b7648..435c78789e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ At this point in time, it is not yet decided what the next steps will be. Ktlint * Add new experimental rule `statement-wrapping` which ensures function, class, or other blocks statement body doesn't start or end at starting or ending braces of the block ([#1938](https://github.com/pinterest/ktlint/issues/1938)) * Add new experimental rule `blank-line-before-declaration`. This rule requires a blank line before class, function or property declarations ([#1939](https://github.com/pinterest/ktlint/issues/1939)) * Wrap multiple statements on same line `wrapping` ([#1078](https://github.com/pinterest/ktlint/issues/1078)) - +* Inform user about using `--format` option of KtLint CLI when finding a violation that can be autocorrected ([#1071](https://github.com/pinterest/ktlint/issues/1071)) ### Removed diff --git a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt index b495705595..bafc680744 100644 --- a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt +++ b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt @@ -242,6 +242,7 @@ internal class KtlintCommandLine { private val tripped = AtomicBoolean() private val fileNumber = AtomicInteger() private val errorNumber = AtomicInteger() + private val adviseToUseFormat = AtomicBoolean() internal var debug: Boolean = false get() = Level.DEBUG.isGreaterOrEqual(minLogLevel) @@ -329,6 +330,13 @@ internal class KtlintCommandLine { baseline.lintErrorsPerFile, aggregatedReporter, ) + if (adviseToUseFormat.get()) { + if (format) { + logger.error { "Format was not able to autocorrect all errors that theoretically can be autocorrected." } + } else { + logger.warn { "Lint has found errors than can be autocorrected using 'ktlint --format'" } + } + } } aggregatedReporter.afterAll() @@ -454,6 +462,9 @@ internal class KtlintCommandLine { fileNumber.incrementAndGet() val errListLimit = minOf(ktlintCliErrors.size, maxOf(limit - errorNumber.get(), 0)) errorNumber.addAndGet(errListLimit) + if (!adviseToUseFormat.get() && ktlintCliErrors.containsErrorThatCanBeAutocorrected()) { + adviseToUseFormat.set(true) + } reporter.before(relativeRoute) ktlintCliErrors @@ -462,6 +473,8 @@ internal class KtlintCommandLine { reporter.after(relativeRoute) } + private fun List.containsErrorThatCanBeAutocorrected() = any { it.status == LINT_CAN_BE_AUTOCORRECTED } + private fun process( ktLintRuleEngine: KtLintRuleEngine, code: Code, diff --git a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt index 9e69ce8983..a1e2a2e8e3 100644 --- a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt +++ b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt @@ -78,6 +78,24 @@ class SimpleCLITest { } } + @Test + fun `Given some code with an error then return from lint with the error exit code and warning to use --format`( + @TempDir + tempDir: Path, + ) { + CommandLineTestRunner(tempDir) + .run( + "too-many-empty-lines", + listOf("**/*.test"), + ) { + SoftAssertions().apply { + assertErrorExitCode() + assertThat(normalOutput) + .containsLineMatching(Regex(".* WARN .* Lint has found errors than can be autocorrected using 'ktlint --format'")) + }.assertAll() + } + } + @Test fun `Given some code with an error but a glob which does not select the file`( @TempDir