diff --git a/crates/oxc_transformer/src/common/arrow_function_converter.rs b/crates/oxc_transformer/src/common/arrow_function_converter.rs index 993c44d5a..60a214ea7 100644 --- a/crates/oxc_transformer/src/common/arrow_function_converter.rs +++ b/crates/oxc_transformer/src/common/arrow_function_converter.rs @@ -146,6 +146,7 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> { if let Some(this_var) = self.this_var_stack.take_last() { self.insert_this_var_statement_at_the_top_of_statements( &mut program.body, + program.scope_id.get().unwrap(), &this_var, ctx, ); @@ -183,6 +184,7 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> { self.insert_this_var_statement_at_the_top_of_statements( &mut body.statements, + func.scope_id.get().unwrap(), &this_var, ctx, ); @@ -205,6 +207,7 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> { if let Some(this_var) = self.this_var_stack.pop() { self.insert_this_var_statement_at_the_top_of_statements( &mut block.body, + block.scope_id.get().unwrap(), &this_var, ctx, ); @@ -448,9 +451,19 @@ impl<'a> ArrowFunctionConverter<'a> { fn insert_this_var_statement_at_the_top_of_statements( &mut self, statements: &mut ArenaVec<'a, Statement<'a>>, + target_scope_id: ScopeId, this_var: &BoundIdentifier<'a>, - ctx: &TraverseCtx<'a>, + ctx: &mut TraverseCtx<'a>, ) { + let symbol_id = this_var.symbol_id; + let scope_id = ctx.symbols().get_scope_id(symbol_id); + // Because scope can be moved or deleted, we need to check the scope id again, + // if it's different, we need to move the binding to the target scope. + if target_scope_id != scope_id { + ctx.scopes_mut().move_binding(scope_id, target_scope_id, &this_var.name); + ctx.symbols_mut().set_scope_id(symbol_id, target_scope_id); + } + let variable_declarator = ctx.ast.variable_declarator( SPAN, VariableDeclarationKind::Var, diff --git a/tasks/transform_conformance/snapshots/babel.snap.md b/tasks/transform_conformance/snapshots/babel.snap.md index 577ab810d..87e342c60 100644 --- a/tasks/transform_conformance/snapshots/babel.snap.md +++ b/tasks/transform_conformance/snapshots/babel.snap.md @@ -1,6 +1,6 @@ commit: d20b314c -Passed: 377/1078 +Passed: 381/1078 # All Passed: * babel-plugin-transform-class-static-block @@ -1450,40 +1450,7 @@ x Output mismatch x Output mismatch -# babel-plugin-transform-async-generator-functions (15/19) -* async-generators/class-method/input.js -Bindings mismatch: -after transform: ScopeId(0): ["C", "_this"] -rebuilt : ScopeId(0): ["C"] -Bindings mismatch: -after transform: ScopeId(3): [] -rebuilt : ScopeId(2): ["_this"] -Symbol scope ID mismatch for "_this": -after transform: SymbolId(1): ScopeId(0) -rebuilt : SymbolId(1): ScopeId(2) - -* async-generators/object-method/input.js -Bindings mismatch: -after transform: ScopeId(0): ["_this"] -rebuilt : ScopeId(0): [] -Bindings mismatch: -after transform: ScopeId(2): [] -rebuilt : ScopeId(1): ["_this"] -Symbol scope ID mismatch for "_this": -after transform: SymbolId(0): ScopeId(0) -rebuilt : SymbolId(0): ScopeId(1) - -* async-generators/static-method/input.js -Bindings mismatch: -after transform: ScopeId(0): ["C", "_this"] -rebuilt : ScopeId(0): ["C"] -Bindings mismatch: -after transform: ScopeId(3): [] -rebuilt : ScopeId(2): ["_this"] -Symbol scope ID mismatch for "_this": -after transform: SymbolId(1): ScopeId(0) -rebuilt : SymbolId(1): ScopeId(2) - +# babel-plugin-transform-async-generator-functions (18/19) * nested/arrows-in-declaration/input.js x Output mismatch @@ -1652,7 +1619,7 @@ x Output mismatch x Output mismatch -# babel-plugin-transform-async-to-generator (9/24) +# babel-plugin-transform-async-to-generator (10/24) * assumption-ignoreFunctionLength-true/basic/input.mjs x Output mismatch @@ -1695,17 +1662,6 @@ x Output mismatch * regression/in-uncompiled-class-fields/input.js x Output mismatch -* regression/regression-2765/input.js -Bindings mismatch: -after transform: ScopeId(10): [] -rebuilt : ScopeId(6): ["_this2"] -Bindings mismatch: -after transform: ScopeId(4): ["_this2", "c"] -rebuilt : ScopeId(7): ["c"] -Symbol scope ID mismatch for "_this2": -after transform: SymbolId(8): ScopeId(4) -rebuilt : SymbolId(6): ScopeId(6) - # babel-plugin-transform-exponentiation-operator (3/4) * regression/4349/input.js