diff --git a/crates/oxc_transformer/src/es2018/object_rest_spread.rs b/crates/oxc_transformer/src/es2018/object_rest_spread.rs index b6c2df13ccf2dc..864a14dd3e1c77 100644 --- a/crates/oxc_transformer/src/es2018/object_rest_spread.rs +++ b/crates/oxc_transformer/src/es2018/object_rest_spread.rs @@ -571,7 +571,7 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> { } } - // Transform `try {} catch (...x) {}`. + // Transform `try {} catch ({...x}) {}`. fn transform_catch_clause(clause: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) { let Some(param) = &mut clause.param else { unreachable!() }; if Self::has_nested_object_rest(¶m.pattern) { @@ -589,12 +589,6 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> { scope_id, ctx, ); - // Add `SymbolFlags::CatchVariable`. - param.pattern.bound_names(&mut |ident| { - ctx.symbols_mut() - .get_flags_mut(ident.symbol_id()) - .insert(SymbolFlags::CatchVariable); - }); } } @@ -755,7 +749,20 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> { scope_id: ScopeId, ctx: &mut TraverseCtx<'a>, ) -> VariableDeclaration<'a> { - let bound_identifier = ctx.generate_uid("ref", scope_id, kind_to_symbol_flags(kind)); + let mut flags = kind_to_symbol_flags(kind); + if matches!(ctx.parent(), Ancestor::TryStatementHandler(_)) { + // try {} catch (ref) {} + // ^^^ + flags |= SymbolFlags::CatchVariable; + } + + let bound_identifier = if flags == SymbolFlags::FunctionScopedVariable + && !ctx.scopes().get_flags(scope_id).is_var() + { + ctx.generate_uid_in_current_hoist_scope("ref") + } else { + ctx.generate_uid("ref", scope_id, flags) + }; let kind = VariableDeclarationKind::Let; let id = mem::replace(pat, bound_identifier.create_binding_pattern(ctx)); let init = bound_identifier.create_read_expression(ctx); diff --git a/tasks/coverage/snapshots/semantic_test262.snap b/tasks/coverage/snapshots/semantic_test262.snap index 45220bee438bdd..a3612d5022c01b 100644 --- a/tasks/coverage/snapshots/semantic_test262.snap +++ b/tasks/coverage/snapshots/semantic_test262.snap @@ -6733,45 +6733,27 @@ rebuilt : SymbolId(7): ScopeId(0) tasks/coverage/test262/test/language/statements/for-of/dstr/var-obj-ptrn-rest-getter.js semantic error: Bindings mismatch: -after transform: ScopeId(0): ["_extends", "_objectDestructuringEmpty", "count", "iterCount", "x"] +after transform: ScopeId(0): ["_extends", "_objectDestructuringEmpty", "_ref", "count", "iterCount", "x"] rebuilt : ScopeId(0): ["_extends", "_objectDestructuringEmpty", "_ref", "count", "iterCount"] Bindings mismatch: -after transform: ScopeId(1): ["_ref"] -rebuilt : ScopeId(1): [] -Bindings mismatch: after transform: ScopeId(3): [] rebuilt : ScopeId(3): ["x"] -Symbol scope ID mismatch for "_ref": -after transform: SymbolId(3): ScopeId(1) -rebuilt : SymbolId(4): ScopeId(0) tasks/coverage/test262/test/language/statements/for-of/dstr/var-obj-ptrn-rest-skip-non-enumerable.js semantic error: Bindings mismatch: -after transform: ScopeId(0): ["_extends", "_objectDestructuringEmpty", "iterCount", "o", "rest"] +after transform: ScopeId(0): ["_extends", "_objectDestructuringEmpty", "_ref", "iterCount", "o", "rest"] rebuilt : ScopeId(0): ["_extends", "_objectDestructuringEmpty", "_ref", "iterCount", "o"] Bindings mismatch: -after transform: ScopeId(1): ["_ref"] -rebuilt : ScopeId(1): [] -Bindings mismatch: after transform: ScopeId(2): [] rebuilt : ScopeId(2): ["rest"] -Symbol scope ID mismatch for "_ref": -after transform: SymbolId(3): ScopeId(1) -rebuilt : SymbolId(4): ScopeId(0) tasks/coverage/test262/test/language/statements/for-of/dstr/var-obj-ptrn-rest-val-obj.js semantic error: Bindings mismatch: -after transform: ScopeId(0): ["_excluded", "_objectWithoutProperties", "a", "b", "iterCount", "rest"] +after transform: ScopeId(0): ["_excluded", "_objectWithoutProperties", "_ref", "a", "b", "iterCount", "rest"] rebuilt : ScopeId(0): ["_excluded", "_objectWithoutProperties", "_ref", "iterCount"] Bindings mismatch: -after transform: ScopeId(1): ["_ref"] -rebuilt : ScopeId(1): [] -Bindings mismatch: after transform: ScopeId(2): [] rebuilt : ScopeId(2): ["a", "b", "rest"] -Symbol scope ID mismatch for "_ref": -after transform: SymbolId(4): ScopeId(1) -rebuilt : SymbolId(3): ScopeId(0) tasks/coverage/test262/test/language/statements/switch/scope-lex-async-function.js semantic error: Symbol flags mismatch for "x":