mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +00:00
[ruff] fix crash with dangling comments in importfrom alias
Resolves the crash when attempting to format code like: ``` from x import (a as # whatever b) ``` And chooses to format it as: ``` from x import ( a as b, # whatever ) ``` Fixes issue #19138
This commit is contained in:
parent
09f570af92
commit
63b3f7adae
1 changed files with 13 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
||||||
use ruff_formatter::write;
|
use ruff_formatter::write;
|
||||||
use ruff_python_ast::Alias;
|
use ruff_python_ast::Alias;
|
||||||
|
|
||||||
|
use crate::comments::dangling_comments;
|
||||||
use crate::other::identifier::DotDelimitedIdentifier;
|
use crate::other::identifier::DotDelimitedIdentifier;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
@ -17,7 +18,18 @@ impl FormatNodeRule<Alias> for FormatAlias {
|
||||||
} = item;
|
} = item;
|
||||||
DotDelimitedIdentifier::new(name).fmt(f)?;
|
DotDelimitedIdentifier::new(name).fmt(f)?;
|
||||||
if let Some(asname) = asname {
|
if let Some(asname) = asname {
|
||||||
write!(f, [space(), token("as"), space(), asname.format()])?;
|
let comments = f.context().comments().clone();
|
||||||
|
let dangling = comments.dangling(item);
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
[
|
||||||
|
space(),
|
||||||
|
token("as"),
|
||||||
|
space(),
|
||||||
|
asname.format(),
|
||||||
|
line_suffix(&dangling_comments(dangling), 0),
|
||||||
|
]
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue