diff --git a/crates/oxc_transformer/src/es2018/object_rest_spread.rs b/crates/oxc_transformer/src/es2018/object_rest_spread.rs index 79e81ee23dfd5..ce93354ce32b1 100644 --- a/crates/oxc_transformer/src/es2018/object_rest_spread.rs +++ b/crates/oxc_transformer/src/es2018/object_rest_spread.rs @@ -561,9 +561,24 @@ impl<'a> ObjectRestSpread<'a, '_> { // Transform `(...x) => {}`. fn transform_arrow(arrow: &mut ArrowFunctionExpression<'a>, ctx: &mut TraverseCtx<'a>) { let scope_id = arrow.scope_id(); - let mut replaced = false; for param in &mut arrow.params.items { if Self::has_nested_object_rest(¶m.pattern) { + // `({ ...args }) => { args }` + if arrow.expression { + arrow.expression = false; + + debug_assert!(arrow.body.statements.len() == 1); + + let Statement::ExpressionStatement(stmt) = arrow.body.statements.pop().unwrap() + else { + unreachable!( + "`arrow.expression` is true, which means it has only one ExpressionStatement." + ); + }; + let return_stmt = + ctx.ast.statement_return(stmt.span, Some(stmt.unbox().expression)); + arrow.body.statements.push(return_stmt); + } Self::replace_rest_element( VariableDeclarationKind::Var, &mut param.pattern, @@ -571,12 +586,8 @@ impl<'a> ObjectRestSpread<'a, '_> { scope_id, ctx, ); - replaced = true; } } - if replaced && arrow.expression { - arrow.expression = false; - } } // Transform `try {} catch ({...x}) {}`. diff --git a/tasks/transform_conformance/snapshots/oxc.snap.md b/tasks/transform_conformance/snapshots/oxc.snap.md index 5ce5fa4c76254..c386d8cd34df2 100644 --- a/tasks/transform_conformance/snapshots/oxc.snap.md +++ b/tasks/transform_conformance/snapshots/oxc.snap.md @@ -1,6 +1,6 @@ commit: 1d4546bc -Passed: 178/296 +Passed: 179/297 # All Passed: * babel-plugin-transform-class-static-block diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/with-arrow-function-expression/input.js b/tasks/transform_conformance/tests/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/with-arrow-function-expression/input.js new file mode 100644 index 0000000000000..b5c7fc4a83c55 --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/with-arrow-function-expression/input.js @@ -0,0 +1 @@ +const fn = ({ ...args }) => args; diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/with-arrow-function-expression/output.js b/tasks/transform_conformance/tests/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/with-arrow-function-expression/output.js new file mode 100644 index 0000000000000..b924c442d8d28 --- /dev/null +++ b/tasks/transform_conformance/tests/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/with-arrow-function-expression/output.js @@ -0,0 +1,7 @@ +const fn = (_ref) => { + let args = babelHelpers.extends( + {}, + (babelHelpers.objectDestructuringEmpty(_ref), _ref), + ); + return args; +};