@@ -15,6 +15,30 @@ import SwiftSyntax
15
15
import SwiftSyntaxParser
16
16
import TSCBasic
17
17
18
+ /// Diagnostic data that retains the separation of a finding category (if present) from the rest of
19
+ /// the message, allowing diagnostic printers that want to print those values separately to do so.
20
+ struct UnifiedDiagnosticData : DiagnosticData {
21
+ /// The category of the diagnostic, if any.
22
+ var category : String ?
23
+
24
+ /// The message text associated with the diagnostic.
25
+ var message : String
26
+
27
+ var description : String {
28
+ if let category = category {
29
+ return " [ \( category) ] \( message) "
30
+ } else {
31
+ return message
32
+ }
33
+ }
34
+
35
+ /// Creates a new unified diagnostic with the given optional category and message.
36
+ init ( category: String ? = nil , message: String ) {
37
+ self . category = category
38
+ self . message = message
39
+ }
40
+ }
41
+
18
42
/// Unifies the handling of findings from the linter, parsing errors from the syntax parser, and
19
43
/// generic errors from the frontend so that they are treated uniformly by the underlying
20
44
/// diagnostics engine from the `swift-tools-support-core` package.
@@ -67,7 +91,9 @@ final class UnifiedDiagnosticsEngine {
67
91
/// - location: The location in the source code associated with the error, or nil if there is no
68
92
/// location associated with the error.
69
93
func emitError( _ message: String , location: SourceLocation ? = nil ) {
70
- diagnosticsEngine. emit ( . error( message) , location: location. map ( UnifiedLocation . parserLocation) )
94
+ diagnosticsEngine. emit (
95
+ . error( UnifiedDiagnosticData ( message: message) ) ,
96
+ location: location. map ( UnifiedLocation . parserLocation) )
71
97
}
72
98
73
99
/// Emits a finding from the linter and any of its associated notes as diagnostics.
@@ -80,7 +106,7 @@ final class UnifiedDiagnosticsEngine {
80
106
81
107
for note in finding. notes {
82
108
diagnosticsEngine. emit (
83
- . note( " \( note. message) " ) ,
109
+ . note( UnifiedDiagnosticData ( message : " \( note. message) " ) ) ,
84
110
location: note. location. map ( UnifiedLocation . findingLocation) )
85
111
}
86
112
}
@@ -95,7 +121,7 @@ final class UnifiedDiagnosticsEngine {
95
121
96
122
for note in diagnostic. notes {
97
123
diagnosticsEngine. emit (
98
- . note( note. message. text) ,
124
+ . note( UnifiedDiagnosticData ( message : note. message. text) ) ,
99
125
location: note. location. map ( UnifiedLocation . parserLocation) )
100
126
}
101
127
}
@@ -105,21 +131,24 @@ final class UnifiedDiagnosticsEngine {
105
131
private func diagnosticMessage( for message: SwiftSyntaxParser . Diagnostic . Message )
106
132
-> TSCBasic . Diagnostic . Message
107
133
{
134
+ let data = UnifiedDiagnosticData ( category: nil , message: message. text)
135
+
108
136
switch message. severity {
109
- case . error: return . error( message . text )
110
- case . warning: return . warning( message . text )
111
- case . note: return . note( message . text )
137
+ case . error: return . error( data )
138
+ case . warning: return . warning( data )
139
+ case . note: return . note( data )
112
140
}
113
141
}
114
142
115
143
/// Converts a lint finding into a diagnostic message that can be used by the `TSCBasic`
116
144
/// diagnostics engine and returns it.
117
145
private func diagnosticMessage( for finding: Finding ) -> TSCBasic . Diagnostic . Message {
118
- let message = " [ \( finding. category) ] \( finding. message. text) "
146
+ let data =
147
+ UnifiedDiagnosticData ( category: " \( finding. category) " , message: " \( finding. message. text) " )
119
148
120
149
switch finding. severity {
121
- case . error: return . error( message )
122
- case . warning: return . warning( message )
150
+ case . error: return . error( data )
151
+ case . warning: return . warning( data )
123
152
}
124
153
}
125
154
}
0 commit comments