@@ -3,10 +3,11 @@ use std::collections::BTreeSet;
3
3
use itertools:: Itertools ;
4
4
use rustc_hash:: { FxHashMap , FxHashSet } ;
5
5
6
- use ruff_diagnostics:: { Diagnostic , Edit , Fix , IsolationLevel , SourceMap } ;
6
+ use ruff_diagnostics:: { Edit , Fix , IsolationLevel , SourceMap } ;
7
7
use ruff_text_size:: { Ranged , TextLen , TextRange , TextSize } ;
8
8
9
9
use crate :: linter:: FixTable ;
10
+ use crate :: message:: { DiagnosticMessage , Message } ;
10
11
use crate :: registry:: { AsRule , Rule } ;
11
12
use crate :: settings:: types:: UnsafeFixes ;
12
13
use crate :: Locator ;
@@ -26,16 +27,17 @@ pub(crate) struct FixResult {
26
27
27
28
/// Fix errors in a file, and write the fixed source code to disk.
28
29
pub ( crate ) fn fix_file (
29
- diagnostics : & [ Diagnostic ] ,
30
+ messages : & [ Message ] ,
30
31
locator : & Locator ,
31
32
unsafe_fixes : UnsafeFixes ,
32
33
) -> Option < FixResult > {
33
34
let required_applicability = unsafe_fixes. required_applicability ( ) ;
34
35
35
- let mut with_fixes = diagnostics
36
+ let mut with_fixes = messages
36
37
. iter ( )
37
- . filter ( |diagnostic| {
38
- diagnostic
38
+ . filter_map ( Message :: as_diagnostic_message)
39
+ . filter ( |message| {
40
+ message
39
41
. fix
40
42
. as_ref ( )
41
43
. is_some_and ( |fix| fix. applies ( required_applicability) )
@@ -51,7 +53,7 @@ pub(crate) fn fix_file(
51
53
52
54
/// Apply a series of fixes.
53
55
fn apply_fixes < ' a > (
54
- diagnostics : impl Iterator < Item = & ' a Diagnostic > ,
56
+ diagnostics : impl Iterator < Item = & ' a DiagnosticMessage > ,
55
57
locator : & ' a Locator < ' a > ,
56
58
) -> FixResult {
57
59
let mut output = String :: with_capacity ( locator. len ( ) ) ;
@@ -161,30 +163,38 @@ fn cmp_fix(rule1: Rule, rule2: Rule, fix1: &Fix, fix2: &Fix) -> std::cmp::Orderi
161
163
162
164
#[ cfg( test) ]
163
165
mod tests {
164
- use ruff_diagnostics:: { Diagnostic , Edit , Fix , SourceMarker } ;
166
+ use ruff_diagnostics:: { Edit , Fix , SourceMarker } ;
167
+ use ruff_source_file:: SourceFileBuilder ;
165
168
use ruff_text_size:: { Ranged , TextSize } ;
166
169
167
170
use crate :: fix:: { apply_fixes, FixResult } ;
171
+ use crate :: message:: DiagnosticMessage ;
168
172
use crate :: rules:: pycodestyle:: rules:: MissingNewlineAtEndOfFile ;
169
173
use crate :: Locator ;
170
174
171
175
#[ allow( deprecated) ]
172
- fn create_diagnostics ( edit : impl IntoIterator < Item = Edit > ) -> Vec < Diagnostic > {
176
+ fn create_diagnostics (
177
+ filename : & str ,
178
+ source : & str ,
179
+ edit : impl IntoIterator < Item = Edit > ,
180
+ ) -> Vec < DiagnosticMessage > {
173
181
edit. into_iter ( )
174
- . map ( |edit| Diagnostic {
182
+ . map ( |edit| DiagnosticMessage {
175
183
// The choice of rule here is arbitrary.
176
184
kind : MissingNewlineAtEndOfFile . into ( ) ,
177
185
range : edit. range ( ) ,
178
186
fix : Some ( Fix :: safe_edit ( edit) ) ,
179
187
parent : None ,
188
+ file : SourceFileBuilder :: new ( filename, source) . finish ( ) ,
189
+ noqa_offset : TextSize :: default ( ) ,
180
190
} )
181
191
. collect ( )
182
192
}
183
193
184
194
#[ test]
185
195
fn empty_file ( ) {
186
196
let locator = Locator :: new ( r"" ) ;
187
- let diagnostics = create_diagnostics ( [ ] ) ;
197
+ let diagnostics = create_diagnostics ( "<filename>" , locator . contents ( ) , [ ] ) ;
188
198
let FixResult {
189
199
code,
190
200
fixes,
@@ -205,10 +215,14 @@ print("hello world")
205
215
"#
206
216
. trim ( ) ,
207
217
) ;
208
- let diagnostics = create_diagnostics ( [ Edit :: insertion (
209
- "import sys\n " . to_string ( ) ,
210
- TextSize :: new ( 10 ) ,
211
- ) ] ) ;
218
+ let diagnostics = create_diagnostics (
219
+ "<filename>" ,
220
+ locator. contents ( ) ,
221
+ [ Edit :: insertion (
222
+ "import sys\n " . to_string ( ) ,
223
+ TextSize :: new ( 10 ) ,
224
+ ) ] ,
225
+ ) ;
212
226
let FixResult {
213
227
code,
214
228
fixes,
@@ -243,11 +257,15 @@ class A(object):
243
257
"
244
258
. trim ( ) ,
245
259
) ;
246
- let diagnostics = create_diagnostics ( [ Edit :: replacement (
247
- "Bar" . to_string ( ) ,
248
- TextSize :: new ( 8 ) ,
249
- TextSize :: new ( 14 ) ,
250
- ) ] ) ;
260
+ let diagnostics = create_diagnostics (
261
+ "<filename>" ,
262
+ locator. contents ( ) ,
263
+ [ Edit :: replacement (
264
+ "Bar" . to_string ( ) ,
265
+ TextSize :: new ( 8 ) ,
266
+ TextSize :: new ( 14 ) ,
267
+ ) ] ,
268
+ ) ;
251
269
let FixResult {
252
270
code,
253
271
fixes,
@@ -280,7 +298,11 @@ class A(object):
280
298
"
281
299
. trim ( ) ,
282
300
) ;
283
- let diagnostics = create_diagnostics ( [ Edit :: deletion ( TextSize :: new ( 7 ) , TextSize :: new ( 15 ) ) ] ) ;
301
+ let diagnostics = create_diagnostics (
302
+ "<filename>" ,
303
+ locator. contents ( ) ,
304
+ [ Edit :: deletion ( TextSize :: new ( 7 ) , TextSize :: new ( 15 ) ) ] ,
305
+ ) ;
284
306
let FixResult {
285
307
code,
286
308
fixes,
@@ -313,10 +335,14 @@ class A(object, object, object):
313
335
"
314
336
. trim ( ) ,
315
337
) ;
316
- let diagnostics = create_diagnostics ( [
317
- Edit :: deletion ( TextSize :: from ( 8 ) , TextSize :: from ( 16 ) ) ,
318
- Edit :: deletion ( TextSize :: from ( 22 ) , TextSize :: from ( 30 ) ) ,
319
- ] ) ;
338
+ let diagnostics = create_diagnostics (
339
+ "<filename>" ,
340
+ locator. contents ( ) ,
341
+ [
342
+ Edit :: deletion ( TextSize :: from ( 8 ) , TextSize :: from ( 16 ) ) ,
343
+ Edit :: deletion ( TextSize :: from ( 22 ) , TextSize :: from ( 30 ) ) ,
344
+ ] ,
345
+ ) ;
320
346
let FixResult {
321
347
code,
322
348
fixes,
@@ -352,10 +378,14 @@ class A(object):
352
378
"
353
379
. trim ( ) ,
354
380
) ;
355
- let diagnostics = create_diagnostics ( [
356
- Edit :: deletion ( TextSize :: from ( 7 ) , TextSize :: from ( 15 ) ) ,
357
- Edit :: replacement ( "ignored" . to_string ( ) , TextSize :: from ( 9 ) , TextSize :: from ( 11 ) ) ,
358
- ] ) ;
381
+ let diagnostics = create_diagnostics (
382
+ "<filename>" ,
383
+ locator. contents ( ) ,
384
+ [
385
+ Edit :: deletion ( TextSize :: from ( 7 ) , TextSize :: from ( 15 ) ) ,
386
+ Edit :: replacement ( "ignored" . to_string ( ) , TextSize :: from ( 9 ) , TextSize :: from ( 11 ) ) ,
387
+ ] ,
388
+ ) ;
359
389
let FixResult {
360
390
code,
361
391
fixes,
0 commit comments