@putout/plugin-conditions 7.2.0 → 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,6 +22,7 @@ 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);
@@ -53,6 +54,7 @@ npm i @putout/plugin-conditions -D
53
54
  "conditions/remove-useless-loop-condition": "on",
54
55
  "conditions/remove-same-values-condition": "on",
55
56
  "conditions/merge-if-statements": "on",
57
+ "conditions/merge-if-with-else": "on",
56
58
  "conditions/simplify": "on",
57
59
  "conditions/wrap-with-block": "on"
58
60
  }
@@ -352,7 +354,7 @@ alert('hello');
352
354
 
353
355
  ## merge-if-statements
354
356
 
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
356
358
  >
357
359
  > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)
358
360
 
@@ -371,6 +373,28 @@ if (a > b && b < c)
371
373
  console.log('hello');
372
374
  ```
373
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
+
374
398
  ## remove-useless-else
375
399
 
376
400
  > You can skip the `else` block if your `if` block always executes a `return` statement, it makes code a lot easier to read.
package/lib/index.js CHANGED
@@ -17,6 +17,7 @@ const convertArrowToCondition = require('./convert-arrow-to-condition');
17
17
  const reverseCondition = require('./reverse-condition');
18
18
  const wrapWithBlock = require('./wrap-with-block');
19
19
  const removeUselessLoopCondition = require('./remove-useless-loop-condition');
20
+ const mergeIfWithElse = require('./merge-if-with-else');
20
21
 
21
22
  module.exports.rules = {
22
23
  'apply-comparison-order': applyComparisonOrder,
@@ -36,4 +37,5 @@ module.exports.rules = {
36
37
  'reverse-condition': reverseCondition,
37
38
  'wrap-with-block': wrapWithBlock,
38
39
  'remove-useless-loop-condition': removeUselessLoopCondition,
40
+ 'merge-if-with-else': mergeIfWithElse,
39
41
  };
@@ -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
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-conditions",
3
- "version": "7.2.0",
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",