Skip to content

Commit

Permalink
Add noqa_row to diagnostics JSON format (#3228)
Browse files Browse the repository at this point in the history
In ruff-lsp (astral-sh/ruff-lsp#76) we want to add a "Disable \<rule\> 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.
  • Loading branch information
bluetech authored Feb 25, 2023
1 parent cd9fbeb commit 33c31cd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
13 changes: 11 additions & 2 deletions crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down Expand Up @@ -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()
}),
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ pub struct Message {
pub fix: Option<Fix>,
pub filename: String,
pub source: Option<Source>,
pub noqa_row: usize,
}

impl Message {
pub fn from_diagnostic(
diagnostic: Diagnostic,
filename: String,
source: Option<Source>,
noqa_row: usize,
) -> Self {
Self {
kind: diagnostic.kind,
Expand All @@ -34,6 +36,7 @@ impl Message {
fix: diagnostic.fix,
filename,
source,
noqa_row,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub fn run(
),
format!("{}", path.display()),
None,
1,
)])
} else {
Diagnostics::default()
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_cli/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct ExpandedMessage<'a> {
location: Location,
end_location: Location,
filename: &'a str,
noqa_row: usize,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -197,6 +198,7 @@ impl Printer {
location: message.location,
end_location: message.end_location,
filename: &message.filename,
noqa_row: message.noqa_row,
})
.collect::<Vec<_>>()
)?
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ fn test_stdin_json() -> Result<()> {
"row": 1,
"column": 10
}},
"filename": "{file_path}"
"filename": "{file_path}",
"noqa_row": 1
}}
]
"#
Expand Down

0 comments on commit 33c31cd

Please sign in to comment.