diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index fa5f0a1f3..adc68ed12 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -2488,4 +2488,28 @@ describe("simplify-plugin", () => { } ` ); + + thePlugin( + "should convert simple arrow block to expression", + ` + const a = () => {return (3, 4);}; + const b = () => {return 3;}; + `, + ` + const a = () => (3, 4); + const b = () => 3; + ` + ); + + thePlugin( + "should NOT convert empty arrow block to expression", + ` + const a = () => {}; + const b = () => {return;}; + `, + ` + const a = () => {}; + const b = () => {}; + ` + ); }); diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index 59c853be7..033e03358 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -780,6 +780,16 @@ module.exports = ({ types: t }) => { exit(path) { const { node, parent } = path; + if ( + t.isArrowFunctionExpression(parent) && + node.body.length === 1 && + t.isReturnStatement(node.body[0]) && + node.body[0].argument !== null + ) { + path.replaceWith(node.body[0].argument); + return; + } + if (needsBlock(node, parent)) { return; }