Skip to content

Commit

Permalink
Convert simple arrow function body to expression (#616)
Browse files Browse the repository at this point in the history
* Convert simple arrow function body to expression

* Update following code review
  • Loading branch information
ashsearle authored and boopathi committed Jul 8, 2017
1 parent 83f8819 commit 5e1870b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/babel-plugin-minify-simplify/__tests__/simplify-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {};
`
);
});
10 changes: 10 additions & 0 deletions packages/babel-plugin-minify-simplify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,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;
}
Expand Down

0 comments on commit 5e1870b

Please sign in to comment.