Skip to content

Commit 5245c00

Browse files
authored
feat(transformer): add annotation enter and exit hooks (#12)
1 parent 341765b commit 5245c00

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/transformer.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ fn box_expr_option(expr: Option<Expr>) -> Option<Box<Expr>> {
1919
}
2020
#[allow(unused_mut)]
2121
pub trait Transformer {
22+
#[allow(unused_variables)]
23+
fn on_enter_annotation(&mut self, expr: &Expr) {}
24+
#[allow(unused_variables)]
25+
fn on_exit_annotation(&mut self, expr: &Option<Expr>) {}
26+
27+
fn visit_annotation(&mut self, expr: Box<Expr>) -> Option<Expr> {
28+
let unboxed_annotation = *expr;
29+
self.on_enter_annotation(&unboxed_annotation);
30+
let new_annotation = self.visit_expr(unboxed_annotation);
31+
self.on_exit_annotation(&new_annotation);
32+
new_annotation
33+
}
34+
2235
fn visit_stmt_vec(&mut self, stmts: Vec<Stmt>) -> Vec<Stmt> {
2336
let mut new_stmts: Vec<Stmt> = Vec::new();
2437

@@ -741,7 +754,7 @@ pub trait Transformer {
741754
stmt.decorator_list = self.visit_expr_vec(stmt.decorator_list);
742755
stmt.args = Box::new(self.visit_arguments(*stmt.args));
743756
if let Some(returns) = stmt.returns {
744-
stmt.returns = box_expr_option(self.visit_expr(*returns));
757+
stmt.returns = box_expr_option(self.visit_annotation(returns));
745758
}
746759
stmt.body = self.visit_stmt_vec(stmt.body);
747760
if stmt.body.len() == 0 {
@@ -765,7 +778,7 @@ pub trait Transformer {
765778
stmt.decorator_list = self.visit_expr_vec(stmt.decorator_list);
766779
stmt.args = Box::new(self.visit_arguments(*stmt.args));
767780
if let Some(returns) = stmt.returns {
768-
stmt.returns = box_expr_option(self.visit_expr(*returns));
781+
stmt.returns = box_expr_option(self.visit_annotation(returns));
769782
}
770783
stmt.body = self.visit_stmt_vec(stmt.body);
771784
if stmt.body.len() == 0 {
@@ -822,7 +835,7 @@ pub trait Transformer {
822835

823836
fn generic_visit_ann_assign(&mut self, mut stmt: StmtAnnAssign) -> Option<StmtAnnAssign> {
824837
stmt.annotation = Box::new(
825-
self.visit_expr(*stmt.annotation)
838+
self.visit_annotation(stmt.annotation)
826839
.expect("Cannot remove annotation from annotated assignment"),
827840
);
828841

@@ -1368,7 +1381,7 @@ pub trait Transformer {
13681381

13691382
fn generic_visit_arg(&mut self, mut arg: Arg) -> Option<Arg> {
13701383
if let Some(annotation) = arg.annotation {
1371-
arg.annotation = box_expr_option(self.visit_expr(*annotation));
1384+
arg.annotation = box_expr_option(self.visit_annotation(annotation));
13721385
}
13731386
return Some(arg);
13741387
}
@@ -1476,7 +1489,7 @@ pub trait Transformer {
14761489
mut param_var: TypeParamTypeVar,
14771490
) -> Option<TypeParamTypeVar> {
14781491
if let Some(bound) = param_var.bound {
1479-
param_var.bound = box_expr_option(self.visit_expr(*bound));
1492+
param_var.bound = box_expr_option(self.visit_annotation(bound));
14801493
}
14811494
Some(param_var)
14821495
}

0 commit comments

Comments
 (0)