Fix formatter with-statement after-as own line comment instability (#6033)

**Summary** Fix an instability in with statement formatter when there is
an own line comment as the `as`
```python
with (
    a as
    # bad comment
    b):
```

**Test Plan** Added the comment to the test cases.
This commit is contained in:
konsti 2023-07-24 20:12:07 +02:00 committed by GitHub
parent a9f535997d
commit 7f3797185c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 11 deletions

View file

@ -1370,15 +1370,15 @@ fn handle_with_item_comment<'a>(
SimpleTokenKind::As,
);
// If before the `as` keyword, then it must be a trailing comment of the context expression.
if comment.end() < as_token.start() {
// If before the `as` keyword, then it must be a trailing comment of the context expression.
CommentPlacement::trailing(context_expr, comment)
}
// Trailing end of line comment coming after the `as` keyword`.
else if comment.line_position().is_end_of_line() {
CommentPlacement::dangling(comment.enclosing_node(), comment)
} else {
CommentPlacement::Default(comment)
CommentPlacement::leading(optional_vars, comment)
}
}