Skip to content

Commit 73d0726

Browse files
committed
Address PR feedback
1 parent 0067ab6 commit 73d0726

File tree

6 files changed

+65
-72
lines changed

6 files changed

+65
-72
lines changed

Package.resolved

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ let package = Package(
2323
dependencies: [
2424
.package(
2525
url: "https://github.com/apple/swift-syntax",
26-
.branch("master")
26+
.revision("swift-DEVELOPMENT-SNAPSHOT-2020-01-29-a")
2727
),
2828
.package(url: "https://github.com/apple/swift-tools-support-core.git", from: "0.0.1"),
29-
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "0.0.1"),
29+
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "0.0.1")),
3030
],
3131
targets: [
3232
.target(

Sources/swift-format/CommandLineOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct SwiftFormatCommand: ParsableCommand {
9494
throw CleanExit.message("0.0.1")
9595
}
9696

97-
if inPlace && (mode == .format || paths.isEmpty) {
97+
if inPlace && (mode != .format || paths.isEmpty) {
9898
throw ValidationError("'--in-place' is only valid when formatting files")
9999
}
100100

Sources/swift-format/FormatError.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,8 @@ import SwiftSyntax
33

44
struct FormatError: LocalizedError {
55
var message: String
6-
76
var errorDescription: String? { message }
87

9-
static func readSource(path: String) -> FormatError {
10-
FormatError(message: "Unable to read source for linting from \(path).")
11-
}
12-
13-
static func unableToLint(path: String, message: String) -> FormatError {
14-
FormatError(message: "Unable to lint \(path): \(message).")
15-
}
16-
17-
static func unableToFormat(path: String, message: String) -> FormatError {
18-
FormatError(message: "Unable to format \(path): \(message).")
19-
}
20-
21-
static func invalidSyntax(location: SourceLocation, message: String) -> FormatError {
22-
FormatError(message: "Unable to format at \(location): \(message).")
23-
}
24-
258
static var exitWithDiagnosticErrors: FormatError {
269
// The diagnostics engine has already printed errors to stderr.
2710
FormatError(message: "")

Sources/swift-format/Run.swift

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,36 @@ import TSCBasic
2929
/// - Returns: Zero if there were no lint errors, otherwise a non-zero number.
3030
func lintMain(
3131
configuration: Configuration, sourceFile: FileHandle, assumingFilename: String?,
32-
debugOptions: DebugOptions
33-
) throws {
34-
let diagnosticEngine = makeDiagnosticEngine()
32+
debugOptions: DebugOptions, diagnosticEngine: DiagnosticEngine
33+
) {
3534
let linter = SwiftLinter(configuration: configuration, diagnosticEngine: diagnosticEngine)
3635
linter.debugOptions = debugOptions
3736
let assumingFileURL = URL(fileURLWithPath: assumingFilename ?? "<stdin>")
3837

3938
guard let source = readSource(from: sourceFile) else {
40-
throw FormatError.readSource(path: assumingFileURL.path)
39+
diagnosticEngine.diagnose(
40+
Diagnostic.Message(.error, "Unable to read source for linting from \(assumingFileURL.path)."))
41+
return
4142
}
4243

4344
do {
4445
try linter.lint(source: source, assumingFileURL: assumingFileURL)
4546
} catch SwiftFormatError.fileNotReadable {
46-
throw FormatError.unableToLint(
47-
path: assumingFileURL.path,
48-
message: "file is not readable or does not exist")
47+
let path = assumingFileURL.path
48+
diagnosticEngine.diagnose(
49+
Diagnostic.Message(.error, "Unable to lint \(path): file is not readable or does not exist."))
50+
return
4951
} catch SwiftFormatError.fileContainsInvalidSyntax(let position) {
5052
let path = assumingFileURL.path
5153
let location = SourceLocationConverter(file: path, source: source).location(for: position)
52-
throw FormatError.invalidSyntax(location: location, message: "file contains invalid or unrecognized Swift syntax.")
54+
diagnosticEngine.diagnose(
55+
Diagnostic.Message(.error, "file contains invalid or unrecognized Swift syntax."),
56+
location: location)
57+
return
5358
} catch {
54-
throw FormatError.unableToLint(path: assumingFileURL.path, message: "\(error)")
55-
}
56-
57-
if !diagnosticEngine.diagnostics.isEmpty {
58-
throw FormatError.exitWithDiagnosticErrors
59+
let path = assumingFileURL.path
60+
diagnosticEngine.diagnose(Diagnostic.Message(.error, "Unable to lint \(path): \(error)"))
61+
return
5962
}
6063
}
6164

@@ -71,15 +74,17 @@ func lintMain(
7174
/// - Returns: Zero if there were no format errors, otherwise a non-zero number.
7275
func formatMain(
7376
configuration: Configuration, sourceFile: FileHandle, assumingFilename: String?, inPlace: Bool,
74-
debugOptions: DebugOptions
75-
) throws {
76-
let diagnosticEngine = makeDiagnosticEngine()
77+
debugOptions: DebugOptions, diagnosticEngine: DiagnosticEngine
78+
) {
7779
let formatter = SwiftFormatter(configuration: configuration, diagnosticEngine: diagnosticEngine)
7880
formatter.debugOptions = debugOptions
7981
let assumingFileURL = URL(fileURLWithPath: assumingFilename ?? "<stdin>")
8082

8183
guard let source = readSource(from: sourceFile) else {
82-
throw FormatError.readSource(path: assumingFileURL.path)
84+
diagnosticEngine.diagnose(
85+
Diagnostic.Message(
86+
.error, "Unable to read source for formatting from \(assumingFileURL.path)."))
87+
return
8388
}
8489

8590
do {
@@ -97,30 +102,25 @@ func formatMain(
97102
stdoutStream.flush()
98103
}
99104
} catch SwiftFormatError.fileNotReadable {
100-
throw FormatError.unableToFormat(
101-
path: assumingFileURL.path,
102-
message: "file is not readable or does not exist")
105+
let path = assumingFileURL.path
106+
diagnosticEngine.diagnose(
107+
Diagnostic.Message(
108+
.error, "Unable to format \(path): file is not readable or does not exist."))
109+
return
103110
} catch SwiftFormatError.fileContainsInvalidSyntax(let position) {
104111
let path = assumingFileURL.path
105112
let location = SourceLocationConverter(file: path, source: source).location(for: position)
106-
throw FormatError.invalidSyntax(location: location, message: "file contains invalid or unrecognized Swift syntax.")
113+
diagnosticEngine.diagnose(
114+
Diagnostic.Message(.error, "file contains invalid or unrecognized Swift syntax."),
115+
location: location)
116+
return
107117
} catch {
108-
throw FormatError.unableToFormat(path: assumingFileURL.path, message: "\(error)")
109-
}
110-
111-
if !diagnosticEngine.diagnostics.isEmpty {
112-
throw FormatError.exitWithDiagnosticErrors
118+
let path = assumingFileURL.path
119+
diagnosticEngine.diagnose(Diagnostic.Message(.error, "Unable to format \(path): \(error)"))
120+
return
113121
}
114122
}
115123

116-
/// Makes and returns a new configured diagnostic engine.
117-
private func makeDiagnosticEngine() -> DiagnosticEngine {
118-
let engine = DiagnosticEngine()
119-
let consumer = PrintingDiagnosticConsumer()
120-
engine.addConsumer(consumer)
121-
return engine
122-
}
123-
124124
/// Reads from the given file handle until EOF is reached, then returns the contents as a UTF8
125125
/// encoded string.
126126
fileprivate func readSource(from fileHandle: FileHandle) -> String? {

Sources/swift-format/main.swift

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,50 @@ import TSCBasic
1919

2020
extension SwiftFormatCommand {
2121
func run() throws {
22+
let diagnosticEngine = makeDiagnosticEngine()
2223
switch mode {
2324
case .format:
2425
if paths.isEmpty {
2526
let configuration = try loadConfiguration(
2627
forSwiftFile: nil, configFilePath: configurationPath)
27-
try formatMain(
28+
formatMain(
2829
configuration: configuration, sourceFile: FileHandle.standardInput,
2930
assumingFilename: assumeFilename, inPlace: false,
30-
debugOptions: debugOptions)
31+
debugOptions: debugOptions, diagnosticEngine: diagnosticEngine)
3132
} else {
32-
try processSources(from: paths, configurationPath: configurationPath) {
33+
try processSources(from: paths, configurationPath: configurationPath, diagnosticEngine: diagnosticEngine) {
3334
(sourceFile, path, configuration) in
34-
try formatMain(
35+
formatMain(
3536
configuration: configuration, sourceFile: sourceFile, assumingFilename: path,
36-
inPlace: inPlace, debugOptions: debugOptions)
37+
inPlace: inPlace, debugOptions: debugOptions, diagnosticEngine: diagnosticEngine)
3738
}
3839
}
3940

4041
case .lint:
4142
if paths.isEmpty {
4243
let configuration = try loadConfiguration(
4344
forSwiftFile: nil, configFilePath: configurationPath)
44-
try lintMain(
45+
lintMain(
4546
configuration: configuration, sourceFile: FileHandle.standardInput,
46-
assumingFilename: assumeFilename, debugOptions: debugOptions)
47+
assumingFilename: assumeFilename, debugOptions: debugOptions, diagnosticEngine: diagnosticEngine)
4748
} else {
48-
try processSources(from: paths, configurationPath: configurationPath) {
49+
try processSources(from: paths, configurationPath: configurationPath, diagnosticEngine: diagnosticEngine) {
4950
(sourceFile, path, configuration) in
50-
try lintMain(
51+
lintMain(
5152
configuration: configuration, sourceFile: sourceFile, assumingFilename: path,
52-
debugOptions: debugOptions)
53+
debugOptions: debugOptions, diagnosticEngine: diagnosticEngine)
5354
}
5455
}
5556

5657
case .dumpConfiguration:
5758
try dumpDefaultConfiguration()
5859
}
60+
61+
// If any of the operations have generated diagnostics, throw an error
62+
// to exit with the error status code.
63+
if !diagnosticEngine.diagnostics.isEmpty {
64+
throw FormatError.exitWithDiagnosticErrors
65+
}
5966
}
6067
}
6168

@@ -68,14 +75,17 @@ extension SwiftFormatCommand {
6875
/// - transform: A closure that performs a transformation on a specific source file.
6976
private func processSources(
7077
from paths: [String], configurationPath: String?,
71-
transform: (FileHandle, String, Configuration) throws -> Void
78+
diagnosticEngine: DiagnosticEngine,
79+
transform: (FileHandle, String, Configuration) -> Void
7280
) throws {
7381
for path in FileIterator(paths: paths) {
7482
guard let sourceFile = FileHandle(forReadingAtPath: path) else {
75-
throw FormatError(message: "Unable to create a file handle for source from \(path).")
83+
diagnosticEngine.diagnose(
84+
Diagnostic.Message(.error, "Unable to create a file handle for source from \(path)."))
85+
return
7686
}
7787
let configuration = try loadConfiguration(forSwiftFile: path, configFilePath: configurationPath)
78-
try transform(sourceFile, path, configuration)
88+
transform(sourceFile, path, configuration)
7989
}
8090
}
8191

0 commit comments

Comments
 (0)