Skip to content

Commit 2abe1fa

Browse files
committed
feature: @putout/plugin-conditions: merge-if-with-else
1 parent 70b56da commit 2abe1fa

File tree

10 files changed

+103
-1
lines changed

10 files changed

+103
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,18 @@ isFn(1);
14971497

14981498
</details>
14991499

1500+
<details><summary>merge <code>if</code> with <code>else</code></summary>
1501+
1502+
```diff
1503+
-if (!matchFn)
1504+
+if (!matchFn || matchFn(options))
1505+
fix(from, to, path);
1506+
-else if (matchFn(options))
1507+
- fix(from, to, path);
1508+
```
1509+
1510+
</details>
1511+
15001512
<details><summary>convert <code>anonymous</code> to <code>arrow function</code></summary>
15011513

15021514
```diff

packages/plugin-conditions/README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ npm i @putout/plugin-conditions -D
2222
-[convert-arrow-to-condition](#convert-arrow-to-condition);
2323
-[evaluate](#evaluate);
2424
-[merge-if-statements](#merge-if-statements);
25+
-[merge-if-with-else](#merge-if-with-else);
2526
-[reverse](#reverse);
2627
-[remove-boolean](#remove-boolean);
2728
-[remove-constant](#remove-constant);
@@ -53,6 +54,7 @@ npm i @putout/plugin-conditions -D
5354
"conditions/remove-useless-loop-condition": "on",
5455
"conditions/remove-same-values-condition": "on",
5556
"conditions/merge-if-statements": "on",
57+
"conditions/merge-if-with-else": "on",
5658
"conditions/simplify": "on",
5759
"conditions/wrap-with-block": "on"
5860
}
@@ -352,7 +354,7 @@ alert('hello');
352354
353355
## merge-if-statements
354356
355-
> The `if` statement executes a statement `if` a specified condition is truthy.
357+
> Multiple `if...else` statements can be nested to create an else if clause
356358
>
357359
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)
358360
@@ -371,6 +373,28 @@ if (a > b && b < c)
371373
console.log('hello');
372374
```
373375
376+
## merge-if-with-else
377+
378+
> The `if` statement executes a statement `if` a specified condition is truthy.
379+
>
380+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)
381+
382+
### ❌ Example of incorrect code
383+
384+
```js
385+
if (!matchFn)
386+
fix(from, to, path);
387+
else if (matchFn(options))
388+
fix(from, to, path);
389+
```
390+
391+
### ✅ Example of correct code
392+
393+
```js
394+
if (!matchFn || matchFn(options))
395+
fix(from, to, path);
396+
```
397+
374398
## remove-useless-else
375399
376400
> You can skip the `else` block if your `if` block always executes a `return` statement, it makes code a lot easier to read.

packages/plugin-conditions/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const convertArrowToCondition = require('./convert-arrow-to-condition');
1717
const reverseCondition = require('./reverse-condition');
1818
const wrapWithBlock = require('./wrap-with-block');
1919
const removeUselessLoopCondition = require('./remove-useless-loop-condition');
20+
const mergeIfWithElse = require('./merge-if-with-else');
2021

2122
module.exports.rules = {
2223
'apply-comparison-order': applyComparisonOrder,
@@ -36,4 +37,5 @@ module.exports.rules = {
3637
'reverse-condition': reverseCondition,
3738
'wrap-with-block': wrapWithBlock,
3839
'remove-useless-loop-condition': removeUselessLoopCondition,
40+
'merge-if-with-else': mergeIfWithElse,
3941
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if (!matchFn || matchFn(options))
2+
fix(from, to, path);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if (!matchFn)
2+
fix(from, to, path);
3+
else if (matchFn(options))
4+
fix(from, to, path);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const {types, operator} = require('putout');
4+
5+
const {compare} = operator;
6+
const {logicalExpression} = types;
7+
8+
module.exports.report = () => `Merge 'if' with 'else' when the body is the same`;
9+
10+
module.exports.fix = (path) => {
11+
const {test: testFirst, alternate} = path.node;
12+
const {test: testSecond} = alternate;
13+
14+
path.node.test = logicalExpression('||', testFirst, testSecond);
15+
delete path.node.alternate;
16+
};
17+
module.exports.traverse = ({push}) => ({
18+
IfStatement: (path) => {
19+
if (!path.node.alternate)
20+
return;
21+
22+
if (!compare(path.node.consequent, path.node.alternate.consequent))
23+
return;
24+
25+
push(path);
26+
},
27+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['merge-if-with-else', plugin],
9+
],
10+
});
11+
12+
test('conditions: merge-if-with-else: report', (t) => {
13+
t.report('merge-if-with-else', `Merge 'if' with 'else' when the body is the same`);
14+
t.end();
15+
});
16+
17+
test('conditions: merge-if-with-else: transform', (t) => {
18+
t.transform('merge-if-with-else');
19+
t.end();
20+
});

packages/plugin-conditions/test/conditions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,8 @@ test('plugin-conditions: transform: remove-useless-loop-condition', (t) => {
9393
t.transform('remove-useless-loop-condition');
9494
t.end();
9595
});
96+
97+
test('plugin-conditions: transform: merge-if-with-else', (t) => {
98+
t.transform('merge-if-with-else');
99+
t.end();
100+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if (!matchFn || matchFn(options))
2+
fix(from, to, path);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if (!matchFn)
2+
fix(from, to, path);
3+
else if (matchFn(options))
4+
fix(from, to, path);

0 commit comments

Comments
 (0)