@@ -15,6 +15,30 @@ import SwiftSyntax
1515import SwiftSyntaxParser
1616import TSCBasic
1717
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+
1842/// Unifies the handling of findings from the linter, parsing errors from the syntax parser, and
1943/// generic errors from the frontend so that they are treated uniformly by the underlying
2044/// diagnostics engine from the `swift-tools-support-core` package.
@@ -67,7 +91,9 @@ final class UnifiedDiagnosticsEngine {
6791 /// - location: The location in the source code associated with the error, or nil if there is no
6892 /// location associated with the error.
6993 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) )
7197 }
7298
7399 /// Emits a finding from the linter and any of its associated notes as diagnostics.
@@ -80,7 +106,7 @@ final class UnifiedDiagnosticsEngine {
80106
81107 for note in finding. notes {
82108 diagnosticsEngine. emit (
83- . note( " \( note. message) " ) ,
109+ . note( UnifiedDiagnosticData ( message : " \( note. message) " ) ) ,
84110 location: note. location. map ( UnifiedLocation . findingLocation) )
85111 }
86112 }
@@ -95,7 +121,7 @@ final class UnifiedDiagnosticsEngine {
95121
96122 for note in diagnostic. notes {
97123 diagnosticsEngine. emit (
98- . note( note. message. text) ,
124+ . note( UnifiedDiagnosticData ( message : note. message. text) ) ,
99125 location: note. location. map ( UnifiedLocation . parserLocation) )
100126 }
101127 }
@@ -105,21 +131,24 @@ final class UnifiedDiagnosticsEngine {
105131 private func diagnosticMessage( for message: SwiftSyntaxParser . Diagnostic . Message )
106132 -> TSCBasic . Diagnostic . Message
107133 {
134+ let data = UnifiedDiagnosticData ( category: nil , message: message. text)
135+
108136 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 )
112140 }
113141 }
114142
115143 /// Converts a lint finding into a diagnostic message that can be used by the `TSCBasic`
116144 /// diagnostics engine and returns it.
117145 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) " )
119148
120149 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 )
123152 }
124153 }
125154}
0 commit comments