Skip to content

Commit 853b910

Browse files
committed
Move suppression logic up to FileIterator
1 parent f54a359 commit 853b910

File tree

5 files changed

+11
-43
lines changed

5 files changed

+11
-43
lines changed

Documentation/Configuration.md

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ top-level keys and values:
1111
* `version` _(number)_: The version of the configuration file. For now, this
1212
should always be `1`.
1313

14-
* `skipAll` _(boolean)_: If this is true, all other configuration
15-
options are ignored, and formatting/linting is disabled.
16-
1714
* `lineLength` _(number)_: The maximum allowed length of a line, in
1815
characters.
1916

Sources/SwiftFormat/API/Configuration.swift

+1-29
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,9 @@ internal let highestSupportedConfigurationVersion = 1
2424
/// Holds the complete set of configured values and defaults.
2525
public struct Configuration: Codable, Equatable {
2626

27-
/// Name of the configuration file to look for.
28-
/// The presence of this file in a directory will cause the formatter
29-
/// to use the configuration specified in that file.
30-
private static let configurationFileName = ".swift-format"
31-
32-
/// Name of the suppression file to look for.
33-
/// The presence of this file in a directory will cause the formatter
34-
/// to skip formatting files in that directory and its subdirectories.
35-
private static let suppressionFileName = ".swift-format-ignore"
3627

3728
private enum CodingKeys: CodingKey {
3829
case version
39-
case skipAll
4030
case maximumBlankLines
4131
case lineLength
4232
case spacesBeforeEndOfLineComments
@@ -70,9 +60,6 @@ public struct Configuration: Codable, Equatable {
7060

7161
/// MARK: Common configuration
7262

73-
/// Is all formatting disbled?
74-
public var skipAll = false
75-
7663
/// The dictionary containing the rule names that we wish to run on. A rule is not used if it is
7764
/// marked as `false`, or if it is missing from the dictionary.
7865
public var rules: [String: Bool]
@@ -276,13 +263,6 @@ public struct Configuration: Codable, Equatable {
276263

277264
/// Creates a new `Configuration` by loading it from a configuration file.
278265
public init(contentsOf url: URL) throws {
279-
if url.lastPathComponent == Self.suppressionFileName {
280-
var config = Configuration()
281-
config.skipAll = true
282-
self = config
283-
return
284-
}
285-
286266
let data = try Data(contentsOf: url)
287267
try self.init(data: data)
288268
}
@@ -316,9 +296,6 @@ public struct Configuration: Codable, Equatable {
316296
// default-initialized instance.
317297
let defaults = Configuration()
318298

319-
self.skipAll =
320-
try container.decodeIfPresent(Bool.self, forKey: .skipAll)
321-
?? false
322299
self.maximumBlankLines =
323300
try container.decodeIfPresent(Int.self, forKey: .maximumBlankLines)
324301
?? defaults.maximumBlankLines
@@ -406,7 +383,6 @@ public struct Configuration: Codable, Equatable {
406383
var container = encoder.container(keyedBy: CodingKeys.self)
407384

408385
try container.encode(version, forKey: .version)
409-
try container.encode(skipAll, forKey: .skipAll)
410386
try container.encode(maximumBlankLines, forKey: .maximumBlankLines)
411387
try container.encode(lineLength, forKey: .lineLength)
412388
try container.encode(spacesBeforeEndOfLineComments, forKey: .spacesBeforeEndOfLineComments)
@@ -451,11 +427,7 @@ public struct Configuration: Codable, Equatable {
451427
}
452428
repeat {
453429
candidateDirectory.deleteLastPathComponent()
454-
let suppressingFile = candidateDirectory.appendingPathComponent(Self.suppressionFileName)
455-
if FileManager.default.isReadableFile(atPath: suppressingFile.path) {
456-
return suppressingFile
457-
}
458-
let candidateFile = candidateDirectory.appendingPathComponent(Self.configurationFileName)
430+
let candidateFile = candidateDirectory.appendingPathComponent(".swift-format")
459431
if FileManager.default.isReadableFile(atPath: candidateFile.path) {
460432
return candidateFile
461433
}

Sources/SwiftFormat/API/SwiftFormatter.swift

-6
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,6 @@ public final class SwiftFormatter {
106106
// also does not touch an empty file even if the setting to add trailing newlines is enabled.)
107107
guard !source.isEmpty else { return }
108108

109-
// If skipAll is set, just emit the source as-is.
110-
guard !configuration.skipAll else {
111-
outputStream.write(source)
112-
return
113-
}
114-
115109
let sourceFile = try parseAndEmitDiagnostics(
116110
source: source,
117111
operatorTable: .standardOperators,

Sources/SwiftFormat/API/SwiftLinter.swift

-5
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ public final class SwiftLinter {
9393
// newline from being diagnosed for an empty file. (This is consistent with clang-format, which
9494
// also does not touch an empty file even if the setting to add trailing newlines is enabled.)
9595
guard !source.isEmpty else { return }
96-
97-
// If skipAll is set, do nothing.
98-
guard !configuration.skipAll else {
99-
return
100-
}
10196

10297
let sourceFile = try parseAndEmitDiagnostics(
10398
source: source,

Sources/SwiftFormat/Utilities/FileIterator.swift

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import Foundation
1717
@_spi(Internal)
1818
public struct FileIterator: Sequence, IteratorProtocol {
1919

20+
/// Name of the suppression file to look for.
21+
/// The presence of this file in a directory will cause the formatter
22+
/// to skip formatting files in that directory and its subdirectories.
23+
private static let suppressionFileName = ".swift-format-ignore"
24+
2025
/// List of file and directory URLs to iterate over.
2126
private let urls: [URL]
2227

@@ -83,6 +88,11 @@ public struct FileIterator: Sequence, IteratorProtocol {
8388
fallthrough
8489

8590
case .typeDirectory:
91+
let suppressionFile = next.appendingPathComponent(Self.suppressionFileName)
92+
if FileManager.default.fileExists(atPath: suppressionFile.path) {
93+
continue
94+
}
95+
8696
dirIterator = FileManager.default.enumerator(
8797
at: next,
8898
includingPropertiesForKeys: nil,

0 commit comments

Comments
 (0)