Skip to content

Commit

Permalink
Merge pull request #2257 from marcelofabri/format-dont-touch
Browse files Browse the repository at this point in the history
Don’t overwrite file when formatting if not needed
  • Loading branch information
marcelofabri authored Jun 24, 2018
2 parents 981d2a9 + 701abbe commit 1c8d5a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
[Frederick Pietschmann](https://github.com/fredpi)
[#2250](https://github.com/realm/SwiftLint/issues/2250)

* Don't touch files when running `autocorrect --format` if the contents haven't
changed.
[Marcelo Fabri](https://github.com/marcelofabri)
[#2249](https://github.com/realm/SwiftLint/issues/2249)

#### Bug Fixes

* Fix an issue with `control_statement` where commas in clauses prevented the
Expand Down
9 changes: 9 additions & 0 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,13 @@ public struct Linter {
}
return corrections
}

public func format(useTabs: Bool, indentWidth: Int) {
let formattedContents = try? file.format(trimmingTrailingWhitespace: true,
useTabs: useTabs,
indentWidth: indentWidth)
if let formattedContents = formattedContents {
file.write(formattedContents)
}
}
}
6 changes: 1 addition & 5 deletions Source/swiftlint/Commands/AutoCorrectCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ struct AutoCorrectCommand: CommandProtocol {
queuedPrint(correctionLogs.joined(separator: "\n"))
}
if options.format {
let formattedContents = try? linter.file.format(trimmingTrailingWhitespace: true,
useTabs: useTabs,
indentWidth: indentWidth)
_ = try? formattedContents?
.write(toFile: linter.file.path!, atomically: true, encoding: .utf8)
linter.format(useTabs: useTabs, indentWidth: indentWidth)
}
}.flatMap { files in
if !options.quiet {
Expand Down

0 comments on commit 1c8d5a4

Please sign in to comment.