Skip to content

Commit

Permalink
Adjust --strict to treat warnings as errors instead of only altering …
Browse files Browse the repository at this point in the history
…exit code (#3372)
  • Loading branch information
Jeehut authored Nov 7, 2020
1 parent b005920 commit 8194244
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
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 @@ -235,11 +238,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 @@ -279,7 +282,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 @@ -406,7 +409,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 @@ -70,7 +70,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 @@ -46,7 +46,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.")
}
}
}
Expand Down

0 comments on commit 8194244

Please sign in to comment.