Skip to content

Commit

Permalink
Added lenient command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
mildm8nnered committed Sep 20, 2024
1 parent e9386ae commit fa45a8e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,9 @@ allow_zero_lintable_files: false
# If true, SwiftLint will treat all warnings as errors.
strict: false
# If true, SwiftLint will treat all errors as warnings.
lenient: false
# The path to a baseline file, which will be used to filter out detected violations.
baseline: Baseline.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extension Configuration {
cachePath: cachePath,
allowZeroLintableFiles: childConfiguration.allowZeroLintableFiles,
strict: childConfiguration.strict,
lenient: childConfiguration.lenient,
baseline: childConfiguration.baseline,
writeBaseline: childConfiguration.writeBaseline,
checkForUpdates: childConfiguration.checkForUpdates
Expand Down
2 changes: 2 additions & 0 deletions Source/SwiftLintCore/Extensions/Configuration+Parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extension Configuration {
case analyzerRules = "analyzer_rules"
case allowZeroLintableFiles = "allow_zero_lintable_files"
case strict = "strict"
case lenient = "lenient"
case baseline = "baseline"
case writeBaseline = "write_baseline"
case checkForUpdates = "check_for_updates"
Expand Down Expand Up @@ -103,6 +104,7 @@ extension Configuration {
pinnedVersion: dict[Key.swiftlintVersion.rawValue].map { ($0 as? String) ?? String(describing: $0) },
allowZeroLintableFiles: dict[Key.allowZeroLintableFiles.rawValue] as? Bool ?? false,
strict: dict[Key.strict.rawValue] as? Bool ?? false,
lenient: dict[Key.lenient.rawValue] as? Bool ?? false,
baseline: dict[Key.baseline.rawValue] as? String,
writeBaseline: dict[Key.writeBaseline.rawValue] as? String,
checkForUpdates: dict[Key.checkForUpdates.rawValue] as? Bool ?? false
Expand Down
11 changes: 11 additions & 0 deletions Source/SwiftLintCore/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public struct Configuration {
/// Treat warnings as errors.
public let strict: Bool

/// Treat errors as warnings.
public let lenient: Bool

/// The path to read a baseline from.
public let baseline: String?

Expand Down Expand Up @@ -83,6 +86,7 @@ public struct Configuration {
cachePath: String?,
allowZeroLintableFiles: Bool,
strict: Bool,
lenient: Bool,
baseline: String?,
writeBaseline: String?,
checkForUpdates: Bool
Expand All @@ -97,6 +101,7 @@ public struct Configuration {
self.cachePath = cachePath
self.allowZeroLintableFiles = allowZeroLintableFiles
self.strict = strict
self.lenient = lenient
self.baseline = baseline
self.writeBaseline = writeBaseline
self.checkForUpdates = checkForUpdates
Expand All @@ -117,6 +122,7 @@ public struct Configuration {
cachePath = configuration.cachePath
allowZeroLintableFiles = configuration.allowZeroLintableFiles
strict = configuration.strict
lenient = configuration.lenient
baseline = configuration.baseline
writeBaseline = configuration.writeBaseline
checkForUpdates = configuration.checkForUpdates
Expand All @@ -143,6 +149,7 @@ public struct Configuration {
/// - parameter allowZeroLintableFiles: Allow SwiftLint to exit successfully when passed ignored or unlintable
/// files.
/// - parameter strict: Treat warnings as errors.
/// - parameter lenient: Treat errors as warnings.
/// - parameter baseline: The path to read a baseline from.
/// - parameter writeBaseline: The path to write a baseline to.
/// - parameter checkForUpdates: Check for updates to SwiftLint.
Expand All @@ -160,6 +167,7 @@ public struct Configuration {
pinnedVersion: String? = nil,
allowZeroLintableFiles: Bool = false,
strict: Bool = false,
lenient: Bool = false,
baseline: String? = nil,
writeBaseline: String? = nil,
checkForUpdates: Bool = false
Expand Down Expand Up @@ -189,6 +197,7 @@ public struct Configuration {
cachePath: cachePath,
allowZeroLintableFiles: allowZeroLintableFiles,
strict: strict,
lenient: lenient,
baseline: baseline,
writeBaseline: writeBaseline,
checkForUpdates: checkForUpdates
Expand Down Expand Up @@ -298,6 +307,7 @@ extension Configuration: Hashable {
hasher.combine(reporter)
hasher.combine(allowZeroLintableFiles)
hasher.combine(strict)
hasher.combine(lenient)
hasher.combine(baseline)
hasher.combine(writeBaseline)
hasher.combine(checkForUpdates)
Expand All @@ -319,6 +329,7 @@ extension Configuration: Hashable {
lhs.fileGraph == rhs.fileGraph &&
lhs.allowZeroLintableFiles == rhs.allowZeroLintableFiles &&
lhs.strict == rhs.strict &&
lhs.lenient == rhs.lenient &&
lhs.baseline == rhs.baseline &&
lhs.writeBaseline == rhs.writeBaseline &&
lhs.checkForUpdates == rhs.checkForUpdates &&
Expand Down
9 changes: 7 additions & 2 deletions Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct LintOrAnalyzeCommand {
currentViolations = applyLeniency(
options: options,
strict: builder.configuration.strict,
lenient: builder.configuration.lenient,
violations: violationsBeforeLeniency
)
visitorMutationQueue.sync {
Expand All @@ -81,6 +82,7 @@ struct LintOrAnalyzeCommand {
currentViolations = applyLeniency(
options: options,
strict: builder.configuration.strict,
lenient: builder.configuration.lenient,
violations: linter.styleViolations(using: builder.storage)
)
}
Expand Down Expand Up @@ -175,11 +177,14 @@ struct LintOrAnalyzeCommand {
private static func applyLeniency(
options: LintOrAnalyzeOptions,
strict: Bool,
lenient: Bool,
violations: [StyleViolation]
) -> [StyleViolation] {
// config file settings can be overridden by either `--strict` or `--lenient` command line options
let strict = (strict && !options.lenient) || options.strict
let lenient = (lenient && !options.strict) || options.lenient

switch (options.lenient, strict) {
switch (lenient, strict) {
case (false, false):
return violations

Expand All @@ -200,7 +205,7 @@ struct LintOrAnalyzeCommand {
}

case (true, true):
queuedFatalError("Invalid command line options: 'lenient' and 'strict' are mutually exclusive.")
queuedFatalError("Invalid command line or config options: 'lenient' and 'strict' are mutually exclusive.")
}
}

Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftLintFrameworkTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ final class ConfigurationTests: SwiftLintTestCase {
XCTAssertEqual(reporterFrom(identifier: config.reporter).identifier, "xcode")
XCTAssertFalse(config.allowZeroLintableFiles)
XCTAssertFalse(config.strict)
XCTAssertFalse(config.lenient)
XCTAssertNil(config.baseline)
XCTAssertNil(config.writeBaseline)
XCTAssertFalse(config.checkForUpdates)
Expand Down Expand Up @@ -447,6 +448,11 @@ final class ConfigurationTests: SwiftLintTestCase {
XCTAssertTrue(configuration.strict)
}

func testLenient() throws {
let configuration = try Configuration(dict: ["lenient": true])
XCTAssertTrue(configuration.lenient)
}

func testBaseline() throws {
let baselinePath = "Baseline.json"
let configuration = try Configuration(dict: ["baseline": baselinePath])
Expand Down

0 comments on commit fa45a8e

Please sign in to comment.