@putout/plugin-conditions 4.3.0 → 5.0.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
@@ -19,6 +19,7 @@ npm i @putout/plugin-conditions -D
19
19
  - ✅ [apply-if](#apply-if);
20
20
  - ✅ [convert-comparison-to-boolean](#convert-comparison-to-boolean);
21
21
  - ✅ [convert-equal-to-strict-equal](#convert-equal-to-strict-equal);
22
+ - ✅ [convert-arrow-to-condition](#convert-arrow-to-condition);
22
23
  - ✅ [evaluate](#evaluate);
23
24
  - ✅ [merge-if-statements](#merge-if-statements);
24
25
  - ✅ [remove-boolean](#remove-boolean);
@@ -39,6 +40,7 @@ npm i @putout/plugin-conditions -D
39
40
  "conditions/add-return": "on",
40
41
  "conditions/convert-comparison-to-boolean": "on",
41
42
  "conditions/convert-equal-to-strict-equal": "on",
43
+ "conditions/convert-arrow-to-condition": "on",
42
44
  "conditions/evaluate": "on",
43
45
  "conditions/remove-boolean": "on",
44
46
  "conditions/remove-constant": "on",
@@ -56,7 +58,10 @@ npm i @putout/plugin-conditions -D
56
58
  >
57
59
  > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block)
58
60
 
59
- Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9035db21bae7b6c76d1dc875d4c74828/07edbfd755060ae263949caf08b31aa43609375a).
61
+ Check out in 🐊**Putout Editor**:
62
+
63
+ - [Replacer](https://putout.cloudcmd.io/#/gist/9035db21bae7b6c76d1dc875d4c74828/07edbfd755060ae263949caf08b31aa43609375a);
64
+ - [Traverser](https://putout.cloudcmd.io/#/gist/77f84f100d82707925c73c1784f589f2/25923f46973b99f72378104778499ff75b550d2c);
60
65
 
61
66
  ### ❌ Example of incorrect code
62
67
 
@@ -163,6 +168,22 @@ if (a)
163
168
  return false;
164
169
  ```
165
170
 
171
+ ## convert-arrow-to-condition
172
+
173
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/4d489393cd08dc771c6324eb0bb2e42f/9e85c4b7b7f2d0f82c660c6db618b31270d3a85e).
174
+
175
+ ## ❌ Example of incorrect code
176
+
177
+ ```js
178
+ if ((a) => b) {}
179
+ ```
180
+
181
+ ## ✅ Example of correct code
182
+
183
+ ```js
184
+ if (a <= b) {}
185
+ ```
186
+
166
187
  ## convert-comparison-to-boolean
167
188
 
168
189
  > Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal.
@@ -1,47 +1,108 @@
1
1
  'use strict';
2
2
 
3
- const {types} = require('putout');
3
+ const {types, operator} = require('putout');
4
+ const {replaceWith} = operator;
4
5
  const {
5
6
  isBlockStatement,
6
- isIfStatement,
7
+ BlockStatement,
7
8
  } = types;
8
9
 
9
10
  module.exports.report = () => `Use consistent blocks`;
10
11
 
11
- const notBlock = ({__b}) => !isBlockStatement(__b);
12
+ module.exports.fix = (path) => {
13
+ const paths = getAllNodes(path);
14
+
15
+ if (isAllBlocks(paths))
16
+ for (const path of paths) {
17
+ if (isBlockStatement(path))
18
+ continue;
19
+
20
+ const {node} = path;
21
+ replaceWith(path, BlockStatement([node]));
22
+ }
23
+ else
24
+ for (const path of paths) {
25
+ if (!isBlockStatement(path))
26
+ continue;
27
+
28
+ const [node] = path.node.body;
29
+ replaceWith(path, node);
30
+ }
31
+ };
12
32
 
13
- module.exports.match = () => ({
14
- 'if (__a) {__b} else {__c}': () => true,
15
- 'if (__a) {__b} else __c': () => true,
16
- 'if (__a) __b; else __body': notBlock,
17
- 'if (__a) __body; else __b': ({__b}) => !isIfStatement(__b),
18
- 'if (__a) {__b}': (vars, path) => {
19
- const {parentPath} = path;
20
- const __bPath = path.get('consequent.body.0');
21
- const {
22
- leadingComments,
23
- trailingComments,
24
- } = __bPath.node;
33
+ function isAllBlocks(paths) {
34
+ const counts = [];
35
+
36
+ for (const path of paths) {
37
+ const is = isBlockStatement(path);
25
38
 
26
- if (leadingComments || trailingComments)
27
- return false;
28
-
29
- if (!parentPath.isIfStatement())
39
+ if (is)
40
+ counts.push(path.node.body.length);
41
+ else
42
+ counts.push(Infinity);
43
+ }
44
+
45
+ for (const count of counts) {
46
+ if (count !== 1 && count !== Infinity)
30
47
  return true;
31
-
32
- return path !== parentPath.get('alternate');
33
- },
34
- });
48
+ }
49
+
50
+ return false;
51
+ }
35
52
 
36
- module.exports.replace = () => ({
37
- 'if (__a) {if (__b) {__c}}': 'if (__a) if (__b) __c',
38
- 'if (__a) {__b}': 'if (__a) __b',
39
- 'if (__a) {__b} else {__c}': 'if (__a) __b; else __c',
40
- 'if (__a) __b; else __body': 'if (__a) {__b} else __body',
41
- 'if (__a) __body; else __b': ({__b}, path) => {
42
- if (isBlockStatement(__b))
43
- return path;
44
-
45
- return 'if (__a) __body; else {__b}';
46
- },
47
- });
53
+ module.exports.include = () => [
54
+ 'IfStatement',
55
+ ];
56
+
57
+ module.exports.filter = (path) => {
58
+ if (path === path.parentPath.get('alternate'))
59
+ return;
60
+
61
+ const nodes = getAllNodes(path);
62
+ const blocks = [];
63
+
64
+ for (const node of nodes) {
65
+ const is = isBlockStatement(node);
66
+ blocks.push(is);
67
+ }
68
+
69
+ const count = blocks.filter(Boolean).length;
70
+
71
+ if (!count)
72
+ return false;
73
+
74
+ const couple = [];
75
+
76
+ for (const node of nodes) {
77
+ const is = isBlockStatement(node) && node.node.body.length > 1;
78
+ couple.push(is);
79
+ }
80
+
81
+ const coupleCount = couple.filter(Boolean).length;
82
+
83
+ if (!coupleCount)
84
+ return true;
85
+
86
+ if (coupleCount === nodes.length)
87
+ return false;
88
+
89
+ return !(count !== 1 && count === nodes.length);
90
+ };
91
+
92
+ function getAllNodes(path, nodes = []) {
93
+ const consequent = path.get('consequent');
94
+ nodes.push(consequent);
95
+
96
+ if (!path.node.alternate)
97
+ return nodes;
98
+
99
+ const alternate = path.get('alternate');
100
+
101
+ if (!alternate.isIfStatement())
102
+ return [
103
+ ...nodes,
104
+ alternate,
105
+ ];
106
+
107
+ return getAllNodes(alternate, nodes);
108
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `Use 'condition' instead of 'arrow function'`;
4
+
5
+ module.exports.replace = () => ({
6
+ 'if (__a => __b) __c': 'if (__a >= __b) __c',
7
+ });
package/lib/index.js CHANGED
@@ -13,6 +13,7 @@ const removeUselessElse = require('./remove-useless-else');
13
13
  const simplify = require('./simplify');
14
14
  const removeSameValuesCondition = require('./remove-same-values-condition');
15
15
  const addReturn = require('./add-return');
16
+ const convertArrowToCondition = require('./convert-arrow-to-condition');
16
17
 
17
18
  module.exports.rules = {
18
19
  'apply-comparison-order': applyComparisonOrder,
@@ -28,4 +29,5 @@ module.exports.rules = {
28
29
  simplify,
29
30
  'remove-same-values-condition': removeSameValuesCondition,
30
31
  'add-return': addReturn,
32
+ 'convert-arrow-to-condition': convertArrowToCondition,
31
33
  };
@@ -20,7 +20,7 @@ module.exports.replace = () => ({
20
20
  });
21
21
 
22
22
  const check = (template) => ({__a, __b}, prev) => {
23
- while (prev = prev.getPrevSibling(), prev.node) {
23
+ while ((prev = prev.getPrevSibling(), prev.node)) {
24
24
  if (!compare(prev, template))
25
25
  continue;
26
26
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-conditions",
3
- "version": "4.3.0",
3
+ "version": "5.0.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",
@@ -33,18 +33,18 @@
33
33
  ],
34
34
  "devDependencies": {
35
35
  "@putout/plugin-for-of": "*",
36
- "@putout/test": "^9.0.0",
37
- "c8": "^9.0.0",
36
+ "@putout/test": "^11.0.0",
37
+ "c8": "^10.0.0",
38
38
  "eslint": "^9.0.0",
39
39
  "eslint-plugin-n": "^17.0.0",
40
- "eslint-plugin-putout": "^22.0.0",
40
+ "eslint-plugin-putout": "^23.0.0",
41
41
  "lerna": "^6.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": ">=33"
47
+ "putout": ">=36"
48
48
  },
49
49
  "license": "MIT",
50
50
  "engines": {