@@ -29,33 +29,36 @@ import TSCBasic
29
29
/// - Returns: Zero if there were no lint errors, otherwise a non-zero number.
30
30
func lintMain(
31
31
configuration: Configuration , sourceFile: FileHandle , assumingFilename: String ? ,
32
- debugOptions: DebugOptions
33
- ) throws {
34
- let diagnosticEngine = makeDiagnosticEngine ( )
32
+ debugOptions: DebugOptions , diagnosticEngine: DiagnosticEngine
33
+ ) {
35
34
let linter = SwiftLinter ( configuration: configuration, diagnosticEngine: diagnosticEngine)
36
35
linter. debugOptions = debugOptions
37
36
let assumingFileURL = URL ( fileURLWithPath: assumingFilename ?? " <stdin> " )
38
37
39
38
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
41
42
}
42
43
43
44
do {
44
45
try linter. lint ( source: source, assumingFileURL: assumingFileURL)
45
46
} 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
49
51
} catch SwiftFormatError . fileContainsInvalidSyntax( let position) {
50
52
let path = assumingFileURL. path
51
53
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
53
58
} 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
59
62
}
60
63
}
61
64
@@ -71,15 +74,17 @@ func lintMain(
71
74
/// - Returns: Zero if there were no format errors, otherwise a non-zero number.
72
75
func formatMain(
73
76
configuration: Configuration , sourceFile: FileHandle , assumingFilename: String ? , inPlace: Bool ,
74
- debugOptions: DebugOptions
75
- ) throws {
76
- let diagnosticEngine = makeDiagnosticEngine ( )
77
+ debugOptions: DebugOptions , diagnosticEngine: DiagnosticEngine
78
+ ) {
77
79
let formatter = SwiftFormatter ( configuration: configuration, diagnosticEngine: diagnosticEngine)
78
80
formatter. debugOptions = debugOptions
79
81
let assumingFileURL = URL ( fileURLWithPath: assumingFilename ?? " <stdin> " )
80
82
81
83
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
83
88
}
84
89
85
90
do {
@@ -97,30 +102,25 @@ func formatMain(
97
102
stdoutStream. flush ( )
98
103
}
99
104
} 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
103
110
} catch SwiftFormatError . fileContainsInvalidSyntax( let position) {
104
111
let path = assumingFileURL. path
105
112
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
107
117
} 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
113
121
}
114
122
}
115
123
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
-
124
124
/// Reads from the given file handle until EOF is reached, then returns the contents as a UTF8
125
125
/// encoded string.
126
126
fileprivate func readSource( from fileHandle: FileHandle ) -> String ? {
0 commit comments