Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
improvements done in the [SwiftSyntax](https://github.com/apple/swift-syntax)
library.

* Adds `baseline` and `write_baseline` configuration file settings, equivalent
to the `--baseline` and `--write-baseline` command line options.
[Martin Redington](https://github.com/mildm8nnered)
[#5552](https://github.com/realm/SwiftLint/issues/5552)

#### Bug Fixes

* Fix a few false positives and negatives by updating the parser to support
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,12 @@ allow_zero_lintable_files: false
# If true, SwiftLint will treat all warnings as errors.
strict: false

# The path to a baseline file, which will be used to filter out detected violations.
baseline: Baseline.json

# The path to save detected violations to as a new baseline.
write_baseline: Baseline.json

# configurable rules can be customized from this configuration file
# binary rules can set their severity level
force_cast: warning # implicitly
Expand Down
4 changes: 3 additions & 1 deletion Source/SwiftLintCore/Extensions/Configuration+Merging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ extension Configuration {
reporter: reporter,
cachePath: cachePath,
allowZeroLintableFiles: childConfiguration.allowZeroLintableFiles,
strict: childConfiguration.strict
strict: childConfiguration.strict,
baseline: childConfiguration.baseline,
writeBaseline: childConfiguration.writeBaseline
)
}

Expand Down
6 changes: 5 additions & 1 deletion Source/SwiftLintCore/Extensions/Configuration+Parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extension Configuration {
case analyzerRules = "analyzer_rules"
case allowZeroLintableFiles = "allow_zero_lintable_files"
case strict = "strict"
case baseline = "baseline"
case writeBaseline = "write_baseline"
case childConfig = "child_config"
case parentConfig = "parent_config"
case remoteConfigTimeout = "remote_timeout"
Expand Down Expand Up @@ -95,7 +97,9 @@ extension Configuration {
cachePath: cachePath ?? dict[Key.cachePath.rawValue] as? String,
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
strict: dict[Key.strict.rawValue] as? Bool ?? false,
baseline: dict[Key.baseline.rawValue] as? String,
writeBaseline: dict[Key.writeBaseline.rawValue] as? String
)
}

Expand Down
35 changes: 29 additions & 6 deletions Source/SwiftLintCore/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ public struct Configuration {
/// Allow or disallow SwiftLint to exit successfully when passed only ignored or unlintable files.
public let allowZeroLintableFiles: Bool

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

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

/// The path to write a baseline to.
public let writeBaseline: String?

/// This value is `true` iff the `--config` parameter was used to specify (a) configuration file(s)
/// In particular, this means that the value is also `true` if the `--config` parameter
/// was used to explicitly specify the default `.swiftlint.yml` as the configuration file
Expand Down Expand Up @@ -73,7 +79,9 @@ public struct Configuration {
reporter: String?,
cachePath: String?,
allowZeroLintableFiles: Bool,
strict: Bool
strict: Bool,
baseline: String?,
writeBaseline: String?
) {
self.rulesWrapper = rulesWrapper
self.fileGraph = fileGraph
Expand All @@ -85,6 +93,8 @@ public struct Configuration {
self.cachePath = cachePath
self.allowZeroLintableFiles = allowZeroLintableFiles
self.strict = strict
self.baseline = baseline
self.writeBaseline = writeBaseline
}

/// Creates a Configuration by copying an existing configuration.
Expand All @@ -102,6 +112,8 @@ public struct Configuration {
cachePath = configuration.cachePath
allowZeroLintableFiles = configuration.allowZeroLintableFiles
strict = configuration.strict
baseline = configuration.baseline
writeBaseline = configuration.writeBaseline
}

/// Creates a `Configuration` by specifying its properties directly,
Expand All @@ -122,8 +134,11 @@ public struct Configuration {
/// - parameter reporter: The identifier for the `Reporter` to use to report style violations.
/// - parameter cachePath: The location of the persisted cache to use whith this configuration.
/// - parameter pinnedVersion: The SwiftLint version defined in this configuration.
/// - parameter allowZeroLintableFiles: Allow SwiftLint to exit successfully when passed ignored or unlintable files
/// - parameter strict: Treat warnings as errors
/// - parameter allowZeroLintableFiles: Allow SwiftLint to exit successfully when passed ignored or unlintable
/// files.
/// - parameter strict: Treat warnings as errors.
/// - parameter baseline: The path to read a baseline from.
/// - parameter writeBaseline: The path to write a baseline to.
package init(
rulesMode: RulesMode = .default(disabled: [], optIn: []),
allRulesWrapped: [ConfigurationRuleWrapper]? = nil,
Expand All @@ -137,7 +152,9 @@ public struct Configuration {
cachePath: String? = nil,
pinnedVersion: String? = nil,
allowZeroLintableFiles: Bool = false,
strict: Bool = false
strict: Bool = false,
baseline: String? = nil,
writeBaseline: String? = nil
) {
if let pinnedVersion, pinnedVersion != Version.current.value {
queuedPrintError(
Expand All @@ -163,7 +180,9 @@ public struct Configuration {
reporter: reporter,
cachePath: cachePath,
allowZeroLintableFiles: allowZeroLintableFiles,
strict: strict
strict: strict,
baseline: baseline,
writeBaseline: writeBaseline
)
}

Expand Down Expand Up @@ -268,6 +287,8 @@ extension Configuration: Hashable {
hasher.combine(reporter)
hasher.combine(allowZeroLintableFiles)
hasher.combine(strict)
hasher.combine(baseline)
hasher.combine(writeBaseline)
hasher.combine(basedOnCustomConfigurationFiles)
hasher.combine(cachePath)
hasher.combine(rules.map { type(of: $0).description.identifier })
Expand All @@ -286,6 +307,8 @@ extension Configuration: Hashable {
lhs.fileGraph == rhs.fileGraph &&
lhs.allowZeroLintableFiles == rhs.allowZeroLintableFiles &&
lhs.strict == rhs.strict &&
lhs.baseline == rhs.baseline &&
lhs.writeBaseline == rhs.writeBaseline &&
lhs.rulesMode == rhs.rulesMode
}
}
Expand Down
8 changes: 4 additions & 4 deletions Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct LintOrAnalyzeCommand {
private static func lintOrAnalyze(_ options: LintOrAnalyzeOptions) async throws {
let builder = LintOrAnalyzeResultBuilder(options)
let files = try await collectViolations(builder: builder)
if let baselineOutputPath = options.writeBaseline {
if let baselineOutputPath = options.writeBaseline ?? builder.configuration.writeBaseline {
try Baseline(violations: builder.unfilteredViolations).write(toPath: baselineOutputPath)
}
try Signposts.record(name: "LintOrAnalyzeCommand.PostProcessViolations") {
Expand All @@ -55,7 +55,7 @@ struct LintOrAnalyzeCommand {
private static func collectViolations(builder: LintOrAnalyzeResultBuilder) async throws -> [SwiftLintFile] {
let options = builder.options
let visitorMutationQueue = DispatchQueue(label: "io.realm.swiftlint.lintVisitorMutation")
let baseline = try baseline(options)
let baseline = try baseline(options, builder.configuration)
return try await builder.configuration.visitLintableFiles(options: options, cache: builder.cache,
storage: builder.storage) { linter in
let currentViolations: [StyleViolation]
Expand Down Expand Up @@ -121,8 +121,8 @@ struct LintOrAnalyzeCommand {
guard numberOfSeriousViolations == 0 else { exit(2) }
}

private static func baseline(_ options: LintOrAnalyzeOptions) throws -> Baseline? {
if let baselinePath = options.baseline {
private static func baseline(_ options: LintOrAnalyzeOptions, _ configuration: Configuration) throws -> Baseline? {
if let baselinePath = options.baseline ?? configuration.baseline {
do {
return try Baseline(fromPath: baselinePath)
} catch {
Expand Down
16 changes: 16 additions & 0 deletions Tests/SwiftLintFrameworkTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ final class ConfigurationTests: SwiftLintTestCase {
XCTAssertEqual(reporterFrom(identifier: config.reporter).identifier, "xcode")
XCTAssertFalse(config.allowZeroLintableFiles)
XCTAssertFalse(config.strict)
XCTAssertNil(config.baseline)
XCTAssertNil(config.writeBaseline)
}

func testInitWithRelativePathAndRootPath() {
Expand All @@ -70,6 +72,8 @@ final class ConfigurationTests: SwiftLintTestCase {
XCTAssertEqual(config.reporter, expectedConfig.reporter)
XCTAssertTrue(config.allowZeroLintableFiles)
XCTAssertTrue(config.strict)
XCTAssertNotNil(config.baseline)
XCTAssertNotNil(config.writeBaseline)
}

func testEnableAllRulesConfiguration() throws {
Expand Down Expand Up @@ -427,6 +431,18 @@ final class ConfigurationTests: SwiftLintTestCase {
let configuration = try Configuration(dict: ["strict": true])
XCTAssertTrue(configuration.strict)
}

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

func testWriteBaseline() throws {
let baselinePath = "Baseline.json"
let configuration = try Configuration(dict: ["write_baseline": baselinePath])
XCTAssertEqual(configuration.writeBaseline, baselinePath)
}
}

// MARK: - ExcludeByPrefix option tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ line_length: 10000000000
reporter: "json"
allow_zero_lintable_files: true
strict: true
baseline: Baseline.json
write_baseline: Baseline.json