Skip to content

Commit

Permalink
refactor(traverse): refactor ChildScopeCollector (#5112)
Browse files Browse the repository at this point in the history
Refactor `ChildScopeCollector`. Now that all scopes are unconditional, can move shared logic into a common method.
  • Loading branch information
overlookmotel committed Aug 23, 2024
1 parent 40e2f6e commit 1ba11a3
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,70 +528,90 @@ impl ChildScopeCollector {
fn new() -> Self {
Self { scope_ids: vec![] }
}

fn add_scope(&mut self, scope_id: &Cell<Option<ScopeId>>) {
self.scope_ids.push(scope_id.get().unwrap());
}
}

impl<'a> Visit<'a> for ChildScopeCollector {
#[inline]
fn visit_block_statement(&mut self, stmt: &BlockStatement<'a>) {
self.scope_ids.push(stmt.scope_id.get().unwrap());
self.add_scope(&stmt.scope_id);
}

#[inline]
fn visit_for_statement(&mut self, stmt: &ForStatement<'a>) {
self.scope_ids.push(stmt.scope_id.get().unwrap());
self.add_scope(&stmt.scope_id);
}

#[inline]
fn visit_for_in_statement(&mut self, stmt: &ForInStatement<'a>) {
self.scope_ids.push(stmt.scope_id.get().unwrap());
self.add_scope(&stmt.scope_id);
}

#[inline]
fn visit_for_of_statement(&mut self, stmt: &ForOfStatement<'a>) {
self.scope_ids.push(stmt.scope_id.get().unwrap());
self.add_scope(&stmt.scope_id);
}

#[inline]
fn visit_switch_statement(&mut self, stmt: &SwitchStatement<'a>) {
self.scope_ids.push(stmt.scope_id.get().unwrap());
self.add_scope(&stmt.scope_id);
}

#[inline]
fn visit_catch_clause(&mut self, clause: &CatchClause<'a>) {
self.scope_ids.push(clause.scope_id.get().unwrap());
self.add_scope(&clause.scope_id);
}

fn visit_finally_clause(&mut self, clause: &BlockStatement<'a>) {
self.scope_ids.push(clause.scope_id.get().unwrap());
#[inline]
fn visit_finally_clause(&mut self, block: &BlockStatement<'a>) {
self.add_scope(&block.scope_id);
}

#[inline]
fn visit_function(&mut self, func: &Function<'a>, _flags: ScopeFlags) {
self.scope_ids.push(func.scope_id.get().unwrap());
self.add_scope(&func.scope_id);
}

#[inline]
fn visit_class(&mut self, class: &Class<'a>) {
self.scope_ids.push(class.scope_id.get().unwrap());
self.add_scope(&class.scope_id);
}

#[inline]
fn visit_static_block(&mut self, block: &StaticBlock<'a>) {
self.scope_ids.push(block.scope_id.get().unwrap());
self.add_scope(&block.scope_id);
}

#[inline]
fn visit_arrow_function_expression(&mut self, expr: &ArrowFunctionExpression<'a>) {
self.scope_ids.push(expr.scope_id.get().unwrap());
self.add_scope(&expr.scope_id);
}

#[inline]
fn visit_ts_enum_declaration(&mut self, decl: &TSEnumDeclaration<'a>) {
self.scope_ids.push(decl.scope_id.get().unwrap());
self.add_scope(&decl.scope_id);
}

#[inline]
fn visit_ts_module_declaration(&mut self, decl: &TSModuleDeclaration<'a>) {
self.scope_ids.push(decl.scope_id.get().unwrap());
self.add_scope(&decl.scope_id);
}

#[inline]
fn visit_ts_interface_declaration(&mut self, it: &TSInterfaceDeclaration<'a>) {
self.scope_ids.push(it.scope_id.get().unwrap());
self.add_scope(&it.scope_id);
}

#[inline]
fn visit_ts_mapped_type(&mut self, it: &TSMappedType<'a>) {
self.scope_ids.push(it.scope_id.get().unwrap());
self.add_scope(&it.scope_id);
}

#[inline]
fn visit_ts_conditional_type(&mut self, it: &TSConditionalType<'a>) {
self.scope_ids.push(it.scope_id.get().unwrap());
self.add_scope(&it.scope_id);
}
}

0 comments on commit 1ba11a3

Please sign in to comment.