From 4c15db9d93be708aeefba52d6f40e58ea00da476 Mon Sep 17 00:00:00 2001 From: Ash Searle Date: Mon, 3 Jul 2017 15:38:30 +0100 Subject: [PATCH 1/2] Convert simple arrow function body to expression --- .../__tests__/simplify-test.js | 24 +++++++++++++++++++ .../babel-plugin-minify-simplify/src/index.js | 11 +++++++++ 2 files changed, 35 insertions(+) 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..ad082e044 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -780,6 +780,17 @@ module.exports = ({ types: t }) => { exit(path) { const { node, parent } = path; + if ( + t.isArrowFunctionExpression(parent) && + node.body && + 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; } From 2c10177ef6882e50837fec0354f3ce1026b5b14f Mon Sep 17 00:00:00 2001 From: Ash Searle Date: Fri, 7 Jul 2017 20:56:18 +0100 Subject: [PATCH 2/2] Update following code review --- packages/babel-plugin-minify-simplify/src/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index ad082e044..033e03358 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -782,7 +782,6 @@ module.exports = ({ types: t }) => { if ( t.isArrowFunctionExpression(parent) && - node.body && node.body.length === 1 && t.isReturnStatement(node.body[0]) && node.body[0].argument !== null