@putout/plugin-conditions 8.6.0 → 9.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
|
@@ -15,6 +15,7 @@ npm i @putout/plugin-conditions -D
|
|
|
15
15
|
|
|
16
16
|
- ✅ [add-return](#add-return);
|
|
17
17
|
- ✅ [apply-comparison-order](#apply-comparison-order);
|
|
18
|
+
- ✅ [apply-early-return](#apply-early-return);
|
|
18
19
|
- ✅ [apply-consistent-blocks](#apply-consistent-blocks);
|
|
19
20
|
- ✅ [apply-equal](#apply-equal);
|
|
20
21
|
- ✅ [apply-if](#apply-if);
|
|
@@ -40,6 +41,7 @@ npm i @putout/plugin-conditions -D
|
|
|
40
41
|
{
|
|
41
42
|
"rules": {
|
|
42
43
|
"conditions/apply-consistent-blocks": "on",
|
|
44
|
+
"conditions/apply-return-return": "on",
|
|
43
45
|
"conditions/apply-comparison-order": "on",
|
|
44
46
|
"conditions/apply-equal": "on",
|
|
45
47
|
"conditions/apply-if": "on",
|
|
@@ -143,6 +145,31 @@ Linter | Rule | Fix
|
|
|
143
145
|
🐊 **Putout**| [`conditions/apply-comparison-order`](https://github.com/coderaiser/putout/tree/master/packages/plugin-conditions/#apply-comparison-order)| ✅
|
|
144
146
|
⏣ **ESLint** | [`yoda`](https://eslint.org/docs/rules/yoda) | ½
|
|
145
147
|
|
|
148
|
+
## apply-early-return
|
|
149
|
+
|
|
150
|
+
### ❌ Example of incorrect code
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
function notBlockNoNext() {
|
|
154
|
+
if (a)
|
|
155
|
+
x();
|
|
156
|
+
else
|
|
157
|
+
b();
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### ✅ Example of correct code
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
function notBlockNoNext() {
|
|
165
|
+
if (a) {
|
|
166
|
+
x();
|
|
167
|
+
return;
|
|
168
|
+
} else
|
|
169
|
+
b();
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
146
173
|
## apply-equal
|
|
147
174
|
|
|
148
175
|
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/7ec940ad79df7e12a1d9111136b4a168/2ccea607e3232721eb950c18195168f8d9b4ef6a).
|
|
@@ -520,17 +547,16 @@ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/256b1b6a90
|
|
|
520
547
|
|
|
521
548
|
### ❌ Example of incorrect code
|
|
522
549
|
|
|
523
|
-
```
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
if (a) {}
|
|
550
|
+
```
|
|
551
|
+
if (a)
|
|
552
|
+
const b = 5;
|
|
527
553
|
```
|
|
528
554
|
|
|
529
555
|
### ✅ Example of correct code
|
|
530
556
|
|
|
531
557
|
```js
|
|
532
558
|
if (a) {
|
|
533
|
-
const
|
|
559
|
+
const b = 5;
|
|
534
560
|
}
|
|
535
561
|
```
|
|
536
562
|
|
|
@@ -97,7 +97,14 @@ export const filter = (path) => {
|
|
|
97
97
|
const couple = [];
|
|
98
98
|
|
|
99
99
|
for (const path of paths) {
|
|
100
|
-
|
|
100
|
+
if (!isBlockStatement(path)) {
|
|
101
|
+
couple.push(false);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const {length} = path.node.body;
|
|
106
|
+
const is = length > 1 || !length;
|
|
107
|
+
|
|
101
108
|
couple.push(is);
|
|
102
109
|
}
|
|
103
110
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isReturnStatement,
|
|
5
|
+
isBlockStatement,
|
|
6
|
+
isFunction,
|
|
7
|
+
returnStatement,
|
|
8
|
+
} = types;
|
|
9
|
+
|
|
10
|
+
export const report = () => `Use 'early return' instead of 'else'`;
|
|
11
|
+
|
|
12
|
+
export const match = () => ({
|
|
13
|
+
'if (__a) __b; else __c': check,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const replace = () => ({
|
|
17
|
+
'if (__a) __b; else __c': ({__b}, path) => {
|
|
18
|
+
if (isBlockStatement(__b)) {
|
|
19
|
+
__b.body.push(returnStatement());
|
|
20
|
+
return path;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return `
|
|
24
|
+
if (__a) {
|
|
25
|
+
__b;
|
|
26
|
+
return;
|
|
27
|
+
} else
|
|
28
|
+
__c
|
|
29
|
+
`;
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
function check(vars, path) {
|
|
34
|
+
if (!isFunction(path.parentPath.parentPath))
|
|
35
|
+
return false;
|
|
36
|
+
|
|
37
|
+
if (path.getNextSibling().node)
|
|
38
|
+
return false;
|
|
39
|
+
|
|
40
|
+
const consequent = path.get('consequent');
|
|
41
|
+
|
|
42
|
+
if (isBlockStatement(consequent)) {
|
|
43
|
+
const last = consequent.node.body.at(-1);
|
|
44
|
+
|
|
45
|
+
if (isReturnStatement(last))
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return true;
|
|
50
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as applyEarlyReturn from './apply-early-return/index.js';
|
|
1
2
|
import * as applyEqual from './apply-equal/index.js';
|
|
2
3
|
import * as applyConsistentBlocks from './apply-consistent-blocks/index.js';
|
|
3
4
|
import * as applyComparisonOrder from './apply-comparison-order/index.js';
|
|
@@ -38,4 +39,5 @@ export const rules = {
|
|
|
38
39
|
'remove-useless-loop-condition': removeUselessLoopCondition,
|
|
39
40
|
'merge-if-with-else': mergeIfWithElse,
|
|
40
41
|
'apply-equal': applyEqual,
|
|
42
|
+
'apply-early-return': applyEarlyReturn,
|
|
41
43
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-conditions",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "🐊Putout plugin adds support of conditions transformations",
|
|
@@ -32,23 +32,23 @@
|
|
|
32
32
|
"conditions"
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@putout/eslint-flat": "^
|
|
35
|
+
"@putout/eslint-flat": "^4.0.0",
|
|
36
36
|
"@putout/plugin-for-of": "*",
|
|
37
|
-
"@putout/test": "^
|
|
38
|
-
"
|
|
39
|
-
"eslint": "^9.0.0",
|
|
37
|
+
"@putout/test": "^15.0.0",
|
|
38
|
+
"eslint": "^10.0.0",
|
|
40
39
|
"eslint-plugin-n": "^17.0.0",
|
|
41
|
-
"eslint-plugin-putout": "^
|
|
42
|
-
"madrun": "^
|
|
40
|
+
"eslint-plugin-putout": "^31.0.0",
|
|
41
|
+
"madrun": "^13.0.0",
|
|
43
42
|
"montag": "^1.2.1",
|
|
44
|
-
"nodemon": "^3.0.1"
|
|
43
|
+
"nodemon": "^3.0.1",
|
|
44
|
+
"superc8": "^12.0.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"putout": ">=
|
|
47
|
+
"putout": ">=42"
|
|
48
48
|
},
|
|
49
49
|
"license": "MIT",
|
|
50
50
|
"engines": {
|
|
51
|
-
"node": ">=
|
|
51
|
+
"node": ">=22"
|
|
52
52
|
},
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|