mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:51:30 +00:00
Compare formatted and unformatted ASTs during formatter tests (#8624)
## Summary This PR implements validation in the formatter tests to ensure that we don't modify the AST during formatting. Black has similar logic. In implementing this, I learned that Black actually _does_ modify the AST, and their test infrastructure normalizes the AST to wipe away those differences. Specifically, Black changes the indentation of docstrings, which _does_ modify the AST; and it also inserts parentheses in `del` statements, which changes the AST too. Ruff also does both these things, so we _also_ implement the same normalization using a new visitor that allows for modifying the AST. Closes https://github.com/astral-sh/ruff/issues/8184. ## Test Plan `cargo test`
This commit is contained in:
parent
3592f44ade
commit
d574fcd1ac
5 changed files with 930 additions and 19 deletions
|
@ -1480,3 +1480,44 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableMod<'a> {
|
||||
Module(ComparableModModule<'a>),
|
||||
Expression(ComparableModExpression<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableModModule<'a> {
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableModExpression<'a> {
|
||||
body: Box<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::Mod> for ComparableMod<'a> {
|
||||
fn from(mod_: &'a ast::Mod) -> Self {
|
||||
match mod_ {
|
||||
ast::Mod::Module(module) => Self::Module(module.into()),
|
||||
ast::Mod::Expression(expr) => Self::Expression(expr.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::ModModule> for ComparableModModule<'a> {
|
||||
fn from(module: &'a ast::ModModule) -> Self {
|
||||
Self {
|
||||
body: module.body.iter().map(Into::into).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::ModExpression> for ComparableModExpression<'a> {
|
||||
fn from(expr: &'a ast::ModExpression) -> Self {
|
||||
Self {
|
||||
body: (&expr.body).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue