Skip to content

Commit

Permalink
Merge pull request realm#1579 from marcelofabri/autocorrect-cache
Browse files Browse the repository at this point in the history
Skip files with valid cache & no violations when auto correcting
  • Loading branch information
marcelofabri authored May 28, 2017
2 parents 6c0cf07 + 3395523 commit fc5e610
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
[Markus Gasser](https://github.com/frenetisch-applaudierend)
[#1562](https://github.com/realm/SwiftLint/issues/1562)

* Skip files with valid cache & no violations when auto correcting.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1554](https://github.com/realm/SwiftLint/issues/1554)

##### Bug Fixes

* None.
Expand Down
4 changes: 4 additions & 0 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public struct Linter {
}

public func correct() -> [Correction] {
if let violations = cachedStyleViolations()?.0, violations.isEmpty {
return []
}

var corrections = [Correction]()
for rule in rules.flatMap({ $0 as? CorrectableRule }) {
let newCorrections = rule.correct(file: file)
Expand Down
22 changes: 16 additions & 6 deletions Source/swiftlint/Commands/AutoCorrectCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ struct AutoCorrectCommand: CommandProtocol {
let function = "Automatically correct warnings and errors"

func run(_ options: AutoCorrectOptions) -> Result<(), CommandantError<()>> {
return Configuration(options: options).visitLintableFiles(path: options.path, action: "Correcting",
quiet: options.quiet, useScriptInputFiles: options.useScriptInputFiles) { linter in
let configuration = Configuration(options: options)
let cache = options.ignoreCache ? nil : LinterCache(configuration: configuration)

return configuration.visitLintableFiles(path: options.path, action: "Correcting",
quiet: options.quiet, useScriptInputFiles: options.useScriptInputFiles,
cache: cache) { linter in
let corrections = linter.correct()
if !corrections.isEmpty && !options.quiet {
let correctionLogs = corrections.map({ $0.consoleDescription })
Expand Down Expand Up @@ -44,12 +48,14 @@ struct AutoCorrectOptions: OptionsProtocol {
let useScriptInputFiles: Bool
let quiet: Bool
let format: Bool
let cachePath: String
let ignoreCache: Bool

// swiftlint:disable line_length
static func create(_ path: String) -> (_ configurationFile: String) -> (_ useScriptInputFiles: Bool) -> (_ quiet: Bool) -> (_ format: Bool) -> AutoCorrectOptions {
return { configurationFile in { useScriptInputFiles in { quiet in { format in
self.init(path: path, configurationFile: configurationFile, useScriptInputFiles: useScriptInputFiles, quiet: quiet, format: format)
}}}}
static func create(_ path: String) -> (_ configurationFile: String) -> (_ useScriptInputFiles: Bool) -> (_ quiet: Bool) -> (_ format: Bool) -> (_ cachePath: String) -> (_ ignoreCache: Bool) -> AutoCorrectOptions {
return { configurationFile in { useScriptInputFiles in { quiet in { format in { cachePath in { ignoreCache in
self.init(path: path, configurationFile: configurationFile, useScriptInputFiles: useScriptInputFiles, quiet: quiet, format: format, cachePath: cachePath, ignoreCache: ignoreCache)
}}}}}}
}

static func evaluate(_ mode: CommandMode) -> Result<AutoCorrectOptions, CommandantError<CommandantError<()>>> {
Expand All @@ -62,5 +68,9 @@ struct AutoCorrectOptions: OptionsProtocol {
<*> mode <| Option(key: "format",
defaultValue: false,
usage: "should reformat the Swift files")
<*> mode <| Option(key: "cache-path", defaultValue: "",
usage: "the directory of the cache used when linting")
<*> mode <| Option(key: "no-cache", defaultValue: false,
usage: "ignore cache when linting")
}
}
4 changes: 3 additions & 1 deletion Source/swiftlint/Extensions/Configuration+CommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ extension Configuration {
// MARK: AutoCorrect Command

init(options: AutoCorrectOptions) {
self.init(commandLinePath: options.configurationFile, rootPath: options.path, quiet: options.quiet)
let cachePath = options.cachePath.isEmpty ? nil : options.cachePath
self.init(commandLinePath: options.configurationFile, rootPath: options.path,
quiet: options.quiet, cachePath: cachePath)
}
}

0 comments on commit fc5e610

Please sign in to comment.