@putout/plugin-conditions 7.1.1 → 7.3.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
@@ -22,11 +22,13 @@ npm i @putout/plugin-conditions -D
22
22
  - ✅ [convert-arrow-to-condition](#convert-arrow-to-condition);
23
23
  - ✅ [evaluate](#evaluate);
24
24
  - ✅ [merge-if-statements](#merge-if-statements);
25
+ - ✅ [merge-if-with-else](#merge-if-with-else);
25
26
  - ✅ [reverse](#reverse);
26
27
  - ✅ [remove-boolean](#remove-boolean);
27
28
  - ✅ [remove-constant](#remove-constant);
28
29
  - ✅ [remove-same-values-condition](hremove-same-values-condition);
29
- - ✅ [remove-useless-else](#remove-ureless-else);
30
+ - ✅ [remove-useless-else](#remove-useless-else);
31
+ - ✅ [remove-useless-loop-condition](#remove-useless-loop-condition);
30
32
  - ✅ [remove-zero](#remove-zero);
31
33
  - ✅ [simplify](#simplify);
32
34
  - ✅ [wrap-with-block](#wrap-with-block);
@@ -49,8 +51,10 @@ npm i @putout/plugin-conditions -D
49
51
  "conditions/remove-constant": "on",
50
52
  "conditions/remove-zero": "on",
51
53
  "conditions/remove-useless-else": "on",
54
+ "conditions/remove-useless-loop-condition": "on",
52
55
  "conditions/remove-same-values-condition": "on",
53
56
  "conditions/merge-if-statements": "on",
57
+ "conditions/merge-if-with-else": "on",
54
58
  "conditions/simplify": "on",
55
59
  "conditions/wrap-with-block": "on"
56
60
  }
@@ -350,7 +354,7 @@ alert('hello');
350
354
 
351
355
  ## merge-if-statements
352
356
 
353
- > 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
354
358
  >
355
359
  > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)
356
360
 
@@ -369,6 +373,28 @@ if (a > b && b < c)
369
373
  console.log('hello');
370
374
  ```
371
375
 
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
+
372
398
  ## remove-useless-else
373
399
 
374
400
  > You can skip the `else` block if your `if` block always executes a `return` statement, it makes code a lot easier to read.
@@ -406,6 +432,25 @@ Linter | Rule | Fix
406
432
  🐊 **Putout** | [`conditions/remove-useless-else`](https://github.com/coderaiser/putout/tree/master/packages/plugin-conditions#remove-useless-else) | ✅
407
433
  ⏣ **ESLint** | [`no-else-return`](https://eslint.org/docs/rules/no-else-return) | ✅
408
434
 
435
+ ## remove-useless-loop-condition
436
+
437
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/eefe12da089fda5554d394746ee25a6f/7a3c467cb95a5909f6d7f7d8228b5003da674113).
438
+
439
+ ### ❌ Example of incorrect code
440
+
441
+ ```js
442
+ while (currentDirPath = getParentDirectory(currentDirPath)) {
443
+ if (!currentDirPath)
444
+ break;
445
+ }
446
+ ```
447
+
448
+ ### ✅ Example of correct code
449
+
450
+ ```js
451
+ while (currentDirPath = getParentDirectory(currentDirPath)) {}
452
+ ```
453
+
409
454
  ## remove-same-values-condition
410
455
 
411
456
  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,8 @@ 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');
20
+ const mergeIfWithElse = require('./merge-if-with-else');
19
21
 
20
22
  module.exports.rules = {
21
23
  'apply-comparison-order': applyComparisonOrder,
@@ -34,4 +36,6 @@ module.exports.rules = {
34
36
  'convert-arrow-to-condition': convertArrowToCondition,
35
37
  'reverse-condition': reverseCondition,
36
38
  'wrap-with-block': wrapWithBlock,
39
+ 'remove-useless-loop-condition': removeUselessLoopCondition,
40
+ 'merge-if-with-else': mergeIfWithElse,
37
41
  };
@@ -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,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
+ });
@@ -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
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-conditions",
3
- "version": "7.1.1",
3
+ "version": "7.3.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,13 +32,13 @@
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": "^24.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"