diff --git a/crates/oxc_transformer/src/es2015/arrow_functions.rs b/crates/oxc_transformer/src/es2015/arrow_functions.rs index 82c4d54d9b535..4843258123df9 100644 --- a/crates/oxc_transformer/src/es2015/arrow_functions.rs +++ b/crates/oxc_transformer/src/es2015/arrow_functions.rs @@ -252,16 +252,20 @@ impl<'a> ArrowFunctions<'a> { // let this_var = self.this_var_stack.last_mut().unwrap(); if this_var.is_none() { - let target_scope_id = - ctx.scopes().ancestors(ctx.current_scope_id()).skip(1).find(|&scope_id| { + let target_scope_id = ctx + .scopes() + .ancestors(ctx.current_scope_id()) + .skip(1) + .find(|&scope_id| { let scope_flags = ctx.scopes().get_flags(scope_id); - scope_flags.intersects(ScopeFlags::Function | ScopeFlags::Top) - && !scope_flags.contains(ScopeFlags::Arrow) - }); + // Function but not arrow function + scope_flags & (ScopeFlags::Function | ScopeFlags::Arrow) == ScopeFlags::Function + }) + .unwrap_or(ctx.scopes().root_scope_id()); this_var.replace(BoundIdentifier::new_uid( "this", - target_scope_id.unwrap(), + target_scope_id, SymbolFlags::FunctionScopedVariable, ctx, ));