From 033d9ab6e9cdd362a86d55cc9f43eeb596bd046a Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Mon, 11 Dec 2017 01:19:42 +0100 Subject: [PATCH] fix(simplify): fix conditional expression pattern match transformation logic (#754) --- .../__tests__/simplify-test.js | 30 +++++++++++++++++++ .../src/conditional-expression.js | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index 4f1ee3689..ade42b62b 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -1969,6 +1969,36 @@ describe("simplify-plugin", () => { ` ); + thePlugin( + "should simplify common conditional operations 2", + ` + function h1() { return a && b ? (foo(), true) : false } + `, + ` + function h1() { + return !!(a && b) && (foo(), true); + } + ` + ); + + thePlugin( + "should fix issue#689", + ` + function foo(object, property, value) { + if (object && property) { + object[property] = value; + return true; + } + return false; + } + `, + ` + function foo(object, property, value) { + return !!(object && property) && (object[property] = value, true); + } + ` + ); + // From UglifyJS thePlugin.inEachLine( "should simplify logical expression of the following forms of && by compressing to the right", diff --git a/packages/babel-plugin-minify-simplify/src/conditional-expression.js b/packages/babel-plugin-minify-simplify/src/conditional-expression.js index deb980e17..17dfc8a84 100644 --- a/packages/babel-plugin-minify-simplify/src/conditional-expression.js +++ b/packages/babel-plugin-minify-simplify/src/conditional-expression.js @@ -31,7 +31,7 @@ module.exports = t => { [EX, EX, true, (e, c) => or(not(e), c)], - [LE, EX, false, (e, c) => and(e, c)], + [LE, EX, false, (e, c) => and(notnot(e), c)], [EX, EX, false, (e, c) => and(notnot(e), c)] ]);