mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 17:41:12 +00:00
Create PreorderVisitor
trait (#4658)
This commit is contained in:
parent
52deeb36ee
commit
33a7ed058f
12 changed files with 1301 additions and 1 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1961,6 +1961,7 @@ version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"bitflags 2.3.1",
|
"bitflags 2.3.1",
|
||||||
|
"insta",
|
||||||
"is-macro",
|
"is-macro",
|
||||||
"itertools",
|
"itertools",
|
||||||
"log",
|
"log",
|
||||||
|
|
|
@ -26,5 +26,8 @@ rustpython-ast = { workspace = true }
|
||||||
serde = { workspace = true, optional = true }
|
serde = { workspace = true, optional = true }
|
||||||
smallvec = { workspace = true }
|
smallvec = { workspace = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
insta = { workspace = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
serde = ["dep:serde", "ruff_text_size/serde"]
|
serde = ["dep:serde", "ruff_text_size/serde"]
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
//! AST visitor trait and walk functions.
|
//! AST visitor trait and walk functions.
|
||||||
|
|
||||||
|
pub mod preorder;
|
||||||
|
|
||||||
use rustpython_parser::ast::{
|
use rustpython_parser::ast::{
|
||||||
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler, Expr,
|
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler, Expr,
|
||||||
ExprContext, Keyword, MatchCase, Operator, Pattern, Stmt, Unaryop, Withitem,
|
ExprContext, Keyword, MatchCase, Operator, Pattern, Stmt, Unaryop, Withitem,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A trait for AST visitors. Visits all nodes in the AST recursively.
|
/// A trait for AST visitors. Visits all nodes in the AST recursively in evaluation-order.
|
||||||
///
|
///
|
||||||
/// Prefer [`crate::statement_visitor::StatementVisitor`] for visitors that only need to visit
|
/// Prefer [`crate::statement_visitor::StatementVisitor`] for visitors that only need to visit
|
||||||
/// statements.
|
/// statements.
|
||||||
|
///
|
||||||
|
/// Use the [`PreorderVisitor`](self::preorder::PreorderVisitor) if you want to visit the nodes
|
||||||
|
/// in pre-order rather than evaluation order.
|
||||||
pub trait Visitor<'a> {
|
pub trait Visitor<'a> {
|
||||||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
||||||
walk_stmt(self, stmt);
|
walk_stmt(self, stmt);
|
||||||
|
|
1147
crates/ruff_python_ast/src/visitor/preorder.rs
Normal file
1147
crates/ruff_python_ast/src/visitor/preorder.rs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_ast/src/visitor/preorder.rs
|
||||||
|
expression: trace
|
||||||
|
---
|
||||||
|
- ModModule
|
||||||
|
- StmtExpr
|
||||||
|
- ExprCompare
|
||||||
|
- ExprConstant
|
||||||
|
- Int(4)
|
||||||
|
- Lt
|
||||||
|
- ExprName
|
||||||
|
- Lt
|
||||||
|
- ExprConstant
|
||||||
|
- Int(5)
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_ast/src/visitor/preorder.rs
|
||||||
|
expression: trace
|
||||||
|
---
|
||||||
|
- ModModule
|
||||||
|
- StmtFunctionDef
|
||||||
|
- ExprName
|
||||||
|
- Arguments
|
||||||
|
- StmtPass
|
||||||
|
- StmtClassDef
|
||||||
|
- ExprName
|
||||||
|
- StmtPass
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_ast/src/visitor/preorder.rs
|
||||||
|
expression: trace
|
||||||
|
---
|
||||||
|
- ModModule
|
||||||
|
- StmtExpr
|
||||||
|
- ExprDictComp
|
||||||
|
- ExprName
|
||||||
|
- ExprBinOp
|
||||||
|
- ExprName
|
||||||
|
- Pow
|
||||||
|
- ExprConstant
|
||||||
|
- Int(2)
|
||||||
|
- Comprehension
|
||||||
|
- ExprName
|
||||||
|
- ExprName
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_ast/src/visitor/preorder.rs
|
||||||
|
expression: trace
|
||||||
|
---
|
||||||
|
- ModModule
|
||||||
|
- StmtFunctionDef
|
||||||
|
- Arguments
|
||||||
|
- Arg
|
||||||
|
- Arg
|
||||||
|
- Arg
|
||||||
|
- Arg
|
||||||
|
- ExprConstant
|
||||||
|
- Int(20)
|
||||||
|
- Arg
|
||||||
|
- Arg
|
||||||
|
- ExprConstant
|
||||||
|
- Int(5)
|
||||||
|
- Arg
|
||||||
|
- ExprConstant
|
||||||
|
- Int(20)
|
||||||
|
- Arg
|
||||||
|
- StmtPass
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_ast/src/visitor/preorder.rs
|
||||||
|
expression: trace
|
||||||
|
---
|
||||||
|
- ModModule
|
||||||
|
- StmtFunctionDef
|
||||||
|
- Arguments
|
||||||
|
- Arg
|
||||||
|
- Arg
|
||||||
|
- ExprConstant
|
||||||
|
- Int(34)
|
||||||
|
- Arg
|
||||||
|
- ExprConstant
|
||||||
|
- Int(20)
|
||||||
|
- Arg
|
||||||
|
- StmtPass
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_ast/src/visitor/preorder.rs
|
||||||
|
expression: trace
|
||||||
|
---
|
||||||
|
- ModModule
|
||||||
|
- StmtExpr
|
||||||
|
- ExprListComp
|
||||||
|
- ExprName
|
||||||
|
- Comprehension
|
||||||
|
- ExprName
|
||||||
|
- ExprName
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_ast/src/visitor/preorder.rs
|
||||||
|
expression: trace
|
||||||
|
---
|
||||||
|
- ModModule
|
||||||
|
- StmtMatch
|
||||||
|
- ExprName
|
||||||
|
- MatchCase
|
||||||
|
- PatternMatchClass
|
||||||
|
- ExprName
|
||||||
|
- PatternMatchValue
|
||||||
|
- ExprConstant
|
||||||
|
- Int(0)
|
||||||
|
- PatternMatchValue
|
||||||
|
- ExprConstant
|
||||||
|
- Int(0)
|
||||||
|
- StmtExpr
|
||||||
|
- ExprConstant
|
||||||
|
- Ellipsis
|
||||||
|
- MatchCase
|
||||||
|
- PatternMatchClass
|
||||||
|
- ExprName
|
||||||
|
- PatternMatchValue
|
||||||
|
- ExprConstant
|
||||||
|
- Int(0)
|
||||||
|
- PatternMatchValue
|
||||||
|
- ExprConstant
|
||||||
|
- Int(0)
|
||||||
|
- PatternMatchValue
|
||||||
|
- ExprConstant
|
||||||
|
- Int(0)
|
||||||
|
- StmtExpr
|
||||||
|
- ExprConstant
|
||||||
|
- Ellipsis
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_ast/src/visitor/preorder.rs
|
||||||
|
expression: trace
|
||||||
|
---
|
||||||
|
- ModModule
|
||||||
|
- StmtExpr
|
||||||
|
- ExprSetComp
|
||||||
|
- ExprName
|
||||||
|
- Comprehension
|
||||||
|
- ExprName
|
||||||
|
- ExprName
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue