From 33c31cda27d30449d02e4122b76ed26fb18cd13e Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sun, 26 Feb 2023 01:13:16 +0200 Subject: [PATCH] Add `noqa_row` to diagnostics JSON format (#3228) In ruff-lsp (https://github.com/charliermarsh/ruff-lsp/pull/76) we want to add a "Disable \ for this line" quickfix. However, finding the correct line into which the `noqa` comment should be inserted is non-trivial (multi-line strings for example). Ruff already has this info, so expose it in the JSON output for use by ruff-lsp. --- crates/ruff/src/linter.rs | 13 +++++++++++-- crates/ruff/src/message.rs | 3 +++ crates/ruff_cli/src/commands/run.rs | 1 + crates/ruff_cli/src/printer.rs | 2 ++ crates/ruff_cli/tests/integration_test.rs | 3 ++- 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/ruff/src/linter.rs b/crates/ruff/src/linter.rs index f98b4a5b28cd6..7e7b3b607cc6b 100644 --- a/crates/ruff/src/linter.rs +++ b/crates/ruff/src/linter.rs @@ -331,7 +331,9 @@ pub fn lint_only( } else { None }; - Message::from_diagnostic(diagnostic, path_lossy.to_string(), source) + let lineno = diagnostic.location.row(); + let noqa_row = *directives.noqa_line_for.get(&lineno).unwrap_or(&lineno); + Message::from_diagnostic(diagnostic, path_lossy.to_string(), source, noqa_row) }) .collect() }) @@ -469,7 +471,14 @@ This indicates a bug in `{}`. If you could open an issue at: } else { None }; - Message::from_diagnostic(diagnostic, path_lossy.to_string(), source) + let lineno = diagnostic.location.row(); + let noqa_row = *directives.noqa_line_for.get(&lineno).unwrap_or(&lineno); + Message::from_diagnostic( + diagnostic, + path_lossy.to_string(), + source, + noqa_row, + ) }) .collect() }), diff --git a/crates/ruff/src/message.rs b/crates/ruff/src/message.rs index c143d16fe61c9..f5d51163dfb05 100644 --- a/crates/ruff/src/message.rs +++ b/crates/ruff/src/message.rs @@ -16,6 +16,7 @@ pub struct Message { pub fix: Option, pub filename: String, pub source: Option, + pub noqa_row: usize, } impl Message { @@ -23,6 +24,7 @@ impl Message { diagnostic: Diagnostic, filename: String, source: Option, + noqa_row: usize, ) -> Self { Self { kind: diagnostic.kind, @@ -34,6 +36,7 @@ impl Message { fix: diagnostic.fix, filename, source, + noqa_row, } } } diff --git a/crates/ruff_cli/src/commands/run.rs b/crates/ruff_cli/src/commands/run.rs index f2f2a7c37c2d1..334c4b4093214 100644 --- a/crates/ruff_cli/src/commands/run.rs +++ b/crates/ruff_cli/src/commands/run.rs @@ -114,6 +114,7 @@ pub fn run( ), format!("{}", path.display()), None, + 1, )]) } else { Diagnostics::default() diff --git a/crates/ruff_cli/src/printer.rs b/crates/ruff_cli/src/printer.rs index 38f7dc24d5ba7..ea9173ce75a8f 100644 --- a/crates/ruff_cli/src/printer.rs +++ b/crates/ruff_cli/src/printer.rs @@ -50,6 +50,7 @@ struct ExpandedMessage<'a> { location: Location, end_location: Location, filename: &'a str, + noqa_row: usize, } #[derive(Serialize)] @@ -197,6 +198,7 @@ impl Printer { location: message.location, end_location: message.end_location, filename: &message.filename, + noqa_row: message.noqa_row, }) .collect::>() )? diff --git a/crates/ruff_cli/tests/integration_test.rs b/crates/ruff_cli/tests/integration_test.rs index a628517c247e8..132a6acb9c1dc 100644 --- a/crates/ruff_cli/tests/integration_test.rs +++ b/crates/ruff_cli/tests/integration_test.rs @@ -112,7 +112,8 @@ fn test_stdin_json() -> Result<()> { "row": 1, "column": 10 }}, - "filename": "{file_path}" + "filename": "{file_path}", + "noqa_row": 1 }} ] "#