From 0400ff9ea5864bb7ce99e9a428ecf364c350971c Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:44:51 +0000 Subject: [PATCH] refactor(transformer): `VarDeclarations` common transform: check if at 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()`. --- crates/oxc_transformer/src/common/var_declarations.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/oxc_transformer/src/common/var_declarations.rs b/crates/oxc_transformer/src/common/var_declarations.rs index 24f5d9ab16b5c..3ae3bcf824ca1 100644 --- a/crates/oxc_transformer/src/common/var_declarations.rs +++ b/crates/oxc_transformer/src/common/var_declarations.rs @@ -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; @@ -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; }