Skip to content

Commit 1ae03f2

Browse files
committed
feature: @putout/plugin-minify: convert-if-to-logical: improve
1 parent 81f775e commit 1ae03f2

File tree

6 files changed

+27
-1
lines changed

6 files changed

+27
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const now = new Date();
2+
now.setDate(1);
3+
now.getMonth() < 11 ? (now.setMonth(now.getMonth() + 1)) : (now.setMonth(0), now.setFullYear(now.getFullYear() + 1));

packages/plugin-minify/lib/convert-if-to-logical/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const replace = () => ({
8484
if (!__b.body.length && isBlockStatement(__c))
8585
return LogicalExpression('&&', UnaryExpression('!', __a), __c.body[0].expression);
8686

87-
if (__b.body.length === 1)
87+
if (__b.body.length === 1 && __c.body.length === 1)
8888
return ConditionalExpression(__a, __b.body[0].expression, __c.body[0].expression);
8989

9090
const expressionsB = parseExpressions(__b);

packages/plugin-minify/lib/convert-if-to-logical/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,8 @@ test('plugin-minify: convert-if-to-logical: transform: nested-or', (t) => {
5252
t.transform('nested-or');
5353
t.end();
5454
});
55+
56+
test('plugin-minify: convert-if-to-logical: no transform: if-no-ternary', (t) => {
57+
t.noTransform('if-no-ternary');
58+
t.end();
59+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var a = new Date();
2+
a.setDate(1);
3+
a.getMonth() < 11 ? (a.setMonth(a.getMonth() + 1)) : (a.setMonth(0), a.setFullYear(a.getFullYear() + 1));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const now = new Date();
2+
now.setDate(1);
3+
if (now.getMonth() < 11) {
4+
// increase month by 1 if it is not currently December
5+
now.setMonth(now.getMonth() + 1);
6+
} else {
7+
// else, consider year rollover and go to next January
8+
now.setMonth(0);
9+
now.setFullYear(now.getFullYear() + 1);
10+
}

packages/plugin-minify/test/minify.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ test('plugin-minify: transform: if', (t) => {
105105
t.end();
106106
});
107107

108+
test('plugin-minify: transform: if-no-ternary', (t) => {
109+
t.transform('if-no-ternary');
110+
t.end();
111+
});
112+
108113
test('plugin-minify: transform: merge-loops', (t) => {
109114
t.transform('merge-loops');
110115
t.end();

0 commit comments

Comments
 (0)