From d60f2475053a55e889b6da1c25ec2c7f81d3f2e4 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 25 Jul 2024 18:49:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(semantic):=20incorrect=20symbol=E2=80=99s?= =?UTF-8?q?=20scope=5Fid=20after=20var=20hoisting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxc_semantic/src/binder.rs | 1 + crates/oxc_semantic/tests/integration/scopes.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/crates/oxc_semantic/src/binder.rs b/crates/oxc_semantic/src/binder.rs index 1a54d593c3e48..cba1854af9aa0 100644 --- a/crates/oxc_semantic/src/binder.rs +++ b/crates/oxc_semantic/src/binder.rs @@ -71,6 +71,7 @@ impl<'a> Binder<'a> for VariableDeclarator<'a> { // avoid same symbols appear in multi-scopes builder.scope.remove_binding(*scope_id, &name); builder.scope.add_binding(target_scope_id, name, symbol_id); + builder.symbols.scope_ids[symbol_id] = target_scope_id; break; } } diff --git a/crates/oxc_semantic/tests/integration/scopes.rs b/crates/oxc_semantic/tests/integration/scopes.rs index be50154e2b5d5..ce32fd663ee08 100644 --- a/crates/oxc_semantic/tests/integration/scopes.rs +++ b/crates/oxc_semantic/tests/integration/scopes.rs @@ -138,3 +138,18 @@ fn test_catch_clause_parameters() { .has_number_of_references(1) .test(); } + +#[test] +fn var_hoisting() { + SemanticTester::js( + " + try {} catch (e) { + var e = 0; + } + ", + ) + .has_root_symbol("e") + // `e` was hoisted to the top scope so the symbol's scope is also the top scope + .is_in_scope(ScopeFlags::Top) + .test(); +}