Skip to content

Commit 730943f

Browse files
authored
Merge pull request swiftlang#12 from allevato/take-source-as-string
Add lint/format APIs that take source text directly.
2 parents 7cee6d3 + a9e6b0c commit 730943f

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

Sources/SwiftFormat/SwiftFormatter.swift

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,40 @@ public final class SwiftFormatter {
6161
try format(syntax: sourceFile, assumingFileURL: url, to: &outputStream)
6262
}
6363

64+
/// Formats the given Swift source code and writes the result to an output stream.
65+
///
66+
/// - Parameters:
67+
/// - source: The Swift source code to be formatted.
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.
71+
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
72+
/// be written.
73+
/// - Throws: If an unrecoverable error occurs when formatting the code.
74+
public func format<Output: TextOutputStream>(
75+
source: String, assumingFileURL url: URL?, to outputStream: inout Output
76+
) throws {
77+
let sourceFile = try SyntaxParser.parse(source: source)
78+
try format(syntax: sourceFile, assumingFileURL: url, to: &outputStream)
79+
}
80+
6481
/// Formats the given Swift syntax tree and writes the result to an output stream.
6582
///
6683
/// - Parameters:
6784
/// - syntax: The Swift syntax tree to be converted to source code and formatted.
68-
/// - 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.
6988
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
7089
/// be written.
7190
/// - Throws: If an unrecoverable error occurs when formatting the code.
7291
public func format<Output: TextOutputStream>(
73-
syntax: SourceFileSyntax, assumingFileURL url: URL, to outputStream: inout Output
92+
syntax: SourceFileSyntax, assumingFileURL url: URL?, to outputStream: inout Output
7493
) throws {
94+
let assumedURL = url ?? URL(fileURLWithPath: "source")
95+
7596
let context = Context(
76-
configuration: configuration, diagnosticEngine: diagnosticEngine, fileURL: url,
97+
configuration: configuration, diagnosticEngine: diagnosticEngine, fileURL: assumedURL,
7798
sourceFileSyntax: syntax)
7899
let pipeline = FormatPipeline(context: context)
79100
let transformedSyntax = pipeline.visit(syntax)
@@ -89,6 +110,4 @@ public final class SwiftFormatter {
89110
printTokenStream: debugOptions.contains(.dumpTokenStream))
90111
outputStream.write(printer.prettyPrint())
91112
}
92-
93-
// TODO: Add an overload of `format` that takes the source text directly.
94113
}

Sources/SwiftFormat/SwiftLinter.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,22 @@ public final class SwiftLinter {
5757
try lint(syntax: sourceFile, assumingFileURL: url)
5858
}
5959

60+
/// Lints the given Swift source code.
61+
///
62+
/// - Parameters:
63+
/// - source: The Swift source code to be linted.
64+
/// - url: A file URL denoting the filename/path that should be assumed for this source code.
65+
/// - Throws: If an unrecoverable error occurs when formatting the code.
66+
public func lint(source: String, assumingFileURL url: URL) throws {
67+
let sourceFile = try SyntaxParser.parse(url)
68+
try lint(syntax: sourceFile, assumingFileURL: url)
69+
}
70+
6071
/// Lints the given Swift syntax tree.
6172
///
6273
/// - Parameters:
6374
/// - syntax: The Swift syntax tree to be converted to be linted.
6475
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree.
65-
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
66-
/// be written.
6776
/// - Throws: If an unrecoverable error occurs when formatting the code.
6877
public func lint(syntax: SourceFileSyntax, assumingFileURL url: URL) throws {
6978
let context = Context(
@@ -82,6 +91,4 @@ public final class SwiftLinter {
8291
let ws = WhitespaceLinter(user: syntax.description, formatted: formatted, context: context)
8392
ws.lint()
8493
}
85-
86-
// TODO: Add an overload of `lint` that takes the source text directly.
8794
}

0 commit comments

Comments
 (0)