@putout/plugin-conditions 8.7.0 → 9.0.1
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 +33 -5
- package/lib/apply-early-return/index.js +50 -0
- package/lib/convert-comparison-to-boolean/index.js +1 -1
- package/lib/index.js +2 -0
- package/package.json +10 -10
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,33 @@ 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
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/dd2ba101a3039784bd83a12157fd7fc4/5cdf5340f761c6df3d82964b27aae7d7befcdc56).
|
|
151
|
+
|
|
152
|
+
### ❌ Example of incorrect code
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
function notBlockNoNext() {
|
|
156
|
+
if (a)
|
|
157
|
+
x();
|
|
158
|
+
else
|
|
159
|
+
b();
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### ✅ Example of correct code
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
function notBlockNoNext() {
|
|
167
|
+
if (a) {
|
|
168
|
+
x();
|
|
169
|
+
return;
|
|
170
|
+
} else
|
|
171
|
+
b();
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
146
175
|
## apply-equal
|
|
147
176
|
|
|
148
177
|
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/7ec940ad79df7e12a1d9111136b4a168/2ccea607e3232721eb950c18195168f8d9b4ef6a).
|
|
@@ -520,17 +549,16 @@ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/256b1b6a90
|
|
|
520
549
|
|
|
521
550
|
### ❌ Example of incorrect code
|
|
522
551
|
|
|
523
|
-
```
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
if (a) {}
|
|
552
|
+
```
|
|
553
|
+
if (a)
|
|
554
|
+
const b = 5;
|
|
527
555
|
```
|
|
528
556
|
|
|
529
557
|
### ✅ Example of correct code
|
|
530
558
|
|
|
531
559
|
```js
|
|
532
560
|
if (a) {
|
|
533
|
-
const
|
|
561
|
+
const b = 5;
|
|
534
562
|
}
|
|
535
563
|
```
|
|
536
564
|
|
|
@@ -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.1",
|
|
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"
|