@@ -18,6 +18,7 @@ import SwiftFormatRules
18
18
import SwiftFormatWhitespaceLinter
19
19
import SwiftSyntax
20
20
import SwiftParser
21
+ import SwiftDiagnostics
21
22
22
23
/// Diagnoses and reports problems in Swift source code or syntax trees according to the Swift style
23
24
/// guidelines.
@@ -53,7 +54,7 @@ public final class SwiftLinter {
53
54
/// - Throws: If an unrecoverable error occurs when formatting the code.
54
55
public func lint(
55
56
contentsOf url: URL ,
56
- parsingDiagnosticHandler: ( ( Diagnostic ) -> Void ) ? = nil
57
+ parsingDiagnosticHandler: ( ( Diagnostic , SourceLocation ) -> Void ) ? = nil
57
58
) throws {
58
59
guard FileManager . default. isReadableFile ( atPath: url. path) else {
59
60
throw SwiftFormatError . fileNotReadable
@@ -64,7 +65,8 @@ public final class SwiftLinter {
64
65
}
65
66
let source = try String ( contentsOf: url, encoding: . utf8)
66
67
let sourceFile = try Parser . parse ( source: source)
67
- try lint ( syntax: sourceFile, assumingFileURL: url, source: source)
68
+ try lint ( syntax: sourceFile, assumingFileURL: url,
69
+ source: source, parsingDiagnosticHandler: parsingDiagnosticHandler)
68
70
}
69
71
70
72
/// Lints the given Swift source code.
@@ -78,10 +80,11 @@ public final class SwiftLinter {
78
80
public func lint(
79
81
source: String ,
80
82
assumingFileURL url: URL ,
81
- parsingDiagnosticHandler: ( ( Diagnostic ) -> Void ) ? = nil
83
+ parsingDiagnosticHandler: ( ( Diagnostic , SourceLocation ) -> Void ) ? = nil
82
84
) throws {
83
85
let sourceFile = try Parser . parse ( source: source)
84
- try lint ( syntax: sourceFile, assumingFileURL: url, source: source)
86
+ try lint ( syntax: sourceFile, assumingFileURL: url,
87
+ source: source, parsingDiagnosticHandler: parsingDiagnosticHandler)
85
88
}
86
89
87
90
/// Lints the given Swift syntax tree.
@@ -93,14 +96,29 @@ public final class SwiftLinter {
93
96
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree.
94
97
/// - Throws: If an unrecoverable error occurs when formatting the code.
95
98
public func lint( syntax: SourceFileSyntax , assumingFileURL url: URL ) throws {
96
- try lint ( syntax: syntax, assumingFileURL: url, source: nil )
99
+ try lint ( syntax: syntax, assumingFileURL: url,
100
+ source: nil , parsingDiagnosticHandler: nil )
97
101
}
98
102
99
- private func lint( syntax: SourceFileSyntax , assumingFileURL url: URL , source: String ? ) throws {
103
+ private func lint(
104
+ syntax: SourceFileSyntax ,
105
+ assumingFileURL url: URL ,
106
+ source: String ? ,
107
+ parsingDiagnosticHandler: ( ( Diagnostic , SourceLocation ) -> Void ) ?
108
+ ) throws {
100
109
if let position = _firstInvalidSyntaxPosition ( in: Syntax ( syntax) ) {
101
110
throw SwiftFormatError . fileContainsInvalidSyntax ( position: position)
102
111
}
103
112
113
+ if let parsingDiagnosticHandler = parsingDiagnosticHandler {
114
+ let expectedConverter = SourceLocationConverter ( file: url. path, tree: syntax)
115
+ let diagnostics = ParseDiagnosticsGenerator . diagnostics ( for: syntax)
116
+ for diagnostic in diagnostics {
117
+ let location = diagnostic. location ( converter: expectedConverter)
118
+ parsingDiagnosticHandler ( diagnostic, location)
119
+ }
120
+ }
121
+
104
122
let context = Context (
105
123
configuration: configuration, findingConsumer: findingConsumer, fileURL: url,
106
124
sourceFileSyntax: syntax, source: source, ruleNameCache: ruleNameCache)
0 commit comments