Show syntax errors on the playground (#12083)

## Summary

This PR updates the playground to show syntax errors.

(I forgot to update this and noticed it this morning.)

## Test Plan

Build the playground locally and preview it:

<img width="764" alt="Screenshot 2024-06-28 at 11 03 35"
src="1fd48d6c-ae41-4672-bf3c-32a61d9946ef">
This commit is contained in:
Dhruv Manilawala 2024-06-28 13:06:15 +05:30 committed by GitHub
parent 526efd398a
commit 9fec384d11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 11 deletions

View file

@ -28,7 +28,7 @@ use ruff_workspace::Settings;
#[wasm_bindgen(typescript_custom_section)]
const TYPES: &'static str = r#"
export interface Diagnostic {
code: string;
code: string | null;
message: string;
location: {
row: number;
@ -57,7 +57,7 @@ export interface Diagnostic {
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct ExpandedMessage {
pub code: String,
pub code: Option<String>,
pub message: String,
pub location: SourceLocation,
pub end_location: SourceLocation,
@ -199,17 +199,17 @@ impl Workspace {
let messages: Vec<ExpandedMessage> = diagnostics
.into_iter()
.map(|message| {
let start_location = source_code.source_location(message.start());
let end_location = source_code.source_location(message.end());
.map(|diagnostic| {
let start_location = source_code.source_location(diagnostic.start());
let end_location = source_code.source_location(diagnostic.end());
ExpandedMessage {
code: message.kind.rule().noqa_code().to_string(),
message: message.kind.body,
code: Some(diagnostic.kind.rule().noqa_code().to_string()),
message: diagnostic.kind.body,
location: start_location,
end_location,
fix: message.fix.map(|fix| ExpandedFix {
message: message.kind.suggestion,
fix: diagnostic.fix.map(|fix| ExpandedFix {
message: diagnostic.kind.suggestion,
edits: fix
.edits()
.iter()
@ -222,6 +222,18 @@ impl Workspace {
}),
}
})
.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,
}
}))
.collect();
serde_wasm_bindgen::to_value(&messages).map_err(into_error)