From 81942449d38cdb95c671628cd707fff884b2fa59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20G=C3=BCnd=C3=BCz?= Date: Sat, 7 Nov 2020 21:47:38 +0100 Subject: [PATCH] Adjust --strict to treat warnings as errors instead of only altering exit code (#3372) --- CHANGELOG.md | 13 +++--- .../swiftlint/Commands/AnalyzeCommand.swift | 2 +- Source/swiftlint/Commands/LintCommand.swift | 2 +- .../Helpers/LintOrAnalyzeCommand.swift | 43 +++++++++++-------- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7111c67191..64206f9537 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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) @@ -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) @@ -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) diff --git a/Source/swiftlint/Commands/AnalyzeCommand.swift b/Source/swiftlint/Commands/AnalyzeCommand.swift index 92c63c693e..a93d6652c9 100644 --- a/Source/swiftlint/Commands/AnalyzeCommand.swift +++ b/Source/swiftlint/Commands/AnalyzeCommand.swift @@ -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, diff --git a/Source/swiftlint/Commands/LintCommand.swift b/Source/swiftlint/Commands/LintCommand.swift index 680d8ad15f..2ea502efe6 100644 --- a/Source/swiftlint/Commands/LintCommand.swift +++ b/Source/swiftlint/Commands/LintCommand.swift @@ -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, diff --git a/Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift b/Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift index 0d0a5393a0..2907abb34a 100644 --- a/Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift +++ b/Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift @@ -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" : "" @@ -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.") } } }