Skip to content

Commit

Permalink
fix(transformer/object-rest-spread): generate catch variable binding …
Browse files Browse the repository at this point in the history
…with correct SymbolFlags
  • Loading branch information
Dunqing committed Nov 26, 2024
1 parent f193c49 commit 8e53319
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
23 changes: 15 additions & 8 deletions crates/oxc_transformer/src/es2018/object_rest_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&param.pattern) {
Expand All @@ -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);
});
}
}

Expand Down Expand Up @@ -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);
Expand Down
24 changes: 3 additions & 21 deletions tasks/coverage/snapshots/semantic_test262.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 8e53319

Please sign in to comment.