mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
Skip over parentheses when detecting in
keyword (#8054)
## Summary Given an expression like `[x for (x) in y]`, we weren't skipping over parentheses when searching for the `in` between `(x)` and `y`. Closes https://github.com/astral-sh/ruff/issues/8053.
This commit is contained in:
parent
b2d1fcf7b2
commit
2729c4cacd
3 changed files with 17 additions and 20 deletions
|
@ -102,3 +102,6 @@ aaaaaaaaaaaaaaaaaaaaa = [
|
|||
c # negative decimal
|
||||
]
|
||||
|
||||
# Parenthesized targets and iterators.
|
||||
[x for (x) in y]
|
||||
[x for x in (y)]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use ruff_formatter::{format_args, write, Buffer, FormatError, FormatResult};
|
||||
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
||||
use ruff_python_ast::{Comprehension, Expr};
|
||||
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
|
||||
use ruff_python_trivia::{find_only_token_in_range, SimpleTokenKind};
|
||||
use ruff_text_size::{Ranged, TextRange};
|
||||
|
||||
use crate::comments::{leading_comments, trailing_comments, SourceComment};
|
||||
|
@ -42,27 +42,14 @@ impl FormatNodeRule<Comprehension> for FormatComprehension {
|
|||
dangling_item_comments.partition_point(|comment| comment.end() < target.start()),
|
||||
);
|
||||
|
||||
let maybe_in_token = SimpleTokenizer::new(
|
||||
f.context().source(),
|
||||
let in_token = find_only_token_in_range(
|
||||
TextRange::new(target.end(), iter.start()),
|
||||
)
|
||||
.skip_trivia()
|
||||
.next();
|
||||
|
||||
let Some(
|
||||
in_keyword @ SimpleToken {
|
||||
kind: SimpleTokenKind::In,
|
||||
..
|
||||
},
|
||||
) = maybe_in_token
|
||||
else {
|
||||
return Err(FormatError::syntax_error(
|
||||
"Expected `in` keyword between the `target` and `iter`.",
|
||||
));
|
||||
};
|
||||
SimpleTokenKind::In,
|
||||
f.context().source(),
|
||||
);
|
||||
|
||||
let (before_in_comments, dangling_comments) = dangling_comments.split_at(
|
||||
dangling_comments.partition_point(|comment| comment.end() < in_keyword.start()),
|
||||
dangling_comments.partition_point(|comment| comment.end() < in_token.start()),
|
||||
);
|
||||
|
||||
let (trailing_in_comments, dangling_if_comments) = dangling_comments
|
||||
|
|
|
@ -108,6 +108,9 @@ aaaaaaaaaaaaaaaaaaaaa = [
|
|||
c # negative decimal
|
||||
]
|
||||
|
||||
# Parenthesized targets and iterators.
|
||||
[x for (x) in y]
|
||||
[x for x in (y)]
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -247,6 +250,10 @@ aaaaaaaaaaaaaaaaaaaaa = [
|
|||
for components in b # pylint: disable=undefined-loop-variable # integer 1 may only have decimal 01-09
|
||||
+ c # negative decimal
|
||||
]
|
||||
|
||||
# Parenthesized targets and iterators.
|
||||
[x for (x) in y]
|
||||
[x for x in (y)]
|
||||
```
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue