diff --git a/crates/oxc_transformer/src/common/arrow_function_converter.rs b/crates/oxc_transformer/src/common/arrow_function_converter.rs index 1672f79af4c79f..e1b80ff69c4c5e 100644 --- a/crates/oxc_transformer/src/common/arrow_function_converter.rs +++ b/crates/oxc_transformer/src/common/arrow_function_converter.rs @@ -202,12 +202,9 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> { self.this_var_stack.push(None); self.arguments_var_stack.push(None); - if self.is_async_only() { - let is_async_method = func.r#async && Self::is_class_method_like_ancestor(ctx.parent()); - self.arguments_needs_transform_stack.push(is_async_method); - if is_async_method { - self.super_methods = Some(FxIndexMap::default()); - } + if self.is_async_only() && func.r#async && Self::is_class_method_like_ancestor(ctx.parent()) + { + self.super_methods = Some(FxIndexMap::default()); } } @@ -233,11 +230,6 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> { }; let this_var = self.this_var_stack.pop(); let arguments_var = self.arguments_var_stack.pop(); - - if self.is_async_only() { - self.arguments_needs_transform_stack.pop(); - } - self.insert_variable_statement_at_the_top_of_statements( scope_id, &mut body.statements, @@ -258,11 +250,19 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> { } } - fn exit_arrow_function_expression( - &mut self, - _arrow: &mut ArrowFunctionExpression<'a>, - _ctx: &mut TraverseCtx<'a>, - ) { + fn enter_function_body(&mut self, _body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) { + if self.is_async_only() { + // Ignore arrow functions + if let Ancestor::FunctionBody(func) = ctx.parent() { + let is_async_method = + *func.r#async() && Self::is_class_method_like_ancestor(ctx.ancestor(1)); + self.arguments_needs_transform_stack.push(is_async_method); + } + } + } + + fn exit_function_body(&mut self, _body: &mut FunctionBody<'a>, _ctx: &mut TraverseCtx<'a>) { + // This covers exiting either a `Function` or an `ArrowFunctionExpression` if self.is_async_only() { self.arguments_needs_transform_stack.pop(); } diff --git a/crates/oxc_transformer/src/common/mod.rs b/crates/oxc_transformer/src/common/mod.rs index 641fd8ce9d5eb5..c84b29e37c8fc2 100644 --- a/crates/oxc_transformer/src/common/mod.rs +++ b/crates/oxc_transformer/src/common/mod.rs @@ -80,12 +80,12 @@ impl<'a, 'ctx> Traverse<'a> for Common<'a, 'ctx> { self.arrow_function_converter.enter_arrow_function_expression(arrow, ctx); } - fn exit_arrow_function_expression( - &mut self, - arrow: &mut ArrowFunctionExpression<'a>, - ctx: &mut TraverseCtx<'a>, - ) { - self.arrow_function_converter.exit_arrow_function_expression(arrow, ctx); + fn enter_function_body(&mut self, body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) { + self.arrow_function_converter.enter_function_body(body, ctx); + } + + fn exit_function_body(&mut self, body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) { + self.arrow_function_converter.exit_function_body(body, ctx); } fn enter_static_block(&mut self, block: &mut StaticBlock<'a>, ctx: &mut TraverseCtx<'a>) { diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 1f8462080eaf8f..72aa494b504e16 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -315,6 +315,14 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { self.common.exit_function(func, ctx); } + fn enter_function_body(&mut self, body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) { + self.common.enter_function_body(body, ctx); + } + + fn exit_function_body(&mut self, body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) { + self.common.exit_function_body(body, ctx); + } + fn enter_jsx_element(&mut self, node: &mut JSXElement<'a>, ctx: &mut TraverseCtx<'a>) { if let Some(typescript) = self.x0_typescript.as_mut() { typescript.enter_jsx_element(node, ctx); @@ -429,8 +437,6 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { .push(ctx.ast.statement_return(SPAN, Some(statement.unbox().expression))); arrow.expression = false; } - - self.common.exit_arrow_function_expression(arrow, ctx); } fn exit_statements( diff --git a/tasks/coverage/snapshots/semantic_test262.snap b/tasks/coverage/snapshots/semantic_test262.snap index 4620416a60ea08..756fc9e92226eb 100644 --- a/tasks/coverage/snapshots/semantic_test262.snap +++ b/tasks/coverage/snapshots/semantic_test262.snap @@ -2,7 +2,7 @@ commit: 06454619 semantic_test262 Summary: AST Parsed : 43851/43851 (100.00%) -Positive Passed: 43039/43851 (98.15%) +Positive Passed: 43040/43851 (98.15%) tasks/coverage/test262/test/annexB/language/function-code/if-decl-else-decl-a-func-block-scoping.js semantic error: Symbol scope ID mismatch for "f": after transform: SymbolId(3): ScopeId(4294967294) @@ -6050,17 +6050,6 @@ Symbol scope ID mismatch for "x": after transform: SymbolId(4): ScopeId(7) rebuilt : SymbolId(5): ScopeId(4) -tasks/coverage/test262/test/language/statements/class/static-init-arguments-methods.js -semantic error: Symbol reference IDs mismatch for "_arguments": -after transform: SymbolId(15): [ReferenceId(16), ReferenceId(18)] -rebuilt : SymbolId(16): [ReferenceId(21)] -Reference symbol mismatch for "_arguments": -after transform: SymbolId(15) "_arguments" -rebuilt : -Unresolved references mismatch: -after transform: ["arguments", "assert", "require"] -rebuilt : ["_arguments", "arguments", "assert", "require"] - tasks/coverage/test262/test/language/statements/for-await-of/async-from-sync-iterator-continuation-abrupt-completion-get-constructor.js semantic error: Bindings mismatch: after transform: ScopeId(1): ["_didIteratorError", "_iterator", "_iteratorAbruptCompletion", "_iteratorError", "_step", "p"]