Skip to content

Commit 2dcf715

Browse files
authored
Merge pull request #599 from allevato/the-emptiness
Don't do anything if the input is empty.
2 parents a2055fa + a0ee6eb commit 2dcf715

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

Sources/SwiftFormat/API/SwiftFormatter.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,12 @@ public final class SwiftFormatter {
6767
if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDir), isDir.boolValue {
6868
throw SwiftFormatError.isDirectory
6969
}
70-
let source = try String(contentsOf: url, encoding: .utf8)
71-
let sourceFile = try parseAndEmitDiagnostics(
72-
source: source,
73-
operatorTable: .standardOperators,
70+
71+
try format(
72+
source: String(contentsOf: url, encoding: .utf8),
7473
assumingFileURL: url,
74+
to: &outputStream,
7575
parsingDiagnosticHandler: parsingDiagnosticHandler)
76-
try format(
77-
syntax: sourceFile, operatorTable: .standardOperators, assumingFileURL: url, source: source,
78-
to: &outputStream)
7976
}
8077

8178
/// Formats the given Swift source code and writes the result to an output stream.
@@ -101,6 +98,11 @@ public final class SwiftFormatter {
10198
to outputStream: inout Output,
10299
parsingDiagnosticHandler: ((Diagnostic, SourceLocation) -> Void)? = nil
103100
) throws {
101+
// If the file or input string is completely empty, do nothing. This prevents even a trailing
102+
// newline from being emitted for an empty file. (This is consistent with clang-format, which
103+
// also does not touch an empty file even if the setting to add trailing newlines is enabled.)
104+
guard !source.isEmpty else { return }
105+
104106
let sourceFile = try parseAndEmitDiagnostics(
105107
source: source,
106108
operatorTable: .standardOperators,

Sources/SwiftFormat/API/SwiftLinter.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,11 @@ public final class SwiftLinter {
6464
if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDir), isDir.boolValue {
6565
throw SwiftFormatError.isDirectory
6666
}
67-
let source = try String(contentsOf: url, encoding: .utf8)
68-
let sourceFile = try parseAndEmitDiagnostics(
69-
source: source,
70-
operatorTable: .standardOperators,
67+
68+
try lint(
69+
source: String(contentsOf: url, encoding: .utf8),
7170
assumingFileURL: url,
7271
parsingDiagnosticHandler: parsingDiagnosticHandler)
73-
try lint(
74-
syntax: sourceFile, operatorTable: .standardOperators, assumingFileURL: url, source: source)
7572
}
7673

7774
/// Lints the given Swift source code.
@@ -92,6 +89,11 @@ public final class SwiftLinter {
9289
assumingFileURL url: URL,
9390
parsingDiagnosticHandler: ((Diagnostic, SourceLocation) -> Void)? = nil
9491
) throws {
92+
// If the file or input string is completely empty, do nothing. This prevents even a trailing
93+
// newline from being diagnosed for an empty file. (This is consistent with clang-format, which
94+
// also does not touch an empty file even if the setting to add trailing newlines is enabled.)
95+
guard !source.isEmpty else { return }
96+
9597
let sourceFile = try parseAndEmitDiagnostics(
9698
source: source,
9799
operatorTable: .standardOperators,

0 commit comments

Comments
 (0)