mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 13:30:49 -05:00
## Summary I always found it odd that we had to pass this in, since it's really higher-level context for the error. The awkwardness is further evidenced by the fact that we pass in fake values everywhere (even outside of tests). The source path isn't actually used to display the error; it's only accessed elsewhere to _re-display_ the error in certain cases. This PR modifies to instead pass the path directly in those cases.
19 lines
650 B
Rust
19 lines
650 B
Rust
mod generator;
|
|
mod stylist;
|
|
|
|
pub use generator::Generator;
|
|
use ruff_python_parser::{lexer, parse_suite, Mode, ParseError};
|
|
use ruff_source_file::Locator;
|
|
pub use stylist::{Quote, Stylist};
|
|
|
|
/// Run round-trip source code generation on a given Python code.
|
|
pub fn round_trip(code: &str) -> Result<String, ParseError> {
|
|
let locator = Locator::new(code);
|
|
let python_ast = parse_suite(code)?;
|
|
let tokens: Vec<_> = lexer::lex(code, Mode::Module).collect();
|
|
let stylist = Stylist::from_tokens(&tokens, &locator);
|
|
let mut generator: Generator = (&stylist).into();
|
|
generator.unparse_suite(&python_ast);
|
|
Ok(generator.generate())
|
|
}
|