diff --git a/crates/oxc_transformer/src/proposals/explicit_resource_management.rs b/crates/oxc_transformer/src/proposals/explicit_resource_management.rs index c4e94515a97fd..48afc2b5f1911 100644 --- a/crates/oxc_transformer/src/proposals/explicit_resource_management.rs +++ b/crates/oxc_transformer/src/proposals/explicit_resource_management.rs @@ -84,6 +84,12 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> { let variable_declarator = decl.declarations.first_mut().unwrap(); variable_declarator.kind = VariableDeclarationKind::Const; + let variable_declarator_binding_ident = + variable_declarator.id.get_binding_identifier().unwrap(); + + let for_of_init_symbol_id = variable_declarator_binding_ident.symbol_id(); + let for_of_init_name = variable_declarator_binding_ident.name; + let temp_id = ctx.generate_uid_based_on_node( variable_declarator.id.get_binding_identifier().unwrap(), for_of_stmt_scope_id, @@ -107,6 +113,13 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> { false, )); + let scope_id = match &mut for_of_stmt.body { + Statement::BlockStatement(block) => block.scope_id(), + _ => ctx.create_child_scope(for_of_stmt_scope_id, ScopeFlags::empty()), + }; + ctx.scoping_mut().set_symbol_scope_id(for_of_init_symbol_id, scope_id); + ctx.scoping_mut().move_binding(for_of_stmt_scope_id, scope_id, &for_of_init_name); + if let Statement::BlockStatement(body) = &mut for_of_stmt.body { // `for (const _x of y) { x(); }` -> `for (const _x of y) { using x = _x; x(); }` body.body.insert(0, using_stmt); @@ -115,11 +128,7 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> { let old_body = ctx.ast.move_statement(&mut for_of_stmt.body); let new_body = ctx.ast.vec_from_array([using_stmt, old_body]); - for_of_stmt.body = ctx.ast.statement_block_with_scope_id( - SPAN, - new_body, - ctx.create_child_scope(for_of_stmt_scope_id, ScopeFlags::empty()), - ); + for_of_stmt.body = ctx.ast.statement_block_with_scope_id(SPAN, new_body, scope_id); }; } diff --git a/tasks/transform_conformance/snapshots/babel.snap.md b/tasks/transform_conformance/snapshots/babel.snap.md index 50c14fa36bfd1..a9d30f6d17498 100644 --- a/tasks/transform_conformance/snapshots/babel.snap.md +++ b/tasks/transform_conformance/snapshots/babel.snap.md @@ -1,6 +1,6 @@ commit: 578ac4df -Passed: 710/1191 +Passed: 713/1191 # All Passed: * babel-plugin-transform-logical-assignment-operators @@ -2752,40 +2752,7 @@ transform-react-jsx: unknown field `autoImport`, expected one of `runtime`, `dev x Output mismatch -# babel-plugin-proposal-explicit-resource-management (17/24) -* source-maps/for-of/input.js -Bindings mismatch: -after transform: ScopeId(1): ["_x", "x"] -rebuilt : ScopeId(1): ["_x"] -Bindings mismatch: -after transform: ScopeId(2): [] -rebuilt : ScopeId(2): ["x"] -Symbol scope ID mismatch for "x": -after transform: SymbolId(0): ScopeId(1) -rebuilt : SymbolId(2): ScopeId(2) - -* transform-sync/for-await-head/input.js -Bindings mismatch: -after transform: ScopeId(1): ["_x", "x"] -rebuilt : ScopeId(1): ["_x"] -Bindings mismatch: -after transform: ScopeId(2): [] -rebuilt : ScopeId(2): ["x"] -Symbol scope ID mismatch for "x": -after transform: SymbolId(0): ScopeId(1) -rebuilt : SymbolId(2): ScopeId(2) - -* transform-sync/for-head/input.js -Bindings mismatch: -after transform: ScopeId(1): ["_x", "x"] -rebuilt : ScopeId(1): ["_x"] -Bindings mismatch: -after transform: ScopeId(2): [] -rebuilt : ScopeId(2): ["x"] -Symbol scope ID mismatch for "x": -after transform: SymbolId(0): ScopeId(1) -rebuilt : SymbolId(2): ScopeId(2) - +# babel-plugin-proposal-explicit-resource-management (20/24) * transform-sync/function-body/input.js Bindings mismatch: after transform: ScopeId(1): ["_usingCtx", "x"] diff --git a/tasks/transform_conformance/snapshots/oxc.snap.md b/tasks/transform_conformance/snapshots/oxc.snap.md index db1a4a5518e10..921cd3d4002d9 100644 --- a/tasks/transform_conformance/snapshots/oxc.snap.md +++ b/tasks/transform_conformance/snapshots/oxc.snap.md @@ -325,24 +325,15 @@ x Output mismatch x Output mismatch * for-of-no-block/input.js -Bindings mismatch: -after transform: ScopeId(1): ["_x", "x"] -rebuilt : ScopeId(1): ["_x"] Scope children mismatch: after transform: ScopeId(1): [ScopeId(2), ScopeId(3), ScopeId(4), ScopeId(5), ScopeId(7)] rebuilt : ScopeId(1): [ScopeId(2), ScopeId(3), ScopeId(5), ScopeId(7)] -Bindings mismatch: -after transform: ScopeId(4): [] -rebuilt : ScopeId(3): ["x"] Scope children mismatch: after transform: ScopeId(4): [] rebuilt : ScopeId(3): [ScopeId(4)] Scope parent mismatch: after transform: ScopeId(3): Some(ScopeId(1)) rebuilt : ScopeId(4): Some(ScopeId(3)) -Symbol scope ID mismatch for "x": -after transform: SymbolId(0): ScopeId(1) -rebuilt : SymbolId(2): ScopeId(3) * function-with-scopes-in-params/input.js Bindings mismatch: