Format import statements (#5493)

## Summary

Format import statements in all their variants. Specifically, this
implemented formatting `StmtImport`, `StmtImportFrom` and `Alias`.

## Test Plan

I added some custom snapshots, even though this has been covered well by
black's tests.
This commit is contained in:
konsti 2023-07-04 09:07:20 +02:00 committed by GitHub
parent 6acc316d19
commit 787e2fd49d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 304 additions and 991 deletions

View file

@ -1,5 +1,6 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, Format, FormatResult};
use rustpython_parser::ast::Alias;
#[derive(Default)]
@ -7,6 +8,15 @@ pub struct FormatAlias;
impl FormatNodeRule<Alias> for FormatAlias {
fn fmt_fields(&self, item: &Alias, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let Alias {
range: _,
name,
asname,
} = item;
name.format().fmt(f)?;
if let Some(asname) = asname {
write!(f, [space(), text("as"), space(), asname.format()])?;
}
Ok(())
}
}