From 03c643a8af504c2d452238cc1ff2480bc4db7cd1 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Tue, 6 Aug 2024 02:57:18 +0000 Subject: [PATCH] fix(semantic): incorrect `scope_id` for catch parameter symbols (#4659) When we move all the bindings in the CatchClause scope to its child scope (BlockStatement), we also need to replace the scope_id in the symbol with the scope_id of the new scope. --- crates/oxc_semantic/src/builder.rs | 3 +++ crates/oxc_semantic/src/symbol.rs | 5 +++++ tasks/transform_conformance/babel.snap.md | 5 ++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 81995347ddfbb..ce53b0e30b2e5 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -675,6 +675,9 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> { if self.scope.get_flags(parent_scope_id).is_catch_clause() { let parent_bindings = self.scope.get_bindings_mut(parent_scope_id).drain(..).collect::(); + parent_bindings.values().for_each(|symbol_id| { + self.symbols.set_scope_id(*symbol_id, self.current_scope_id); + }); *self.scope.get_bindings_mut(self.current_scope_id) = parent_bindings; } diff --git a/crates/oxc_semantic/src/symbol.rs b/crates/oxc_semantic/src/symbol.rs index e2ea21d0e5d90..e5ae07be56d7a 100644 --- a/crates/oxc_semantic/src/symbol.rs +++ b/crates/oxc_semantic/src/symbol.rs @@ -117,6 +117,11 @@ impl SymbolTable { self.flags[symbol_id] |= includes; } + #[inline] + pub fn set_scope_id(&mut self, symbol_id: SymbolId, scope_id: ScopeId) { + self.scope_ids[symbol_id] = scope_id; + } + #[inline] pub fn get_scope_id(&self, symbol_id: SymbolId) -> ScopeId { self.scope_ids[symbol_id] diff --git a/tasks/transform_conformance/babel.snap.md b/tasks/transform_conformance/babel.snap.md index 8cbe06083bbd5..fe635cc51138e 100644 --- a/tasks/transform_conformance/babel.snap.md +++ b/tasks/transform_conformance/babel.snap.md @@ -1,6 +1,6 @@ commit: 12619ffe -Passed: 434/927 +Passed: 435/927 # All Passed: * babel-preset-react @@ -445,8 +445,7 @@ Passed: 434/927 * opts/optimizeConstEnums/input.ts * opts/rewriteImportExtensions/input.ts -# babel-plugin-transform-typescript (90/151) -* catch-clause/param-type/input.ts +# babel-plugin-transform-typescript (91/151) * class/accessor-allowDeclareFields-false/input.ts * class/accessor-allowDeclareFields-true/input.ts * enum/boolean-value/input.ts