Fix preorder visitor tests (#9025)

Follow-up PR to #9009 to fix the `PreorderVisitor` test cases as
suggested here: https://github.com/astral-sh/ruff/pull/9009#discussion_r1416459688
This commit is contained in:
Dhruv Manilawala 2023-12-06 10:58:51 -06:00 committed by GitHub
parent bd443ebe91
commit ef7778d794
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 86 additions and 177 deletions

View file

@ -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);
}

View file

@ -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<AnyNodeRef<'a>>,
{
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();
}
}

View file

@ -4,11 +4,12 @@ expression: trace
---
- ModModule
- StmtClassDef
- TypeParamTypeVar
- ExprName
- TypeParamTypeVar
- TypeParamTypeVarTuple
- TypeParamParamSpec
- TypeParams
- TypeParamTypeVar
- ExprName
- TypeParamTypeVar
- TypeParamTypeVarTuple
- TypeParamParamSpec
- StmtExpr
- ExprEllipsisLiteral

View file

@ -4,10 +4,12 @@ expression: trace
---
- ModModule
- StmtFunctionDef
- ExprName
- Decorator
- ExprName
- Parameters
- StmtPass
- StmtClassDef
- ExprName
- Decorator
- ExprName
- StmtPass

View file

@ -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

View file

@ -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

View file

@ -5,11 +5,14 @@ expression: trace
- ModModule
- StmtFunctionDef
- Parameters
- Parameter
- Parameter
- ExprNumberLiteral
- Parameter
- ExprNumberLiteral
- ParameterWithDefault
- Parameter
- ParameterWithDefault
- Parameter
- ExprNumberLiteral
- ParameterWithDefault
- Parameter
- ExprNumberLiteral
- Parameter
- StmtPass

View file

@ -4,11 +4,12 @@ expression: trace
---
- ModModule
- StmtFunctionDef
- TypeParamTypeVar
- ExprName
- TypeParamTypeVar
- TypeParamTypeVarTuple
- TypeParamParamSpec
- TypeParams
- TypeParamTypeVar
- ExprName
- TypeParamTypeVar
- TypeParamTypeVarTuple
- TypeParamParamSpec
- Parameters
- StmtExpr
- ExprEllipsisLiteral

View file

@ -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

View file

@ -5,11 +5,12 @@ expression: trace
- ModModule
- StmtTypeAlias
- ExprName
- TypeParamTypeVar
- ExprName
- TypeParamTypeVar
- TypeParamTypeVarTuple
- TypeParamParamSpec
- TypeParams
- TypeParamTypeVar
- ExprName
- TypeParamTypeVar
- TypeParamTypeVarTuple
- TypeParamParamSpec
- ExprSubscript
- ExprName
- ExprName