diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index c7446f8cc..799456de3 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -2133,8 +2133,10 @@ pub struct BindingRestElement<'a> { /// Function Definitions #[visited_node( scope(ScopeFlags::Function), - // Don't create a 2nd scope if `MethodDefinition` already created one - scope_if((ctx.scope() & ScopeFlags::Modifiers).is_empty()), + // Don't create scope if this is a method - `MethodDefinition` already created one. + // `ctx.ancestor(2).unwrap()` not `ctx.parent()` because this code is inserted + // into `walk_function` *after* `Function` is added to stack. + scope_if(matches!(ctx.ancestor(2).unwrap(), Ancestor::MethodDefinitionValue(_))), strict_if(self.body.as_ref().is_some_and(|body| body.has_use_strict_directive())) )] #[derive(Debug, Hash)] @@ -2586,7 +2588,7 @@ impl MethodDefinitionKind { pub fn scope_flags(self) -> ScopeFlags { match self { Self::Constructor => ScopeFlags::Constructor, - Self::Method => ScopeFlags::Method, + Self::Method => ScopeFlags::empty(), Self::Get => ScopeFlags::GetAccessor, Self::Set => ScopeFlags::SetAccessor, } diff --git a/crates/oxc_syntax/src/scope.rs b/crates/oxc_syntax/src/scope.rs index 7aa3f0ea9..d6b9e0f48 100644 --- a/crates/oxc_syntax/src/scope.rs +++ b/crates/oxc_syntax/src/scope.rs @@ -23,11 +23,8 @@ bitflags! { const Constructor = 1 << 6; const GetAccessor = 1 << 7; const SetAccessor = 1 << 8; - // Only used in `Traverse` - const Method = 1 << 9; const Var = Self::Top.bits() | Self::Function.bits() | Self::ClassStaticBlock.bits() | Self::TsModuleBlock.bits(); - const Modifiers = Self::Constructor.bits() | Self::GetAccessor.bits() - | Self::SetAccessor.bits() | Self::Method.bits(); + const Modifiers = Self::Constructor.bits() | Self::GetAccessor.bits() | Self::SetAccessor.bits(); } } diff --git a/crates/oxc_traverse/src/walk.rs b/crates/oxc_traverse/src/walk.rs index c1690a292..f6c6792c3 100644 --- a/crates/oxc_traverse/src/walk.rs +++ b/crates/oxc_traverse/src/walk.rs @@ -2228,7 +2228,7 @@ pub(crate) unsafe fn walk_function<'a, Tr: Traverse<'a>>( ) { traverser.enter_function(&mut *node, ctx); ctx.push_stack(Ancestor::FunctionId(ancestor::FunctionWithoutId(node))); - let has_scope = (ctx.scope() & ScopeFlags::Modifiers).is_empty(); + let has_scope = matches!(ctx.ancestor(2).unwrap(), Ancestor::MethodDefinitionValue(_)); if has_scope { ctx.push_scope_stack( ScopeFlags::Function.with_strict_mode(