Skip to content

Commit

Permalink
refactor(transformer): VarDeclarations common transform: check if a…
Browse files Browse the repository at this point in the history
…t top level with `ctx.parent()` (#6231)

Micro-optimization. The stack of ancestors is now a `NonEmptyStack`, so `ctx.parent()` is now cheaper than `ctx.ancestors_depth()`.
  • Loading branch information
overlookmotel committed Oct 1, 2024
1 parent a949ecb commit 0400ff9
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions crates/oxc_transformer/src/common/var_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use oxc_ast::{ast::*, NONE};
use oxc_data_structures::stack::SparseStack;
use oxc_span::SPAN;
use oxc_syntax::symbol::SymbolId;
use oxc_traverse::{Traverse, TraverseCtx};
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};

use crate::TransformCtx;

Expand Down Expand Up @@ -59,9 +59,8 @@ impl<'a, 'ctx> Traverse<'a> for VarDeclarations<'a, 'ctx> {
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
if ctx.ancestors_depth() == 2 {
// Top level. Handle in `exit_program` instead.
// (depth 1 = None, depth 2 = Program)
if matches!(ctx.parent(), Ancestor::ProgramBody(_)) {
// Handle in `exit_program` instead
return;
}

Expand Down

0 comments on commit 0400ff9

Please sign in to comment.