Skip to content

Commit 1c9f5e3

Browse files
authored
Display the AST even with syntax errors (#11147)
## Summary This PR updates the playground to display the AST even if it contains a syntax error. This could be useful for development and also to give a quick preview of what error recovery looks like. Note that not all recovery is correct but this allows us to iterate quickly on what can be improved. ## Test Plan Build the playground locally and test it. <img width="1688" alt="Screenshot 2024-04-25 at 21 02 22" src="https://github.com/astral-sh/ruff/assets/67177269/2b94934c-4f2c-4a9a-9693-3d8460ed9d0b">
1 parent 263a0d2 commit 1c9f5e3

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

crates/ruff_wasm/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ruff_python_codegen::Stylist;
1818
use ruff_python_formatter::{format_module_ast, pretty_comments, PyFormatContext, QuoteStyle};
1919
use ruff_python_index::{CommentRangesBuilder, Indexer};
2020
use ruff_python_parser::lexer::LexResult;
21-
use ruff_python_parser::{parse_tokens, tokenize_all, AsMode, Mode};
21+
use ruff_python_parser::{parse_tokens, tokenize_all, AsMode, Mode, Program};
2222
use ruff_python_trivia::CommentRanges;
2323
use ruff_source_file::{Locator, SourceLocation};
2424
use ruff_text_size::Ranged;
@@ -250,9 +250,9 @@ impl Workspace {
250250

251251
/// Parses the content and returns its AST
252252
pub fn parse(&self, contents: &str) -> Result<String, Error> {
253-
let parsed = ruff_python_parser::parse(contents, Mode::Module).map_err(into_error)?;
253+
let program = Program::parse_str(contents, Mode::Module);
254254

255-
Ok(format!("{parsed:#?}"))
255+
Ok(format!("{:#?}", program.into_ast()))
256256
}
257257

258258
pub fn tokens(&self, contents: &str) -> Result<String, Error> {

0 commit comments

Comments
 (0)