@@ -54,6 +54,7 @@ pub trait Lint {
54
54
pub struct LintContext < ' s , ' c > {
55
55
sess : & ' s Session ,
56
56
with_description : bool ,
57
+ with_json_emitter : bool ,
57
58
pub config : LinterConfig < ' c > ,
58
59
active_lints : Vec < & ' static str > ,
59
60
}
@@ -67,10 +68,11 @@ impl<'s, 'c> LintContext<'s, 'c> {
67
68
pub fn new (
68
69
sess : & ' s Session ,
69
70
with_description : bool ,
71
+ with_json_emitter : bool ,
70
72
config : LinterConfig < ' c > ,
71
73
active_lints : Vec < & ' static str > ,
72
74
) -> Self {
73
- Self { sess, with_description, config, active_lints }
75
+ Self { sess, with_description, with_json_emitter , config, active_lints }
74
76
}
75
77
76
78
pub fn session ( & self ) -> & ' s Session {
@@ -92,13 +94,19 @@ impl<'s, 'c> LintContext<'s, 'c> {
92
94
}
93
95
94
96
let desc = if self . with_description { lint. description ( ) } else { "" } ;
95
- let diag: DiagBuilder < ' _ , ( ) > = self
97
+ let mut diag: DiagBuilder < ' _ , ( ) > = self
96
98
. sess
97
99
. dcx
98
100
. diag ( lint. severity ( ) . into ( ) , desc)
99
101
. code ( DiagId :: new_str ( lint. id ( ) ) )
100
- . span ( MultiSpan :: from_span ( span) )
101
- . help ( lint. help ( ) ) ;
102
+ . span ( MultiSpan :: from_span ( span) ) ;
103
+
104
+ // Avoid ANSI characters when using a JSON emitter
105
+ if self . with_json_emitter {
106
+ diag = diag. help ( lint. help ( ) ) ;
107
+ } else {
108
+ diag = diag. help ( hyperlink ( lint. help ( ) ) ) ;
109
+ }
102
110
103
111
diag. emit ( ) ;
104
112
}
@@ -131,14 +139,21 @@ impl<'s, 'c> LintContext<'s, 'c> {
131
139
} ;
132
140
133
141
let desc = if self . with_description { lint. description ( ) } else { "" } ;
134
- let diag: DiagBuilder < ' _ , ( ) > = self
142
+ let mut diag: DiagBuilder < ' _ , ( ) > = self
135
143
. sess
136
144
. dcx
137
145
. diag ( lint. severity ( ) . into ( ) , desc)
138
146
. code ( DiagId :: new_str ( lint. id ( ) ) )
139
- . span ( MultiSpan :: from_span ( span) )
140
- . highlighted_note ( snippet. to_note ( self ) )
141
- . help ( lint. help ( ) ) ;
147
+ . span ( MultiSpan :: from_span ( span) ) ;
148
+
149
+ // Avoid ANSI characters when using a JSON emitter
150
+ if self . with_json_emitter {
151
+ diag = diag
152
+ . note ( snippet. to_note ( self ) . iter ( ) . map ( |l| l. 0 . as_str ( ) ) . collect :: < String > ( ) )
153
+ . help ( lint. help ( ) ) ;
154
+ } else {
155
+ diag = diag. highlighted_note ( snippet. to_note ( self ) ) . help ( hyperlink ( lint. help ( ) ) ) ;
156
+ }
142
157
143
158
diag. emit ( ) ;
144
159
}
@@ -253,3 +268,8 @@ impl Snippet {
253
268
& s[ byte_offset..]
254
269
}
255
270
}
271
+
272
+ /// Creates a hyperlink of the input url.
273
+ fn hyperlink ( url : & ' static str ) -> String {
274
+ format ! ( "\x1b ]8;;{url}\x1b \\ {url}\x1b ]8;;\x1b \\ " )
275
+ }
0 commit comments