mirror of
https://github.com/astral-sh/ruff
synced 2026-01-08 15:14:19 -05:00
Remove source path from parser errors (#9322)
## 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.
This commit is contained in:
@@ -1416,7 +1416,7 @@ mod tests {
|
||||
let indentation = Indentation::default();
|
||||
let quote = Quote::default();
|
||||
let line_ending = LineEnding::default();
|
||||
let stmt = parse_suite(contents, "<filename>").unwrap();
|
||||
let stmt = parse_suite(contents).unwrap();
|
||||
let mut generator = Generator::new(&indentation, quote, line_ending);
|
||||
generator.unparse_suite(&stmt);
|
||||
generator.generate()
|
||||
@@ -1428,7 +1428,7 @@ mod tests {
|
||||
line_ending: LineEnding,
|
||||
contents: &str,
|
||||
) -> String {
|
||||
let stmt = parse_suite(contents, "<filename>").unwrap();
|
||||
let stmt = parse_suite(contents).unwrap();
|
||||
let mut generator = Generator::new(indentation, quote, line_ending);
|
||||
generator.unparse_suite(&stmt);
|
||||
generator.generate()
|
||||
@@ -1438,7 +1438,7 @@ mod tests {
|
||||
let indentation = Indentation::default();
|
||||
let quote = Quote::default();
|
||||
let line_ending = LineEnding::default();
|
||||
let ast = ruff_python_parser::parse(contents, Mode::Ipython, "<filename>").unwrap();
|
||||
let ast = ruff_python_parser::parse(contents, Mode::Ipython).unwrap();
|
||||
let Mod::Module(ModModule { body, .. }) = ast else {
|
||||
panic!("Source code didn't return ModModule")
|
||||
};
|
||||
|
||||
@@ -7,9 +7,9 @@ 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, source_path: &str) -> Result<String, ParseError> {
|
||||
pub fn round_trip(code: &str) -> Result<String, ParseError> {
|
||||
let locator = Locator::new(code);
|
||||
let python_ast = parse_suite(code, source_path)?;
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user