|
1 | 1 | #[allow(clippy::wildcard_imports)] |
2 | 2 | use oxc_ast::{ast::*, syntax_directed_operations::BoundNames, AstBuilder, Visit}; |
3 | 3 | use oxc_span::{Atom, Span, SPAN}; |
4 | | -use oxc_syntax::scope::ScopeFlags; |
5 | 4 |
|
6 | 5 | pub struct KeepVar<'a> { |
7 | 6 | ast: AstBuilder<'a>, |
8 | 7 | vars: std::vec::Vec<(Atom<'a>, Span)>, |
9 | 8 | } |
10 | 9 |
|
11 | 10 | impl<'a> Visit<'a> for KeepVar<'a> { |
12 | | - fn visit_variable_declaration(&mut self, decl: &VariableDeclaration<'a>) { |
13 | | - if decl.kind.is_var() { |
14 | | - decl.bound_names(&mut |ident| { |
15 | | - self.vars.push((ident.name.clone(), ident.span)); |
16 | | - }); |
| 11 | + fn visit_statement(&mut self, it: &Statement<'a>) { |
| 12 | + // Only visit blocks where vars could be hoisted |
| 13 | + match it { |
| 14 | + Statement::BlockStatement(it) => self.visit_block_statement(it), |
| 15 | + Statement::BreakStatement(it) => self.visit_break_statement(it), |
| 16 | + Statement::ContinueStatement(it) => self.visit_continue_statement(it), |
| 17 | + // Statement::DebuggerStatement(it) => self.visit_debugger_statement(it), |
| 18 | + Statement::DoWhileStatement(it) => self.visit_do_while_statement(it), |
| 19 | + // Statement::EmptyStatement(it) => self.visit_empty_statement(it), |
| 20 | + // Statement::ExpressionStatement(it) => self.visit_expression_statement(it), |
| 21 | + Statement::ForInStatement(it) => self.visit_for_in_statement(it), |
| 22 | + Statement::ForOfStatement(it) => self.visit_for_of_statement(it), |
| 23 | + Statement::ForStatement(it) => self.visit_for_statement(it), |
| 24 | + Statement::IfStatement(it) => self.visit_if_statement(it), |
| 25 | + Statement::LabeledStatement(it) => self.visit_labeled_statement(it), |
| 26 | + // Statement::ReturnStatement(it) => self.visit_return_statement(it), |
| 27 | + Statement::SwitchStatement(it) => self.visit_switch_statement(it), |
| 28 | + // Statement::ThrowStatement(it) => self.visit_throw_statement(it), |
| 29 | + Statement::TryStatement(it) => self.visit_try_statement(it), |
| 30 | + Statement::WhileStatement(it) => self.visit_while_statement(it), |
| 31 | + Statement::WithStatement(it) => self.visit_with_statement(it), |
| 32 | + // match_declaration!(Statement) => visitor.visit_declaration(it.to_declaration()), |
| 33 | + // match_module_declaration!(Statement) => { |
| 34 | + // visitor.visit_module_declaration(it.to_module_declaration()) |
| 35 | + // } |
| 36 | + Statement::VariableDeclaration(decl) => { |
| 37 | + if decl.kind.is_var() { |
| 38 | + decl.bound_names(&mut |ident| { |
| 39 | + self.vars.push((ident.name.clone(), ident.span)); |
| 40 | + }); |
| 41 | + } |
| 42 | + } |
| 43 | + _ => {} |
17 | 44 | } |
18 | 45 | } |
19 | | - |
20 | | - fn visit_function(&mut self, _it: &Function<'a>, _flags: ScopeFlags) { |
21 | | - /* skip functions */ |
22 | | - } |
23 | | - |
24 | | - fn visit_arrow_function_expression(&mut self, _it: &ArrowFunctionExpression<'a>) {} |
25 | | - |
26 | | - fn visit_class(&mut self, _it: &Class<'a>) { |
27 | | - /* skip classes */ |
28 | | - } |
29 | 46 | } |
30 | 47 |
|
31 | 48 | impl<'a> KeepVar<'a> { |
|
0 commit comments