diff --git a/crates/ruff_python_ast/src/visitor/preorder.rs b/crates/ruff_python_ast/src/visitor/preorder.rs index 5b2e3ad793..ad5cbceccc 100644 --- a/crates/ruff_python_ast/src/visitor/preorder.rs +++ b/crates/ruff_python_ast/src/visitor/preorder.rs @@ -139,7 +139,6 @@ pub trait PreorderVisitor<'a> { } #[inline] - fn visit_pattern_keyword(&mut self, pattern_keyword: &'a PatternKeyword) { walk_pattern_keyword(self, pattern_keyword); } diff --git a/crates/ruff_python_ast/tests/preorder.rs b/crates/ruff_python_ast/tests/preorder.rs index 37e8d72fa8..ea0542d9f1 100644 --- a/crates/ruff_python_ast/tests/preorder.rs +++ b/crates/ruff_python_ast/tests/preorder.rs @@ -2,17 +2,8 @@ use std::fmt::{Debug, Write}; use insta::assert_snapshot; -use ruff_python_ast::visitor::preorder::{ - walk_alias, walk_bytes_literal, walk_comprehension, walk_except_handler, walk_expr, - walk_keyword, walk_match_case, walk_module, walk_parameter, walk_parameters, walk_pattern, - walk_stmt, walk_string_literal, walk_type_param, walk_with_item, PreorderVisitor, -}; -use ruff_python_ast::AnyNodeRef; -use ruff_python_ast::{ - Alias, BoolOp, BytesLiteral, CmpOp, Comprehension, ExceptHandler, Expr, Keyword, MatchCase, - Mod, Operator, Parameter, Parameters, Pattern, Singleton, Stmt, StringLiteral, TypeParam, - UnaryOp, WithItem, -}; +use ruff_python_ast::visitor::preorder::{PreorderVisitor, TraversalSignal}; +use ruff_python_ast::{AnyNodeRef, BoolOp, CmpOp, Operator, Singleton, UnaryOp}; use ruff_python_parser::lexer::lex; use ruff_python_parser::{parse_tokens, Mode}; @@ -175,18 +166,6 @@ struct RecordVisitor { } impl RecordVisitor { - fn enter_node<'a, T>(&mut self, node: T) - where - T: Into>, - { - self.emit(&node.into().kind()); - self.depth += 1; - } - - fn exit_node(&mut self) { - self.depth -= 1; - } - fn emit(&mut self, text: &dyn Debug) { for _ in 0..self.depth { self.output.push_str(" "); @@ -196,29 +175,16 @@ impl RecordVisitor { } } -impl PreorderVisitor<'_> for RecordVisitor { - fn visit_mod(&mut self, module: &Mod) { - self.enter_node(module); - walk_module(self, module); - self.exit_node(); +impl<'a> PreorderVisitor<'a> for RecordVisitor { + fn enter_node(&mut self, node: AnyNodeRef<'a>) -> TraversalSignal { + self.emit(&node.kind()); + self.depth += 1; + + TraversalSignal::Traverse } - fn visit_stmt(&mut self, stmt: &Stmt) { - self.enter_node(stmt); - walk_stmt(self, stmt); - self.exit_node(); - } - - fn visit_annotation(&mut self, expr: &Expr) { - self.enter_node(expr); - walk_expr(self, expr); - self.exit_node(); - } - - fn visit_expr(&mut self, expr: &Expr) { - self.enter_node(expr); - walk_expr(self, expr); - self.exit_node(); + fn leave_node(&mut self, _node: AnyNodeRef<'a>) { + self.depth -= 1; } fn visit_singleton(&mut self, singleton: &Singleton) { @@ -240,82 +206,4 @@ impl PreorderVisitor<'_> for RecordVisitor { fn visit_cmp_op(&mut self, cmp_op: &CmpOp) { self.emit(&cmp_op); } - - fn visit_comprehension(&mut self, comprehension: &Comprehension) { - self.enter_node(comprehension); - walk_comprehension(self, comprehension); - self.exit_node(); - } - - fn visit_except_handler(&mut self, except_handler: &ExceptHandler) { - self.enter_node(except_handler); - walk_except_handler(self, except_handler); - self.exit_node(); - } - - fn visit_format_spec(&mut self, format_spec: &Expr) { - self.enter_node(format_spec); - walk_expr(self, format_spec); - self.exit_node(); - } - - fn visit_parameters(&mut self, parameters: &Parameters) { - self.enter_node(parameters); - walk_parameters(self, parameters); - self.exit_node(); - } - - fn visit_parameter(&mut self, parameter: &Parameter) { - self.enter_node(parameter); - walk_parameter(self, parameter); - self.exit_node(); - } - - fn visit_keyword(&mut self, keyword: &Keyword) { - self.enter_node(keyword); - walk_keyword(self, keyword); - self.exit_node(); - } - - fn visit_alias(&mut self, alias: &Alias) { - self.enter_node(alias); - walk_alias(self, alias); - self.exit_node(); - } - - fn visit_with_item(&mut self, with_item: &WithItem) { - self.enter_node(with_item); - walk_with_item(self, with_item); - self.exit_node(); - } - - fn visit_match_case(&mut self, match_case: &MatchCase) { - self.enter_node(match_case); - walk_match_case(self, match_case); - self.exit_node(); - } - - fn visit_pattern(&mut self, pattern: &Pattern) { - self.enter_node(pattern); - walk_pattern(self, pattern); - self.exit_node(); - } - - fn visit_type_param(&mut self, type_param: &TypeParam) { - self.enter_node(type_param); - walk_type_param(self, type_param); - self.exit_node(); - } - - fn visit_string_literal(&mut self, string_literal: &StringLiteral) { - self.enter_node(string_literal); - walk_string_literal(self, string_literal); - self.exit_node(); - } - - fn visit_bytes_literal(&mut self, bytes_literal: &BytesLiteral) { - self.enter_node(bytes_literal); - walk_bytes_literal(self, bytes_literal); - self.exit_node(); - } } diff --git a/crates/ruff_python_ast/tests/snapshots/preorder__class_type_parameters.snap b/crates/ruff_python_ast/tests/snapshots/preorder__class_type_parameters.snap index bf2a7eccb5..20ef6b71fa 100644 --- a/crates/ruff_python_ast/tests/snapshots/preorder__class_type_parameters.snap +++ b/crates/ruff_python_ast/tests/snapshots/preorder__class_type_parameters.snap @@ -4,11 +4,12 @@ expression: trace --- - ModModule - StmtClassDef - - TypeParamTypeVar - - ExprName - - TypeParamTypeVar - - TypeParamTypeVarTuple - - TypeParamParamSpec + - TypeParams + - TypeParamTypeVar + - ExprName + - TypeParamTypeVar + - TypeParamTypeVarTuple + - TypeParamParamSpec - StmtExpr - ExprEllipsisLiteral diff --git a/crates/ruff_python_ast/tests/snapshots/preorder__decorators.snap b/crates/ruff_python_ast/tests/snapshots/preorder__decorators.snap index ff57b138de..e91bd2e83b 100644 --- a/crates/ruff_python_ast/tests/snapshots/preorder__decorators.snap +++ b/crates/ruff_python_ast/tests/snapshots/preorder__decorators.snap @@ -4,10 +4,12 @@ expression: trace --- - ModModule - StmtFunctionDef - - ExprName + - Decorator + - ExprName - Parameters - StmtPass - StmtClassDef - - ExprName + - Decorator + - ExprName - StmtPass diff --git a/crates/ruff_python_ast/tests/snapshots/preorder__f_strings.snap b/crates/ruff_python_ast/tests/snapshots/preorder__f_strings.snap index 426a155c78..613106d59e 100644 --- a/crates/ruff_python_ast/tests/snapshots/preorder__f_strings.snap +++ b/crates/ruff_python_ast/tests/snapshots/preorder__f_strings.snap @@ -6,17 +6,20 @@ expression: trace - StmtExpr - ExprFString - StringLiteral - - ExprStringLiteral - - StringLiteral - - ExprFormattedValue - - ExprName - - ExprFString - - ExprStringLiteral - - StringLiteral - - ExprFormattedValue - - ExprName - - ExprStringLiteral - - StringLiteral - - ExprStringLiteral - - StringLiteral + - FString + - ExprStringLiteral + - StringLiteral + - ExprFormattedValue + - ExprName + - ExprFString + - ExprFString + - FString + - ExprStringLiteral + - StringLiteral + - ExprFormattedValue + - ExprName + - ExprStringLiteral + - StringLiteral + - ExprStringLiteral + - StringLiteral diff --git a/crates/ruff_python_ast/tests/snapshots/preorder__function_arguments.snap b/crates/ruff_python_ast/tests/snapshots/preorder__function_arguments.snap index 5ecec46ee0..da83fc10f8 100644 --- a/crates/ruff_python_ast/tests/snapshots/preorder__function_arguments.snap +++ b/crates/ruff_python_ast/tests/snapshots/preorder__function_arguments.snap @@ -5,16 +5,22 @@ expression: trace - ModModule - StmtFunctionDef - Parameters + - ParameterWithDefault + - Parameter + - ParameterWithDefault + - Parameter + - ParameterWithDefault + - Parameter + - ParameterWithDefault + - Parameter + - ExprNumberLiteral - Parameter - - Parameter - - Parameter - - Parameter - - ExprNumberLiteral - - Parameter - - Parameter - - ExprNumberLiteral - - Parameter - - ExprNumberLiteral + - ParameterWithDefault + - Parameter + - ExprNumberLiteral + - ParameterWithDefault + - Parameter + - ExprNumberLiteral - Parameter - StmtPass diff --git a/crates/ruff_python_ast/tests/snapshots/preorder__function_positional_only_with_default.snap b/crates/ruff_python_ast/tests/snapshots/preorder__function_positional_only_with_default.snap index fdc755b1f0..6511aa3468 100644 --- a/crates/ruff_python_ast/tests/snapshots/preorder__function_positional_only_with_default.snap +++ b/crates/ruff_python_ast/tests/snapshots/preorder__function_positional_only_with_default.snap @@ -5,11 +5,14 @@ expression: trace - ModModule - StmtFunctionDef - Parameters - - Parameter - - Parameter - - ExprNumberLiteral - - Parameter - - ExprNumberLiteral + - ParameterWithDefault + - Parameter + - ParameterWithDefault + - Parameter + - ExprNumberLiteral + - ParameterWithDefault + - Parameter + - ExprNumberLiteral - Parameter - StmtPass diff --git a/crates/ruff_python_ast/tests/snapshots/preorder__function_type_parameters.snap b/crates/ruff_python_ast/tests/snapshots/preorder__function_type_parameters.snap index 47aa1ecf6a..11b98d0fa7 100644 --- a/crates/ruff_python_ast/tests/snapshots/preorder__function_type_parameters.snap +++ b/crates/ruff_python_ast/tests/snapshots/preorder__function_type_parameters.snap @@ -4,11 +4,12 @@ expression: trace --- - ModModule - StmtFunctionDef - - TypeParamTypeVar - - ExprName - - TypeParamTypeVar - - TypeParamTypeVarTuple - - TypeParamParamSpec + - TypeParams + - TypeParamTypeVar + - ExprName + - TypeParamTypeVar + - TypeParamTypeVarTuple + - TypeParamParamSpec - Parameters - StmtExpr - ExprEllipsisLiteral diff --git a/crates/ruff_python_ast/tests/snapshots/preorder__match_class_pattern.snap b/crates/ruff_python_ast/tests/snapshots/preorder__match_class_pattern.snap index da4fe2cd97..dbe56f11ff 100644 --- a/crates/ruff_python_ast/tests/snapshots/preorder__match_class_pattern.snap +++ b/crates/ruff_python_ast/tests/snapshots/preorder__match_class_pattern.snap @@ -8,21 +8,26 @@ expression: trace - MatchCase - PatternMatchClass - ExprName - - PatternMatchValue - - ExprNumberLiteral - - PatternMatchValue - - ExprNumberLiteral + - PatternArguments + - PatternMatchValue + - ExprNumberLiteral + - PatternMatchValue + - ExprNumberLiteral - StmtExpr - ExprEllipsisLiteral - MatchCase - PatternMatchClass - ExprName - - PatternMatchValue - - ExprNumberLiteral - - PatternMatchValue - - ExprNumberLiteral - - PatternMatchValue - - ExprNumberLiteral + - PatternArguments + - PatternKeyword + - PatternMatchValue + - ExprNumberLiteral + - PatternKeyword + - PatternMatchValue + - ExprNumberLiteral + - PatternKeyword + - PatternMatchValue + - ExprNumberLiteral - StmtExpr - ExprEllipsisLiteral diff --git a/crates/ruff_python_ast/tests/snapshots/preorder__type_aliases.snap b/crates/ruff_python_ast/tests/snapshots/preorder__type_aliases.snap index 56c170d9b8..56d6aec5c0 100644 --- a/crates/ruff_python_ast/tests/snapshots/preorder__type_aliases.snap +++ b/crates/ruff_python_ast/tests/snapshots/preorder__type_aliases.snap @@ -5,11 +5,12 @@ expression: trace - ModModule - StmtTypeAlias - ExprName - - TypeParamTypeVar - - ExprName - - TypeParamTypeVar - - TypeParamTypeVarTuple - - TypeParamParamSpec + - TypeParams + - TypeParamTypeVar + - ExprName + - TypeParamTypeVar + - TypeParamTypeVarTuple + - TypeParamParamSpec - ExprSubscript - ExprName - ExprName