From e4ca06ae8c355c01ebf9df645a516288bac49caf Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 25 Jul 2024 21:12:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(semantic):=20incorrect=20symbol=E2=80=99s?= =?UTF-8?q?=20scope=5Fid=20after=20var=20hoisting=20(#4458)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug found in https://github.com/rolldown/rolldown/pull/1726 --- 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 1a54d593c..cba1854af 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 be50154e2..ce32fd663 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(); +}