Skip to content

Commit

Permalink
feature: @putout/plugin-minify: convert-if-to-logical: parens (putout…
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jun 5, 2024
1 parent 4a87189 commit 1a05e31
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(var1 || (var2 && var3)) && console.log('test');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (var1 || (var2 && var3)) {
console.log("test")
}
13 changes: 11 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 @@ -9,6 +9,7 @@ const {
ConditionalExpression,
UnaryExpression,
isConditionalExpression,
isLogicalExpression,
} = types;

const parseExpressions = ({body}) => body.map(getExpression).filter(Boolean);
Expand Down Expand Up @@ -65,8 +66,16 @@ export const replace = () => ({
if (!__b.body.length)
return '__a';

if (__b.body.length === 1)
return LogicalExpression('&&', __a, __b.body[0].expression);
if (__b.body.length === 1) {
const {expression} = __b.body[0];

if (isLogicalExpression(__a))
__a.extra = {
parenthesized: true,
};

return LogicalExpression('&&', __a, expression);
}

const expressions = parseExpressions(__b);

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

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

0 comments on commit 1a05e31

Please sign in to comment.