mirror of
https://github.com/astral-sh/ruff
synced 2026-01-22 14:00:51 -05:00
## Summary
It turns out that _some_ identifiers can contain newlines --
specifically, dot-delimited import identifiers, like:
```python
import foo\
.bar
```
At present, we print all identifiers verbatim, which causes us to retain
the `\` in the formatted output. This also leads to violating some debug
assertions (see the linked issue, though that's a symptom of this
formatting failure).
This PR adds detection for import identifiers that contain newlines, and
formats them via `text` (slow) rather than `source_code_slice` (fast) in
those cases.
Closes https://github.com/astral-sh/ruff/issues/7734.
## Test Plan
`cargo test`
24 lines
600 B
Rust
24 lines
600 B
Rust
use ruff_formatter::write;
|
|
use ruff_python_ast::Alias;
|
|
|
|
use crate::other::identifier::DotDelimitedIdentifier;
|
|
use crate::prelude::*;
|
|
|
|
#[derive(Default)]
|
|
pub struct FormatAlias;
|
|
|
|
impl FormatNodeRule<Alias> for FormatAlias {
|
|
fn fmt_fields(&self, item: &Alias, f: &mut PyFormatter) -> FormatResult<()> {
|
|
let Alias {
|
|
range: _,
|
|
name,
|
|
asname,
|
|
} = item;
|
|
DotDelimitedIdentifier::new(name).fmt(f)?;
|
|
if let Some(asname) = asname {
|
|
write!(f, [space(), token("as"), space(), asname.format()])?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|