@putout/plugin-conditions 7.1.0 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,7 +26,8 @@ npm i @putout/plugin-conditions -D
26
26
  - ✅ [remove-boolean](#remove-boolean);
27
27
  - ✅ [remove-constant](#remove-constant);
28
28
  - ✅ [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);
30
31
  - ✅ [remove-zero](#remove-zero);
31
32
  - ✅ [simplify](#simplify);
32
33
  - ✅ [wrap-with-block](#wrap-with-block);
@@ -49,6 +50,7 @@ npm i @putout/plugin-conditions -D
49
50
  "conditions/remove-constant": "on",
50
51
  "conditions/remove-zero": "on",
51
52
  "conditions/remove-useless-else": "on",
53
+ "conditions/remove-useless-loop-condition": "on",
52
54
  "conditions/remove-same-values-condition": "on",
53
55
  "conditions/merge-if-statements": "on",
54
56
  "conditions/simplify": "on",
@@ -406,6 +408,25 @@ Linter | Rule | Fix
406
408
  🐊 **Putout** | [`conditions/remove-useless-else`](https://github.com/coderaiser/putout/tree/master/packages/plugin-conditions#remove-useless-else) | ✅
407
409
  ⏣ **ESLint** | [`no-else-return`](https://eslint.org/docs/rules/no-else-return) | ✅
408
410
 
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
+
409
430
  ## remove-same-values-condition
410
431
 
411
432
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e537d4ec636d4a9b849063a8326b70ae/661041b3fbb1e3678bf7f828e4c8bf6ca723f89d).
@@ -4,8 +4,8 @@ const {types, operator} = require('putout');
4
4
  const {replaceWith} = operator;
5
5
  const {
6
6
  isBlockStatement,
7
- BlockStatement,
8
7
  isVariableDeclaration,
8
+ blockStatement,
9
9
  } = types;
10
10
 
11
11
  module.exports.report = () => `Use consistent blocks`;
@@ -19,7 +19,7 @@ module.exports.fix = (path) => {
19
19
  continue;
20
20
 
21
21
  const {node} = path;
22
- replaceWith(path, BlockStatement([node]));
22
+ replaceWith(path, blockStatement([node]));
23
23
  }
24
24
  else
25
25
  for (const path of paths) {
@@ -4,12 +4,12 @@ const {types, operator} = require('putout');
4
4
 
5
5
  const {replaceWith, compute} = operator;
6
6
 
7
- const {isIdentifier, BooleanLiteral} = types;
7
+ const {isIdentifier, booleanLiteral} = types;
8
8
 
9
9
  module.exports.report = () => 'Avoid constant conditions';
10
10
 
11
11
  module.exports.fix = ({path, value}) => {
12
- replaceWith(path, BooleanLiteral(value));
12
+ replaceWith(path, booleanLiteral(value));
13
13
  };
14
14
 
15
15
  module.exports.traverse = ({push}) => ({
package/lib/index.js CHANGED
@@ -16,6 +16,7 @@ const addReturn = require('./add-return');
16
16
  const convertArrowToCondition = require('./convert-arrow-to-condition');
17
17
  const reverseCondition = require('./reverse-condition');
18
18
  const wrapWithBlock = require('./wrap-with-block');
19
+ const removeUselessLoopCondition = require('./remove-useless-loop-condition');
19
20
 
20
21
  module.exports.rules = {
21
22
  'apply-comparison-order': applyComparisonOrder,
@@ -34,4 +35,5 @@ module.exports.rules = {
34
35
  'convert-arrow-to-condition': convertArrowToCondition,
35
36
  'reverse-condition': reverseCondition,
36
37
  'wrap-with-block': wrapWithBlock,
38
+ 'remove-useless-loop-condition': removeUselessLoopCondition,
37
39
  };
@@ -3,7 +3,7 @@
3
3
  const {types, operator} = require('putout');
4
4
 
5
5
  const {replaceWith} = operator;
6
- const {LogicalExpression} = types;
6
+ const {logicalExpression} = types;
7
7
 
8
8
  module.exports.report = () => `Merge 'if' statements`;
9
9
 
@@ -12,7 +12,7 @@ module.exports.fix = ({path, consequentPath}) => {
12
12
  const left = testPath.node;
13
13
  const right = consequentPath.node.test;
14
14
 
15
- replaceWith(testPath, LogicalExpression('&&', left, right));
15
+ replaceWith(testPath, logicalExpression('&&', left, right));
16
16
 
17
17
  replaceWith(path.get('consequent'), consequentPath.get('consequent'));
18
18
  };
@@ -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
+ });
@@ -6,4 +6,5 @@ module.exports.replace = () => ({
6
6
  '!(__a > __b)': '__a <= __b',
7
7
  '!(__a !== __b && __c === __d)': '__a === __b || __c !== __d',
8
8
  '!(__a !== __b || __c !== __d)': '__a === __b && __c === __d',
9
+ '!(__a || __b)': '!__a && !__b',
9
10
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-conditions",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds support of conditions transformations",
@@ -32,19 +32,19 @@
32
32
  "conditions"
33
33
  ],
34
34
  "devDependencies": {
35
+ "@putout/eslint-flat": "^2.0.0",
35
36
  "@putout/plugin-for-of": "*",
36
- "@putout/test": "^11.0.0",
37
+ "@putout/test": "^12.0.0",
37
38
  "c8": "^10.0.0",
38
39
  "eslint": "^9.0.0",
39
40
  "eslint-plugin-n": "^17.0.0",
40
- "eslint-plugin-putout": "^23.0.0",
41
- "lerna": "^6.0.1",
41
+ "eslint-plugin-putout": "^25.0.1",
42
42
  "madrun": "^10.0.0",
43
43
  "montag": "^1.2.1",
44
44
  "nodemon": "^3.0.1"
45
45
  },
46
46
  "peerDependencies": {
47
- "putout": ">=37"
47
+ "putout": ">=38"
48
48
  },
49
49
  "license": "MIT",
50
50
  "engines": {