@@ -65,12 +65,14 @@ public final class SwiftFormatter {
65
65
///
66
66
/// - Parameters:
67
67
/// - source: The Swift source code to be formatted.
68
- /// - url: A file URL denoting the filename/path that should be assumed for this source code.
68
+ /// - url: A file URL denoting the filename/path that should be assumed for this syntax tree,
69
+ /// which is associated with any diagnostics emitted during formatting. If this is nil, a
70
+ /// dummy value will be used.
69
71
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
70
72
/// be written.
71
73
/// - Throws: If an unrecoverable error occurs when formatting the code.
72
74
public func format< Output: TextOutputStream > (
73
- source: String , assumingFileURL url: URL , to outputStream: inout Output
75
+ source: String , assumingFileURL url: URL ? , to outputStream: inout Output
74
76
) throws {
75
77
let sourceFile = try SyntaxParser . parse ( source: source)
76
78
try format ( syntax: sourceFile, assumingFileURL: url, to: & outputStream)
@@ -80,15 +82,19 @@ public final class SwiftFormatter {
80
82
///
81
83
/// - Parameters:
82
84
/// - syntax: The Swift syntax tree to be converted to source code and formatted.
83
- /// - url: A file URL denoting the filename/path that should be assumed for this syntax tree.
85
+ /// - url: A file URL denoting the filename/path that should be assumed for this syntax tree,
86
+ /// which is associated with any diagnostics emitted during formatting. If this is nil, a
87
+ /// dummy value will be used.
84
88
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
85
89
/// be written.
86
90
/// - Throws: If an unrecoverable error occurs when formatting the code.
87
91
public func format< Output: TextOutputStream > (
88
- syntax: SourceFileSyntax , assumingFileURL url: URL , to outputStream: inout Output
92
+ syntax: SourceFileSyntax , assumingFileURL url: URL ? , to outputStream: inout Output
89
93
) throws {
94
+ let assumedURL = url ?? URL ( fileURLWithPath: " source " )
95
+
90
96
let context = Context (
91
- configuration: configuration, diagnosticEngine: diagnosticEngine, fileURL: url ,
97
+ configuration: configuration, diagnosticEngine: diagnosticEngine, fileURL: assumedURL ,
92
98
sourceFileSyntax: syntax)
93
99
let pipeline = FormatPipeline ( context: context)
94
100
let transformedSyntax = pipeline. visit ( syntax)
0 commit comments