From 6fd7a3c42712487430ddfd627f61654a32ab58ce Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Tue, 14 May 2024 11:27:18 +0100 Subject: [PATCH] fix(traverse): create scopes for functions (#3273) Fix bug with: 1. scopes not being created for functions. 2. too many scopes being created for class methods. i.e. Logic for when `Function` creates a scope was opposite of what it should be. Discovered by @Dunqing in https://github.com/oxc-project/oxc/pull/3258#pullrequestreview-2054259476. --- crates/oxc_ast/src/ast/js.rs | 2 +- crates/oxc_traverse/src/walk.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 559624e2c..6a4f7809b 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -2136,7 +2136,7 @@ pub struct BindingRestElement<'a> { // 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(_))), + 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)] diff --git a/crates/oxc_traverse/src/walk.rs b/crates/oxc_traverse/src/walk.rs index f6c6792c3..449735ac5 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 = matches!(ctx.ancestor(2).unwrap(), Ancestor::MethodDefinitionValue(_)); + let has_scope = !matches!(ctx.ancestor(2).unwrap(), Ancestor::MethodDefinitionValue(_)); if has_scope { ctx.push_scope_stack( ScopeFlags::Function.with_strict_mode(