@putout/plugin-conditions 3.1.0 β 4.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
|
@@ -16,6 +16,7 @@ npm i @putout/plugin-conditions -D
|
|
|
16
16
|
```json
|
|
17
17
|
{
|
|
18
18
|
"rules": {
|
|
19
|
+
"conditions/apply-consistent-blocks": "on",
|
|
19
20
|
"conditions/apply-comparison-order": "on",
|
|
20
21
|
"conditions/apply-if": "on",
|
|
21
22
|
"conditions/convert-comparison-to-boolean": "on",
|
|
@@ -31,6 +32,54 @@ npm i @putout/plugin-conditions -D
|
|
|
31
32
|
}
|
|
32
33
|
```
|
|
33
34
|
|
|
35
|
+
## apply-consistent-blocks
|
|
36
|
+
|
|
37
|
+
> A **block statement** is used to group zero or more statements. The block is delimited by a pair of braces ("curly braces") and contains a list of zero or more statements and declarations.
|
|
38
|
+
>
|
|
39
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block)
|
|
40
|
+
|
|
41
|
+
Check out in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9035db21bae7b6c76d1dc875d4c74828/07edbfd755060ae263949caf08b31aa43609375a).
|
|
42
|
+
|
|
43
|
+
### β Example of incorrect code
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
if (a > 3) {
|
|
47
|
+
m();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (a > 3)
|
|
51
|
+
b = 5;
|
|
52
|
+
else {
|
|
53
|
+
b = 6;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (a > 3)
|
|
57
|
+
b = 5;
|
|
58
|
+
else {
|
|
59
|
+
b = 6;
|
|
60
|
+
fn();
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### β
Example of correct code
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
if (a > 3)
|
|
68
|
+
m();
|
|
69
|
+
|
|
70
|
+
if (a > 3)
|
|
71
|
+
b = 5;
|
|
72
|
+
else
|
|
73
|
+
b = 6;
|
|
74
|
+
|
|
75
|
+
if (a > 3) {
|
|
76
|
+
b = 5;
|
|
77
|
+
} else {
|
|
78
|
+
b = 6;
|
|
79
|
+
fn();
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
34
83
|
## apply-comparison-order
|
|
35
84
|
|
|
36
85
|
> The result of evaluating an equality operator is always of type boolean based on whether the comparison is true.
|
|
@@ -126,9 +175,8 @@ if (a === b) {}
|
|
|
126
175
|
const a = [];
|
|
127
176
|
const c = a;
|
|
128
177
|
|
|
129
|
-
if (a)
|
|
178
|
+
if (a)
|
|
130
179
|
console.log(a);
|
|
131
|
-
}
|
|
132
180
|
```
|
|
133
181
|
|
|
134
182
|
### β
Example of correct code
|
|
@@ -201,9 +249,8 @@ if (b) {}
|
|
|
201
249
|
### β Example of incorrect code
|
|
202
250
|
|
|
203
251
|
```js
|
|
204
|
-
if (zone?.tooltipCallback)
|
|
252
|
+
if (zone?.tooltipCallback)
|
|
205
253
|
zone.tooltipCallback(e);
|
|
206
|
-
}
|
|
207
254
|
|
|
208
255
|
if (a)
|
|
209
256
|
alert('hello');
|
|
@@ -228,19 +275,16 @@ alert('hello');
|
|
|
228
275
|
### β Example of incorrect code
|
|
229
276
|
|
|
230
277
|
```js
|
|
231
|
-
if (a > b)
|
|
232
|
-
if (b < c)
|
|
278
|
+
if (a > b)
|
|
279
|
+
if (b < c)
|
|
233
280
|
console.log('hello');
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
281
|
```
|
|
237
282
|
|
|
238
283
|
### β
Example of correct code
|
|
239
284
|
|
|
240
285
|
```js
|
|
241
|
-
if (a > b && b < c)
|
|
286
|
+
if (a > b && b < c)
|
|
242
287
|
console.log('hello');
|
|
243
|
-
}
|
|
244
288
|
```
|
|
245
289
|
|
|
246
290
|
## remove-useless-else
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('putout');
|
|
4
|
+
const {
|
|
5
|
+
isBlockStatement,
|
|
6
|
+
isIfStatement,
|
|
7
|
+
} = types;
|
|
8
|
+
|
|
9
|
+
module.exports.report = () => `Use consistent blocks`;
|
|
10
|
+
|
|
11
|
+
const notBlock = ({__b}) => !isBlockStatement(__b);
|
|
12
|
+
|
|
13
|
+
module.exports.match = () => ({
|
|
14
|
+
'if (__a) {__b} else {__c}': () => true,
|
|
15
|
+
'if (__a) __b; else __body': notBlock,
|
|
16
|
+
'if (__a) __body; else __b': ({__b}) => !isIfStatement(__b),
|
|
17
|
+
'if (__a) {__b}': (vars, path) => {
|
|
18
|
+
const {parentPath} = path;
|
|
19
|
+
|
|
20
|
+
if (!parentPath.isIfStatement())
|
|
21
|
+
return true;
|
|
22
|
+
|
|
23
|
+
return path !== parentPath.get('alternate');
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
module.exports.replace = () => ({
|
|
28
|
+
'if (__a) {if (__b) {__c}}': 'if (__a) if (__b) __c',
|
|
29
|
+
'if (__a) {__b}': 'if (__a) __b',
|
|
30
|
+
'if (__a) {__b} else {__c}': () => 'if (__a) __b; else __c',
|
|
31
|
+
'if (__a) __b; else __body': 'if (__a) {__b} else __body',
|
|
32
|
+
'if (__a) __body; else __b': ({__b}, path) => {
|
|
33
|
+
if (isBlockStatement(__b))
|
|
34
|
+
return path;
|
|
35
|
+
|
|
36
|
+
return 'if (__a) __body; else {__b}';
|
|
37
|
+
},
|
|
38
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const applyConsistentBlocks = require('./apply-consistent-blocks');
|
|
3
4
|
const applyComparisonOrder = require('./apply-comparison-order');
|
|
4
5
|
const applyIf = require('./apply-if');
|
|
5
6
|
const evaluate = require('./evaluate');
|
|
@@ -14,6 +15,7 @@ const removeSameValuesCondition = require('./remove-same-values-condition');
|
|
|
14
15
|
|
|
15
16
|
module.exports.rules = {
|
|
16
17
|
'apply-comparison-order': applyComparisonOrder,
|
|
18
|
+
'apply-consistent-blocks': applyConsistentBlocks,
|
|
17
19
|
'apply-if': applyIf,
|
|
18
20
|
evaluate,
|
|
19
21
|
'convert-comparison-to-boolean': convertComparisonToBoolean,
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
const {operator} = require('putout');
|
|
4
4
|
const {
|
|
5
|
-
replaceWith,
|
|
6
5
|
getTemplateValues,
|
|
7
6
|
compare,
|
|
8
7
|
traverse,
|
|
@@ -11,37 +10,37 @@ const {
|
|
|
11
10
|
module.exports.report = () => `Avoid condition with the same value`;
|
|
12
11
|
|
|
13
12
|
module.exports.match = () => ({
|
|
14
|
-
'if (__a === __b) __c'(
|
|
15
|
-
|
|
16
|
-
if (!compare(prev, 'if (__a !== __b) __c'))
|
|
17
|
-
continue;
|
|
18
|
-
|
|
19
|
-
const values = getTemplateValues(prev, 'if (__a !== __b) __c');
|
|
20
|
-
|
|
21
|
-
if (!compare(__a, values.__a))
|
|
22
|
-
continue;
|
|
23
|
-
|
|
24
|
-
if (!compare(__b, values.__b))
|
|
25
|
-
continue;
|
|
26
|
-
|
|
27
|
-
if (!hasContinue(prev))
|
|
28
|
-
continue;
|
|
29
|
-
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return false;
|
|
34
|
-
},
|
|
13
|
+
'if (__a === __b) __c': check('if (__a !== __b) __c'),
|
|
14
|
+
'if (__a !== __b) __c': check('if (__a === __b) __c'),
|
|
35
15
|
});
|
|
36
16
|
|
|
37
17
|
module.exports.replace = () => ({
|
|
38
|
-
'if (__a === __b) __c':
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return path;
|
|
42
|
-
},
|
|
18
|
+
'if (__a === __b) __c': '__c',
|
|
19
|
+
'if (__a !== __b) __c': '__c',
|
|
43
20
|
});
|
|
44
21
|
|
|
22
|
+
const check = (template) => ({__a, __b}, prev) => {
|
|
23
|
+
while (prev = prev.getPrevSibling(), prev.node) {
|
|
24
|
+
if (!compare(prev, template))
|
|
25
|
+
continue;
|
|
26
|
+
|
|
27
|
+
const values = getTemplateValues(prev, template);
|
|
28
|
+
|
|
29
|
+
if (!compare(__a, values.__a))
|
|
30
|
+
continue;
|
|
31
|
+
|
|
32
|
+
if (!compare(__b, values.__b))
|
|
33
|
+
continue;
|
|
34
|
+
|
|
35
|
+
if (!hasContinue(prev))
|
|
36
|
+
continue;
|
|
37
|
+
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return false;
|
|
42
|
+
};
|
|
43
|
+
|
|
45
44
|
function hasContinue(path) {
|
|
46
45
|
let is = false;
|
|
47
46
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-conditions",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.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",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"nodemon": "^3.0.1"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"putout": ">=
|
|
47
|
+
"putout": ">=33"
|
|
48
48
|
},
|
|
49
49
|
"license": "MIT",
|
|
50
50
|
"engines": {
|