Skip to content

Commit c7b1056

Browse files
authored
fix(transformer/object-rest-spread): arrow function expressions with object rest patterns (#12185)
close: #12184 I couldn't figure out where **exactly** to put tests regarding object rest spread and arrow function transformation. Probably somewhere underneath tasks/transform_conformance
1 parent 2f9bd11 commit c7b1056

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

crates/oxc_transformer/src/es2018/object_rest_spread.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,22 +561,33 @@ impl<'a> ObjectRestSpread<'a, '_> {
561561
// Transform `(...x) => {}`.
562562
fn transform_arrow(arrow: &mut ArrowFunctionExpression<'a>, ctx: &mut TraverseCtx<'a>) {
563563
let scope_id = arrow.scope_id();
564-
let mut replaced = false;
565564
for param in &mut arrow.params.items {
566565
if Self::has_nested_object_rest(&param.pattern) {
566+
// `({ ...args }) => { args }`
567+
if arrow.expression {
568+
arrow.expression = false;
569+
570+
debug_assert!(arrow.body.statements.len() == 1);
571+
572+
let Statement::ExpressionStatement(stmt) = arrow.body.statements.pop().unwrap()
573+
else {
574+
unreachable!(
575+
"`arrow.expression` is true, which means it has only one ExpressionStatement."
576+
);
577+
};
578+
let return_stmt =
579+
ctx.ast.statement_return(stmt.span, Some(stmt.unbox().expression));
580+
arrow.body.statements.push(return_stmt);
581+
}
567582
Self::replace_rest_element(
568583
VariableDeclarationKind::Var,
569584
&mut param.pattern,
570585
&mut arrow.body.statements,
571586
scope_id,
572587
ctx,
573588
);
574-
replaced = true;
575589
}
576590
}
577-
if replaced && arrow.expression {
578-
arrow.expression = false;
579-
}
580591
}
581592

582593
// Transform `try {} catch ({...x}) {}`.

tasks/transform_conformance/snapshots/oxc.snap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
commit: 1d4546bc
22

3-
Passed: 178/296
3+
Passed: 179/297
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const fn = ({ ...args }) => args;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const fn = (_ref) => {
2+
let args = babelHelpers.extends(
3+
{},
4+
(babelHelpers.objectDestructuringEmpty(_ref), _ref),
5+
);
6+
return args;
7+
};

0 commit comments

Comments
 (0)