@@ -67,15 +67,12 @@ public final class SwiftFormatter {
67
67
if FileManager . default. fileExists ( atPath: url. path, isDirectory: & isDir) , isDir. boolValue {
68
68
throw SwiftFormatError . isDirectory
69
69
}
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) ,
74
73
assumingFileURL: url,
74
+ to: & outputStream,
75
75
parsingDiagnosticHandler: parsingDiagnosticHandler)
76
- try format (
77
- syntax: sourceFile, operatorTable: . standardOperators, assumingFileURL: url, source: source,
78
- to: & outputStream)
79
76
}
80
77
81
78
/// Formats the given Swift source code and writes the result to an output stream.
@@ -101,6 +98,11 @@ public final class SwiftFormatter {
101
98
to outputStream: inout Output ,
102
99
parsingDiagnosticHandler: ( ( Diagnostic , SourceLocation ) -> Void ) ? = nil
103
100
) 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
+
104
106
let sourceFile = try parseAndEmitDiagnostics (
105
107
source: source,
106
108
operatorTable: . standardOperators,
0 commit comments