mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 04:19:18 +00:00
Introduce AST nodes for PatternMatchClass
arguments (#6881)
## Summary This PR introduces two new AST nodes to improve the representation of `PatternMatchClass`. As a reminder, `PatternMatchClass` looks like this: ```python case Point2D(0, 0, x=1, y=2): ... ``` Historically, this was represented as a vector of patterns (for the `0, 0` portion) and parallel vectors of keyword names (for `x` and `y`) and values (for `1` and `2`). This introduces a bunch of challenges for the formatter, but importantly, it's also really different from how we represent similar nodes, like arguments (`func(0, 0, x=1, y=2)`) or parameters (`def func(x, y)`). So, firstly, we now use a single node (`PatternArguments`) for the entire parenthesized region, making it much more consistent with our other nodes. So, above, `PatternArguments` would be `(0, 0, x=1, y=2)`. Secondly, we now have a `PatternKeyword` node for `x=1` and `y=2`. This is much more similar to the how `Keyword` is represented within `Arguments` for call expressions. Closes https://github.com/astral-sh/ruff/issues/6866. Closes https://github.com/astral-sh/ruff/issues/6880.
This commit is contained in:
parent
ed1b4122d0
commit
15b73bdb8a
19 changed files with 25299 additions and 25824 deletions
|
@ -1900,9 +1900,7 @@ impl From<PatternMatchMapping> for Pattern {
|
|||
pub struct PatternMatchClass {
|
||||
pub range: TextRange,
|
||||
pub cls: Box<Expr>,
|
||||
pub patterns: Vec<Pattern>,
|
||||
pub kwd_attrs: Vec<Identifier>,
|
||||
pub kwd_patterns: Vec<Pattern>,
|
||||
pub arguments: PatternArguments,
|
||||
}
|
||||
|
||||
impl From<PatternMatchClass> for Pattern {
|
||||
|
@ -1911,6 +1909,28 @@ impl From<PatternMatchClass> for Pattern {
|
|||
}
|
||||
}
|
||||
|
||||
/// An AST node to represent the arguments to a [`PatternMatchClass`], i.e., the
|
||||
/// parenthesized contents in `case Point(1, x=0, y=0)`.
|
||||
///
|
||||
/// Like [`Arguments`], but for [`PatternMatchClass`].
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PatternArguments {
|
||||
pub range: TextRange,
|
||||
pub patterns: Vec<Pattern>,
|
||||
pub keywords: Vec<PatternKeyword>,
|
||||
}
|
||||
|
||||
/// An AST node to represent the keyword arguments to a [`PatternMatchClass`], i.e., the
|
||||
/// `x=0` and `y=0` in `case Point(x=0, y=0)`.
|
||||
///
|
||||
/// Like [`Keyword`], but for [`PatternMatchClass`].
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PatternKeyword {
|
||||
pub range: TextRange,
|
||||
pub attr: Identifier,
|
||||
pub pattern: Pattern,
|
||||
}
|
||||
|
||||
/// See also [MatchStar](https://docs.python.org/3/library/ast.html#ast.MatchStar)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PatternMatchStar {
|
||||
|
@ -3004,6 +3024,16 @@ impl Ranged for crate::Pattern {
|
|||
}
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::PatternArguments {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::PatternKeyword {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for crate::nodes::TypeParams {
|
||||
fn range(&self) -> TextRange {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue