mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-14 09:45:54 +00:00
Add support for PatternMatchMapping formatting (#6836)
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:
- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->
## Summary
Adds support for `PatternMatchMapping` -- i.e., cases like:
```python
match foo:
case {"a": 1, "b": 2, **rest}:
pass
```
Unfortunately, this node has _three_ kinds of dangling comments:
```python
{ # "open parenthesis comment"
key: pattern,
** # end-of-line "double star comment"
# own-line "double star comment"
rest # end-of-line "after rest comment"
# own-line "after rest comment"
}
```
Some of the complexity comes from the fact that in `**rest`, `rest` is
an _identifier_, not a node, so we have to handle comments _after_ it as
dangling on the enclosing node, rather than trailing on `**rest`. (We
could change the AST to use `PatternMatchAs` there, which would be more
permissive than the grammar but not totally crazy -- `PatternMatchAs` is
used elsewhere to mean "a single identifier".)
Closes https://github.com/astral-sh/ruff/issues/6644.
## Test Plan
`cargo test`
This commit is contained in:
parent
813d7da7ec
commit
1044d66c1c
6 changed files with 407 additions and 73 deletions
|
|
@ -344,3 +344,58 @@ match foo:
|
|||
pass
|
||||
case [(*rest), (a as b)]:
|
||||
pass
|
||||
|
||||
|
||||
match foo:
|
||||
case {"a": 1, "b": 2}:
|
||||
pass
|
||||
|
||||
case {
|
||||
# own line
|
||||
"a": 1, # end-of-line
|
||||
# own line
|
||||
"b": 2,
|
||||
}:
|
||||
pass
|
||||
|
||||
case { # open
|
||||
1 # key
|
||||
: # colon
|
||||
value # value
|
||||
}:
|
||||
pass
|
||||
|
||||
case {**d}:
|
||||
pass
|
||||
|
||||
case {
|
||||
** # middle with single item
|
||||
b
|
||||
}:
|
||||
pass
|
||||
|
||||
case {
|
||||
# before
|
||||
** # between
|
||||
b,
|
||||
}:
|
||||
pass
|
||||
|
||||
case {
|
||||
1: x,
|
||||
# foo
|
||||
** # bop
|
||||
# before
|
||||
b, # boo
|
||||
# baz
|
||||
}:
|
||||
pass
|
||||
|
||||
case {
|
||||
1: x
|
||||
# foo
|
||||
,
|
||||
**
|
||||
b,
|
||||
}:
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue