Skip to content

Commit

Permalink
Fix panic with empty attribute inner comment
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszotten committed Aug 4, 2023
1 parent 8a5bc93 commit d00d924
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,6 @@
a.b
)


# regression: https://github.com/astral-sh/ruff/issues/6181
(#
()).a
12 changes: 8 additions & 4 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,10 +998,14 @@ fn handle_attribute_comment<'a>(
comment: DecoratedComment<'a>,
attribute: &'a ast::ExprAttribute,
) -> CommentPlacement<'a> {
debug_assert!(
comment.preceding_node().is_some(),
"The enclosing node an attribute expression, expected to be at least after the name"
);
if comment.preceding_node().is_none() {
// ```text
// ( value) . attr
// ^^^^ we're in this range
// ```

return CommentPlacement::leading(attribute.value.as_ref(), comment);
}

// ```text
// value . attr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ x6 = (
a.b
)
# regression: https://github.com/astral-sh/ruff/issues/6181
(#
()).a
```

## Output
Expand Down Expand Up @@ -200,6 +202,12 @@ x6 = (
# Check assumption with enclosing nodes
a.b
)
# regression: https://github.com/astral-sh/ruff/issues/6181
(
#
()
).a
```


Expand Down

0 comments on commit d00d924

Please sign in to comment.