Skip to content

Commit 5f51ad2

Browse files
committed
[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
1 parent caf48f4 commit 5f51ad2

File tree

1 file changed

+13
-1
lines changed
  • crates/ruff_python_formatter/src/other

1 file changed

+13
-1
lines changed

crates/ruff_python_formatter/src/other/alias.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use ruff_formatter::write;
22
use ruff_python_ast::Alias;
33

4+
use crate::comments::trailing_comments;
45
use crate::other::identifier::DotDelimitedIdentifier;
56
use crate::prelude::*;
67

@@ -17,7 +18,18 @@ impl FormatNodeRule<Alias> for FormatAlias {
1718
} = item;
1819
DotDelimitedIdentifier::new(name).fmt(f)?;
1920
if let Some(asname) = asname {
20-
write!(f, [space(), token("as"), space(), asname.format()])?;
21+
let comments = f.context().comments().clone();
22+
let dangling = comments.dangling(item);
23+
write!(
24+
f,
25+
[
26+
space(),
27+
token("as"),
28+
space(),
29+
asname.format(),
30+
trailing_comments(dangling),
31+
]
32+
)?;
2133
}
2234
Ok(())
2335
}

0 commit comments

Comments
 (0)