[internal] Return Messages from check_path (#16837)

Summary
--

This PR updates `check_path` in the `ruff_linter` crate to return a
`Vec<Message>` instead of a `Vec<Diagnostic>`. The main motivation for
this is to make it easier to convert semantic syntax errors directly
into `Message`s rather than `Diagnostic`s in #16106. However, this also
has the benefit of keeping the preview check on unsupported syntax
errors in `check_path`, as suggested in
https://github.com/astral-sh/ruff/pull/16429#discussion_r1974748024.

All of the interesting changes are in the first commit. The second
commit just renames variables like `diagnostics` to `messages`, and the
third commit is a tiny import fix.

I also updated the `ExpandedMessage::location` field name, which caused
a few extra commits tidying up the playground code. I thought it was
nicely symmetric with `end_location`, but I'm happy to revert that too.

Test Plan
--

Existing tests. I also tested the playground and server manually.
This commit is contained in:
Brent Westbrook 2025-03-19 10:08:07 -04:00 committed by GitHub
parent f2a9960fb3
commit 22de00de16
13 changed files with 280 additions and 294 deletions

View file

@ -1,6 +1,7 @@
use std::path::Path;
use js_sys::Error;
use ruff_linter::message::{DiagnosticMessage, Message, SyntaxErrorMessage};
use ruff_linter::settings::types::PythonVersion;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
@ -31,7 +32,7 @@ const TYPES: &'static str = r#"
export interface Diagnostic {
code: string | null;
message: string;
location: {
start_location: {
row: number;
column: number;
};
@ -60,7 +61,7 @@ export interface Diagnostic {
pub struct ExpandedMessage {
pub code: Option<String>,
pub message: String,
pub location: SourceLocation,
pub start_location: SourceLocation,
pub end_location: SourceLocation,
pub fix: Option<ExpandedFix>,
}
@ -188,7 +189,7 @@ impl Workspace {
);
// Generate checks.
let diagnostics = check_path(
let messages = check_path(
Path::new("<filename>"),
None,
&locator,
@ -205,25 +206,18 @@ impl Workspace {
let source_code = locator.to_source_code();
let unsupported_syntax_errors = if self.settings.linter.preview.is_enabled() {
parsed.unsupported_syntax_errors()
} else {
&[]
};
let messages: Vec<ExpandedMessage> = diagnostics
let messages: Vec<ExpandedMessage> = messages
.into_iter()
.map(|diagnostic| {
let start_location = source_code.source_location(diagnostic.start());
let end_location = source_code.source_location(diagnostic.end());
ExpandedMessage {
code: Some(diagnostic.kind.rule().noqa_code().to_string()),
message: diagnostic.kind.body,
location: start_location,
end_location,
fix: diagnostic.fix.map(|fix| ExpandedFix {
message: diagnostic.kind.suggestion,
.map(|message| match message {
Message::Diagnostic(DiagnosticMessage {
kind, range, fix, ..
}) => ExpandedMessage {
code: Some(kind.rule().noqa_code().to_string()),
message: kind.body,
start_location: source_code.source_location(range.start()),
end_location: source_code.source_location(range.end()),
fix: fix.map(|fix| ExpandedFix {
message: kind.suggestion,
edits: fix
.edits()
.iter()
@ -234,32 +228,17 @@ impl Workspace {
})
.collect(),
}),
},
Message::SyntaxError(SyntaxErrorMessage { message, range, .. }) => {
ExpandedMessage {
code: None,
message,
start_location: source_code.source_location(range.start()),
end_location: source_code.source_location(range.end()),
fix: None,
}
}
})
.chain(parsed.errors().iter().map(|parse_error| {
let start_location = source_code.source_location(parse_error.location.start());
let end_location = source_code.source_location(parse_error.location.end());
ExpandedMessage {
code: None,
message: format!("SyntaxError: {}", parse_error.error),
location: start_location,
end_location,
fix: None,
}
}))
.chain(unsupported_syntax_errors.iter().map(|error| {
let start_location = source_code.source_location(error.range.start());
let end_location = source_code.source_location(error.range.end());
ExpandedMessage {
code: None,
message: format!("SyntaxError: {error}"),
location: start_location,
end_location,
fix: None,
}
}))
.collect();
serde_wasm_bindgen::to_value(&messages).map_err(into_error)