Skip to content

Commit 5fa44bd

Browse files
committed
feature: @putout/plugin-conditions: remove-useless-loop-condition
1 parent 10da63f commit 5fa44bd

File tree

9 files changed

+101
-1
lines changed

9 files changed

+101
-1
lines changed

packages/plugin-conditions/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ npm i @putout/plugin-conditions -D
2626
-[remove-boolean](#remove-boolean);
2727
-[remove-constant](#remove-constant);
2828
-[remove-same-values-condition](hremove-same-values-condition);
29-
-[remove-useless-else](#remove-ureless-else);
29+
-[remove-useless-else](#remove-useless-else);
30+
-[remove-useless-loop-condition](#remove-useless-loop-condition);
3031
-[remove-zero](#remove-zero);
3132
-[simplify](#simplify);
3233
-[wrap-with-block](#wrap-with-block);
@@ -49,6 +50,7 @@ npm i @putout/plugin-conditions -D
4950
"conditions/remove-constant": "on",
5051
"conditions/remove-zero": "on",
5152
"conditions/remove-useless-else": "on",
53+
"conditions/remove-useless-loop-condition": "on",
5254
"conditions/remove-same-values-condition": "on",
5355
"conditions/merge-if-statements": "on",
5456
"conditions/simplify": "on",
@@ -406,6 +408,25 @@ Linter | Rule | Fix
406408
🐊 **Putout** | [`conditions/remove-useless-else`](https://github.com/coderaiser/putout/tree/master/packages/plugin-conditions#remove-useless-else) | ✅
407409
⏣ **ESLint** | [`no-else-return`](https://eslint.org/docs/rules/no-else-return) | ✅
408410
411+
## remove-useless-loop-condition
412+
413+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/eefe12da089fda5554d394746ee25a6f/7a3c467cb95a5909f6d7f7d8228b5003da674113).
414+
415+
### ❌ Example of incorrect code
416+
417+
```js
418+
while (currentDirPath = getParentDirectory(currentDirPath)) {
419+
if (!currentDirPath)
420+
break;
421+
}
422+
```
423+
424+
### ✅ Example of correct code
425+
426+
```js
427+
while (currentDirPath = getParentDirectory(currentDirPath)) {}
428+
```
429+
409430
## remove-same-values-condition
410431
411432
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e537d4ec636d4a9b849063a8326b70ae/661041b3fbb1e3678bf7f828e4c8bf6ca723f89d).

packages/plugin-conditions/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const addReturn = require('./add-return');
1616
const convertArrowToCondition = require('./convert-arrow-to-condition');
1717
const reverseCondition = require('./reverse-condition');
1818
const wrapWithBlock = require('./wrap-with-block');
19+
const removeUselessLoopCondition = require('./remove-useless-loop-condition');
1920

2021
module.exports.rules = {
2122
'apply-comparison-order': applyComparisonOrder,
@@ -34,4 +35,5 @@ module.exports.rules = {
3435
'convert-arrow-to-condition': convertArrowToCondition,
3536
'reverse-condition': reverseCondition,
3637
'wrap-with-block': wrapWithBlock,
38+
'remove-useless-loop-condition': removeUselessLoopCondition,
3739
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
while (currentDirPath = getParentDirectory(currentDirPath)) {}
2+
3+
while (currentDirPath = getParentDirectory(currentDirPath)) {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
while (currentDirPath = getParentDirectory(currentDirPath)) {
2+
if (!currentDirPath)
3+
break;
4+
}
5+
6+
while (currentDirPath = getParentDirectory(currentDirPath)) {
7+
if (!currentDirPath)
8+
continue;
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const {operator} = require('putout');
4+
const {
5+
getTemplateValues,
6+
compare,
7+
} = operator;
8+
9+
const LOOP = 'while (__c = __d) __body';
10+
11+
module.exports.report = () => `Avoid useless loop condition`;
12+
13+
module.exports.match = () => ({
14+
'if (!__a) __b': ({__a}, path) => {
15+
const {parentPath} = path.parentPath;
16+
17+
if (!compare(parentPath, LOOP))
18+
return false;
19+
20+
const {__c} = getTemplateValues(parentPath, LOOP);
21+
22+
return compare(__a, __c);
23+
},
24+
});
25+
26+
module.exports.replace = () => ({
27+
'if (!__a) __b': '',
28+
});
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+
['remove-useless-loop-condition', plugin],
9+
],
10+
});
11+
12+
test('conditions: remove-useless-loop-condition: report', (t) => {
13+
t.report('remove-useless-loop-condition', `Avoid useless loop condition`);
14+
t.end();
15+
});
16+
17+
test('conditions: remove-useless-loop-condition: transform', (t) => {
18+
t.transform('remove-useless-loop-condition');
19+
t.end();
20+
});

packages/plugin-conditions/test/conditions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,8 @@ test('plugin-conditions: transform: wrap-with-block', (t) => {
8888
t.transform('wrap-with-block');
8989
t.end();
9090
});
91+
92+
test('plugin-conditions: transform: remove-useless-loop-condition', (t) => {
93+
t.transform('remove-useless-loop-condition');
94+
t.end();
95+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
while (currentDirPath = getParentDirectory(currentDirPath)) {}
2+
3+
while (currentDirPath = getParentDirectory(currentDirPath)) {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
while (currentDirPath = getParentDirectory(currentDirPath)) {
2+
if (!currentDirPath)
3+
break;
4+
}
5+
6+
while (currentDirPath = getParentDirectory(currentDirPath)) {
7+
if (!currentDirPath)
8+
continue;
9+
}

0 commit comments

Comments
 (0)