Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust --strict to treat warnings as errors instead of only altering exit code #3372

Merged
merged 3 commits into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#### Breaking

* None.
* Changed behavior of `strict` option on `lint` and `analyze` to treat
all warnings as errors instead of only changing the exit code.
[Jeehut](https://github.com/Jeehut)
[#3312](https://github.com/realm/SwiftLint/issues/3312)

#### Experimental

Expand Down Expand Up @@ -200,11 +203,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#3225](https://github.com/realm/SwiftLint/issues/3225)

* Fix some cases where the output would be incomplete when running
* Fix some cases where the output would be incomplete when running
SwiftLint on Linux.
[Marcelo Fabri](https://github.com/marcelofabri)
[#3214](https://github.com/realm/SwiftLint/issues/3214)

* `compiler_protocol_init` now triggers on `IndexSet(arrayLiteral:)`.
[Janak Shah](https://github.com/janakshah)
[#3284](https://github.com/realm/SwiftLint/pull/3284)
Expand Down Expand Up @@ -244,7 +247,7 @@ This is the last release to support building with Swift 5.0.x.
[Marcelo Fabri](https://github.com/marcelofabri)
[#3149](https://github.com/realm/SwiftLint/issues/3149)

* Fix false positives in `redundant_objc_attribute` rule in extensions when
* Fix false positives in `redundant_objc_attribute` rule in extensions when
using Swift 5.2.
[Marcelo Fabri](https://github.com/marcelofabri)

Expand Down Expand Up @@ -371,7 +374,7 @@ This is the last release to support building with Swift 5.0.x.
* Fix false positives when line ends with carriage return + line feed.
[John Mueller](https://github.com/john-mueller)
[#3060](https://github.com/realm/SwiftLint/issues/3060)

* Implicit_return description now reports current config correctly.
[John Buckley](https://github.com/nhojb)

Expand Down
2 changes: 1 addition & 1 deletion Source/swiftlint/Commands/AnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct AnalyzeOptions: OptionsProtocol {
<*> mode <| pathOption(action: "analyze")
<*> mode <| configOption
<*> mode <| Option(key: "strict", defaultValue: false,
usage: "fail on warnings")
usage: "upgrades warnings to serious violations (errors)")
<*> mode <| Option(key: "lenient", defaultValue: false,
usage: "downgrades serious violations to warnings, warning threshold is disabled")
<*> mode <| Option(key: "force-exclude", defaultValue: false,
Expand Down
2 changes: 1 addition & 1 deletion Source/swiftlint/Commands/LintCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct LintOptions: OptionsProtocol {
usage: "lint standard input")
<*> mode <| configOption
<*> mode <| Option(key: "strict", defaultValue: false,
usage: "fail on warnings")
usage: "upgrades warnings to serious violations (errors)")
<*> mode <| Option(key: "lenient", defaultValue: false,
usage: "downgrades serious violations to warnings, warning threshold is disabled")
<*> mode <| Option(key: "force-exclude", defaultValue: false,
Expand Down
43 changes: 24 additions & 19 deletions Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,11 @@ struct LintOrAnalyzeCommand {
ruleBenchmark.save()
}
try? cache?.save()
return successOrExit(numberOfSeriousViolations: numberOfSeriousViolations,
strictWithViolations: options.strict && !violations.isEmpty)
guard numberOfSeriousViolations == 0 else { exit(2) }
return .success(())
}
}

private static func successOrExit(numberOfSeriousViolations: Int,
strictWithViolations: Bool) -> Result<(), CommandantError<()>> {
if numberOfSeriousViolations > 0 {
exit(2)
} else if strictWithViolations {
exit(3)
}
return .success(())
}

private static func printStatus(violations: [StyleViolation], files: [SwiftLintFile], serious: Int, verb: String) {
let pluralSuffix = { (collection: [Any]) -> String in
return collection.count != 1 ? "s" : ""
Expand Down Expand Up @@ -109,15 +99,30 @@ struct LintOrAnalyzeCommand {
}

private static func applyLeniency(options: LintOrAnalyzeOptions, violations: [StyleViolation]) -> [StyleViolation] {
if !options.lenient {
switch (options.lenient, options.strict) {
case (false, false):
return violations
}
return violations.map {
if $0.severity == .error {
return $0.with(severity: .warning)
} else {
return $0

case (true, false):
return violations.map {
if $0.severity == .error {
return $0.with(severity: .warning)
} else {
return $0
}
}

case (false, true):
return violations.map {
if $0.severity == .warning {
return $0.with(severity: .error)
} else {
return $0
}
}

case (true, true):
queuedFatalError("Invalid command line options: 'lenient' and 'strict' are mutually exclusive.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

}
}
}
Expand Down