Skip to content

Commit

Permalink
Inform user about using --format option of KtLint CLI when finding …
Browse files Browse the repository at this point in the history
…a violation that can be autocorrected (#2091)

Closes #1071
  • Loading branch information
paul-dingemans authored Jun 26, 2023
1 parent 4b8e514 commit 9754f5f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -462,6 +473,8 @@ internal class KtlintCommandLine {
reporter.after(relativeRoute)
}

private fun List<KtlintCliError>.containsErrorThatCanBeAutocorrected() = any { it.status == LINT_CAN_BE_AUTOCORRECTED }

private fun process(
ktLintRuleEngine: KtLintRuleEngine,
code: Code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9754f5f

Please sign in to comment.