diff --git a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs index b3fcc910a..40e863a7a 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs @@ -121,6 +121,26 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax { self.changed = true; } } + // `() => { return foo })` -> `() => foo` + Expression::ArrowFunctionExpression(arrow_expr) => { + if !arrow_expr.expression + && arrow_expr.body.directives.is_empty() + && arrow_expr.body.statements.len() == 1 + { + if let Some(body) = arrow_expr.body.statements.first_mut() { + if let Statement::ReturnStatement(ret_stmt) = body { + let return_stmt_arg = + ret_stmt.argument.as_mut().map(|arg| ctx.ast.move_expression(arg)); + + if let Some(return_stmt_arg) = return_stmt_arg { + *body = ctx.ast.statement_expression(SPAN, return_stmt_arg); + arrow_expr.expression = true; + self.changed = true; + } + } + } + } + } _ => {} } } @@ -963,4 +983,10 @@ mod test { test("1/x * (y/1 * (1/z))", "1/x * (y/1) * (1/z)"); test_same("1/x * (y/1) * (1/z)"); } + + #[test] + fn test_fold_arrow_function_return() { + test("const foo = () => { return 'baz' }", "const foo = () => 'baz'"); + test_same("const foo = () => { foo; return 'baz' }"); + } } diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 8ca5db9ce..375432cb7 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -12,7 +12,7 @@ Original | Minified | esbuild | Gzip | esbuild 555.77 kB | 276.49 kB | 270.13 kB | 91.15 kB | 90.80 kB | d3.js -1.01 MB | 467.63 kB | 458.89 kB | 126.75 kB | 126.71 kB | bundle.min.js +1.01 MB | 467.60 kB | 458.89 kB | 126.74 kB | 126.71 kB | bundle.min.js 1.25 MB | 662.86 kB | 646.76 kB | 164.00 kB | 163.73 kB | three.js