Skip to content

Commit e9b0c33

Browse files
ntBreMichaReiser
andauthored
Move RDJSON rendering to ruff_db (#19293)
## Summary Another output format like #19133. This is the [reviewdog](https://github.com/reviewdog/reviewdog) output format, which is somewhat similar to regular JSON. Like #19270, in the first commit I converted from using `json!` to `Serialize` structs, then in the second commit I moved the module to `ruff_db`. The reviewdog [schema](https://github.com/reviewdog/reviewdog/blob/320a8e73a94a09248044314d8ca326a6cd710692/proto/rdf/jsonschema/DiagnosticResult.json) seems a bit more flexible than our JSON schema, so I'm not sure if we need any preview checks here. I'll flag the places I wasn't sure about as review comments. ## Test Plan New tests in `rdjson.rs`, ported from the old `rjdson.rs` module, as well as the new CLI output tests. --------- Co-authored-by: Micha Reiser <micha@reiser.io>
1 parent 82391b5 commit e9b0c33

12 files changed

+317
-214
lines changed

crates/ruff/src/printer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ruff_linter::fs::relativize_path;
1616
use ruff_linter::logging::LogLevel;
1717
use ruff_linter::message::{
1818
Emitter, EmitterContext, GithubEmitter, GitlabEmitter, GroupedEmitter, JunitEmitter,
19-
PylintEmitter, RdjsonEmitter, SarifEmitter, TextEmitter,
19+
PylintEmitter, SarifEmitter, TextEmitter,
2020
};
2121
use ruff_linter::notify_user;
2222
use ruff_linter::settings::flags::{self};
@@ -238,7 +238,11 @@ impl Printer {
238238
write!(writer, "{value}")?;
239239
}
240240
OutputFormat::Rdjson => {
241-
RdjsonEmitter.emit(writer, &diagnostics.inner, &context)?;
241+
let config = DisplayDiagnosticConfig::default()
242+
.format(DiagnosticFormat::Rdjson)
243+
.preview(preview);
244+
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
245+
write!(writer, "{value}")?;
242246
}
243247
OutputFormat::JsonLines => {
244248
let config = DisplayDiagnosticConfig::default()

crates/ruff/tests/snapshots/lint__output_format_rdjson.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ exit_code: 1
7575
},
7676
{
7777
"code": {
78-
"url": null,
79-
"value": null
78+
"value": "invalid-syntax"
8079
},
8180
"location": {
8281
"path": "[TMP]/input.py",
@@ -94,7 +93,7 @@ exit_code: 1
9493
"message": "SyntaxError: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)"
9594
}
9695
],
97-
"severity": "warning",
96+
"severity": "WARNING",
9897
"source": {
9998
"name": "ruff",
10099
"url": "https://docs.astral.sh/ruff"

crates/ruff_db/src/diagnostic/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ impl Diagnostic {
308308

309309
/// Set the fix for this diagnostic.
310310
pub fn set_fix(&mut self, fix: Fix) {
311+
debug_assert!(
312+
self.primary_span().is_some(),
313+
"Expected a source file for a diagnostic with a fix"
314+
);
311315
Arc::make_mut(&mut self.inner).fix = Some(fix);
312316
}
313317

@@ -1259,6 +1263,11 @@ pub enum DiagnosticFormat {
12591263
/// format for an array of all diagnostics. See <https://jsonlines.org/> for more details.
12601264
#[cfg(feature = "serde")]
12611265
JsonLines,
1266+
/// Print diagnostics in the JSON format expected by [reviewdog].
1267+
///
1268+
/// [reviewdog]: https://github.com/reviewdog/reviewdog
1269+
#[cfg(feature = "serde")]
1270+
Rdjson,
12621271
}
12631272

12641273
/// A representation of the kinds of messages inside a diagnostic.

crates/ruff_db/src/diagnostic/render.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ mod azure;
2828
mod json;
2929
#[cfg(feature = "serde")]
3030
mod json_lines;
31+
#[cfg(feature = "serde")]
32+
mod rdjson;
3133

3234
/// A type that implements `std::fmt::Display` for diagnostic rendering.
3335
///
@@ -184,6 +186,10 @@ impl std::fmt::Display for DisplayDiagnostics<'_> {
184186
json_lines::JsonLinesRenderer::new(self.resolver, self.config)
185187
.render(f, self.diagnostics)?;
186188
}
189+
#[cfg(feature = "serde")]
190+
DiagnosticFormat::Rdjson => {
191+
rdjson::RdjsonRenderer::new(self.resolver).render(f, self.diagnostics)?;
192+
}
187193
}
188194

189195
Ok(())

crates/ruff_db/src/diagnostic/render/json.rs

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,6 @@ struct JsonEdit<'a> {
262262

263263
#[cfg(test)]
264264
mod tests {
265-
use ruff_diagnostics::{Edit, Fix};
266-
use ruff_text_size::TextSize;
267-
268265
use crate::diagnostic::{
269266
DiagnosticFormat,
270267
render::tests::{
@@ -297,13 +294,7 @@ mod tests {
297294
env.format(DiagnosticFormat::Json);
298295
env.preview(false);
299296

300-
let diag = env
301-
.err()
302-
.fix(Fix::safe_edit(Edit::insertion(
303-
"edit".to_string(),
304-
TextSize::from(0),
305-
)))
306-
.build();
297+
let diag = env.err().build();
307298

308299
insta::assert_snapshot!(
309300
env.render(&diag),
@@ -317,23 +308,7 @@ mod tests {
317308
"row": 1
318309
},
319310
"filename": "",
320-
"fix": {
321-
"applicability": "safe",
322-
"edits": [
323-
{
324-
"content": "edit",
325-
"end_location": {
326-
"column": 1,
327-
"row": 1
328-
},
329-
"location": {
330-
"column": 1,
331-
"row": 1
332-
}
333-
}
334-
],
335-
"message": null
336-
},
311+
"fix": null,
337312
"location": {
338313
"column": 1,
339314
"row": 1
@@ -353,13 +328,7 @@ mod tests {
353328
env.format(DiagnosticFormat::Json);
354329
env.preview(true);
355330

356-
let diag = env
357-
.err()
358-
.fix(Fix::safe_edit(Edit::insertion(
359-
"edit".to_string(),
360-
TextSize::from(0),
361-
)))
362-
.build();
331+
let diag = env.err().build();
363332

364333
insta::assert_snapshot!(
365334
env.render(&diag),
@@ -370,17 +339,7 @@ mod tests {
370339
"code": null,
371340
"end_location": null,
372341
"filename": null,
373-
"fix": {
374-
"applicability": "safe",
375-
"edits": [
376-
{
377-
"content": "edit",
378-
"end_location": null,
379-
"location": null
380-
}
381-
],
382-
"message": null
383-
},
342+
"fix": null,
384343
"location": null,
385344
"message": "main diagnostic message",
386345
"noqa_row": null,

0 commit comments

Comments
 (0)