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.
This commit is contained in:
overlookmotel 2024-05-14 11:27:18 +01:00 committed by GitHub
parent 530455849b
commit 6fd7a3c427
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 2 additions and 2 deletions

View file

@ -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)]

View file

@ -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(