Skip to content

Commit a9e6b0c

Browse files
committed
Make URL argument for SwiftFormatter optional.
1 parent a22e7a5 commit a9e6b0c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Sources/SwiftFormat/SwiftFormatter.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ public final class SwiftFormatter {
6565
///
6666
/// - Parameters:
6767
/// - 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.
6971
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
7072
/// be written.
7173
/// - Throws: If an unrecoverable error occurs when formatting the code.
7274
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
7476
) throws {
7577
let sourceFile = try SyntaxParser.parse(source: source)
7678
try format(syntax: sourceFile, assumingFileURL: url, to: &outputStream)
@@ -80,15 +82,19 @@ public final class SwiftFormatter {
8082
///
8183
/// - Parameters:
8284
/// - 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.
8488
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
8589
/// be written.
8690
/// - Throws: If an unrecoverable error occurs when formatting the code.
8791
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
8993
) throws {
94+
let assumedURL = url ?? URL(fileURLWithPath: "source")
95+
9096
let context = Context(
91-
configuration: configuration, diagnosticEngine: diagnosticEngine, fileURL: url,
97+
configuration: configuration, diagnosticEngine: diagnosticEngine, fileURL: assumedURL,
9298
sourceFileSyntax: syntax)
9399
let pipeline = FormatPipeline(context: context)
94100
let transformedSyntax = pipeline.visit(syntax)

0 commit comments

Comments
 (0)