mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-01 16:17:53 +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
|
@ -2,7 +2,8 @@ use crate::node::{AnyNodeRef, AstNode};
|
|||
use crate::{
|
||||
Alias, Arguments, BoolOp, CmpOp, Comprehension, Constant, Decorator, ElifElseClause,
|
||||
ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Parameter, ParameterWithDefault,
|
||||
Parameters, Pattern, Stmt, TypeParam, TypeParams, UnaryOp, WithItem,
|
||||
Parameters, Pattern, PatternArguments, PatternKeyword, Stmt, TypeParam, TypeParams, UnaryOp,
|
||||
WithItem,
|
||||
};
|
||||
|
||||
/// Visitor that traverses all nodes recursively in pre-order.
|
||||
|
@ -132,6 +133,17 @@ pub trait PreorderVisitor<'a> {
|
|||
walk_pattern(self, pattern);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_pattern_arguments(&mut self, pattern_arguments: &'a PatternArguments) {
|
||||
walk_pattern_arguments(self, pattern_arguments);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
||||
fn visit_pattern_keyword(&mut self, pattern_keyword: &'a PatternKeyword) {
|
||||
walk_pattern_keyword(self, pattern_keyword);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_body(&mut self, body: &'a [Stmt]) {
|
||||
walk_body(self, body);
|
||||
|
@ -460,6 +472,33 @@ where
|
|||
visitor.leave_node(node);
|
||||
}
|
||||
|
||||
pub fn walk_pattern_arguments<'a, V>(visitor: &mut V, pattern_arguments: &'a PatternArguments)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
let node = AnyNodeRef::from(pattern_arguments);
|
||||
if visitor.enter_node(node).is_traverse() {
|
||||
for pattern in &pattern_arguments.patterns {
|
||||
visitor.visit_pattern(pattern);
|
||||
}
|
||||
for keyword in &pattern_arguments.keywords {
|
||||
visitor.visit_pattern_keyword(keyword);
|
||||
}
|
||||
}
|
||||
visitor.leave_node(node);
|
||||
}
|
||||
|
||||
pub fn walk_pattern_keyword<'a, V>(visitor: &mut V, pattern_keyword: &'a PatternKeyword)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
let node = AnyNodeRef::from(pattern_keyword);
|
||||
if visitor.enter_node(node).is_traverse() {
|
||||
visitor.visit_pattern(&pattern_keyword.pattern);
|
||||
}
|
||||
visitor.leave_node(node);
|
||||
}
|
||||
|
||||
pub fn walk_bool_op<'a, V>(_visitor: &mut V, _bool_op: &'a BoolOp)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue