Skip to content

Commit f2b57bb

Browse files
committed
perf(transformer/using): only do work to a switch stmt if it contains a using decl
1 parent 0f5c32b commit f2b57bb

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

crates/oxc_transformer/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ impl<'a> Traverse<'a> for TransformerImpl<'a, '_> {
544544
if let Some(typescript) = self.x0_typescript.as_mut() {
545545
typescript.exit_statement(stmt, ctx);
546546
}
547+
self.explicit_resource_management.exit_statement(stmt, ctx);
547548
self.decorator.enter_statement(stmt, ctx);
548549
self.x2_es2018.exit_statement(stmt, ctx);
549550
self.x2_es2017.exit_statement(stmt, ctx);

crates/oxc_transformer/src/proposals/explicit_resource_management.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use oxc_data_structures::stack::NonEmptyStack;
4343
use oxc_ecmascript::BoundNames;
4444
use oxc_semantic::{ScopeFlags, ScopeId, SymbolFlags};
4545
use oxc_span::{Atom, SPAN};
46-
use oxc_traverse::{BoundIdentifier, Traverse, TraverseCtx};
46+
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx};
4747

4848
use crate::{Helper, TransformCtx};
4949

@@ -55,6 +55,10 @@ pub struct ExplicitResourceManagement<'a, 'ctx> {
5555
/// keeps track of whether the current static block contains a `using` declaration
5656
/// so that we can transform it in `exit_static_block`
5757
static_blocks_stack: NonEmptyStack<bool>,
58+
59+
/// keeps track of whether the current switch statement contains a `using` declaration
60+
/// so that we can transform it in `exit_statement`
61+
switch_stmt_stack: NonEmptyStack<bool>,
5862
}
5963

6064
impl<'a, 'ctx> ExplicitResourceManagement<'a, 'ctx> {
@@ -63,6 +67,7 @@ impl<'a, 'ctx> ExplicitResourceManagement<'a, 'ctx> {
6367
ctx,
6468
top_level_using: FxHashMap::default(),
6569
static_blocks_stack: NonEmptyStack::new(false),
70+
switch_stmt_stack: NonEmptyStack::new(false),
6671
}
6772
}
6873
}
@@ -173,9 +178,16 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> {
173178
ctx: &mut TraverseCtx<'a>,
174179
) {
175180
if matches!(node.kind, VariableDeclarationKind::Using | VariableDeclarationKind::AwaitUsing)
176-
&& ctx.parent().is_static_block()
177181
{
178-
*self.static_blocks_stack.last_mut() = true;
182+
match ctx.parent() {
183+
Ancestor::StaticBlockBody(_) => {
184+
*self.static_blocks_stack.last_mut() = true;
185+
}
186+
Ancestor::SwitchCaseConsequent(_) => {
187+
*self.switch_stmt_stack.last_mut() = true;
188+
}
189+
_ => {}
190+
}
179191
}
180192
}
181193

@@ -222,12 +234,23 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> {
222234
#[inline]
223235
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
224236
match stmt {
237+
// TODO: move this to exit_statement
225238
Statement::BlockStatement(_) => self.transform_block_statement(stmt, ctx),
226-
Statement::SwitchStatement(_) => self.transform_switch_statement(stmt, ctx),
239+
Statement::SwitchStatement(_) => {
240+
self.switch_stmt_stack.push(false);
241+
}
227242
_ => {}
228243
}
229244
}
230245

246+
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
247+
if let Statement::SwitchStatement(_) = stmt {
248+
if self.switch_stmt_stack.pop() {
249+
self.transform_switch_statement(stmt, ctx);
250+
}
251+
}
252+
}
253+
231254
/// Move any top level `using` declarations within a block statement,
232255
/// allowing `enter_statement` to transform them.
233256
fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {

0 commit comments

Comments
 (0)