Skip to content

Commit

Permalink
feature: @putout/plugin-minify: convert-if-to-logical: nested-or: (co…
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jun 5, 2024
1 parent 492d1b5 commit 12b63fd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(var1 || (var2 && var3)) && (fn1(), var4 && fn2());
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if (var1 || (var2 && var3)) {
fn1();
if (var4) {
fn2();
}
}
6 changes: 4 additions & 2 deletions packages/plugin-minify/lib/convert-if-to-logical/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const replace = () => ({

const expressions = parseExpressions(__b);

return LogicalExpression('&&', __a, SequenceExpression(expressions));
return LogicalExpression('&&', maybeAddParens(__a), SequenceExpression(expressions));
},
'if (__a) __b; else __c': ({__a, __b, __c}) => {
if (!__b.body.length && isBlockStatement(__c))
Expand All @@ -100,9 +100,11 @@ export const replace = () => ({

function maybeAddParens(node) {
if (!isLogicalExpression(node))
return;
return node;

node.extra = {
parenthesized: true,
};

return node;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ test('plugin-minify: convert-if-to-logical: transform: or', (t) => {
t.transform('or');
t.end();
});

test('plugin-minify: convert-if-to-logical: transform: nested-or', (t) => {
t.transform('nested-or');
t.end();
});

0 comments on commit 12b63fd

Please sign in to comment.