From 64c4ccaa9dc8674796a07fc6e033a18e7224cacb Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 10 May 2023 11:24:16 +0200 Subject: [PATCH] Add `Fix::applicability` to JSON output Add the `Fix::applicability` to the JSON output. I'll use the presence of the new field to detect whether this ruff version emits one-based indices for edits or not. --- crates/ruff/src/message/json.rs | 5 +++-- crates/ruff/src/message/mod.rs | 3 +-- .../snapshots/ruff__message__json__tests__output.snap | 2 ++ crates/ruff_cli/tests/integration_test.rs | 1 + 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/ruff/src/message/json.rs b/crates/ruff/src/message/json.rs index ebb12053cdb76..73e4914823838 100644 --- a/crates/ruff/src/message/json.rs +++ b/crates/ruff/src/message/json.rs @@ -39,8 +39,9 @@ impl Serialize for ExpandedMessages<'_> { let fix = message.fix.as_ref().map(|fix| { json!({ - "message": message.kind.suggestion.as_deref(), - "edits": &ExpandedEdits { edits: fix.edits(), source_code: &source_code }, + "applicability": fix.applicability(), + "message": message.kind.suggestion.as_deref(), + "edits": &ExpandedEdits { edits: fix.edits(), source_code: &source_code }, }) }); diff --git a/crates/ruff/src/message/mod.rs b/crates/ruff/src/message/mod.rs index 6cc9061415e2f..82f063ad6c370 100644 --- a/crates/ruff/src/message/mod.rs +++ b/crates/ruff/src/message/mod.rs @@ -189,14 +189,13 @@ def fibonacci(n): let fib_source = SourceFileBuilder::new("fib.py", fib).finish(); - #[allow(deprecated)] let unused_variable = Diagnostic::new( UnusedVariable { name: "x".to_string(), }, TextRange::new(TextSize::from(94), TextSize::from(95)), ) - .with_fix(Fix::unspecified(Edit::deletion( + .with_fix(Fix::suggested(Edit::deletion( TextSize::from(94), TextSize::from(99), ))); diff --git a/crates/ruff/src/message/snapshots/ruff__message__json__tests__output.snap b/crates/ruff/src/message/snapshots/ruff__message__json__tests__output.snap index 7afb9cdb7340f..6d44ff64af2e3 100644 --- a/crates/ruff/src/message/snapshots/ruff__message__json__tests__output.snap +++ b/crates/ruff/src/message/snapshots/ruff__message__json__tests__output.snap @@ -7,6 +7,7 @@ expression: content "code": "F401", "message": "`os` imported but unused", "fix": { + "applicability": "Suggested", "message": "Remove unused import: `os`", "edits": [ { @@ -37,6 +38,7 @@ expression: content "code": "F841", "message": "Local variable `x` is assigned to but never used", "fix": { + "applicability": "Suggested", "message": "Remove assignment to unused variable `x`", "edits": [ { diff --git a/crates/ruff_cli/tests/integration_test.rs b/crates/ruff_cli/tests/integration_test.rs index 075c0f1a9df26..c6c761100bdb7 100644 --- a/crates/ruff_cli/tests/integration_test.rs +++ b/crates/ruff_cli/tests/integration_test.rs @@ -93,6 +93,7 @@ fn test_stdin_json() -> Result<()> { "code": "F401", "message": "`os` imported but unused", "fix": {{ + "applicability": "Unspecified", "message": "Remove unused import: `os`", "edits": [ {{