@putout/plugin-conditions 1.0.2 → 1.1.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 +23 -7
- package/lib/apply-if/index.js +6 -2
- package/lib/convert-comparison-to-boolean/index.js +5 -1
- package/lib/convert-equal-to-strict-equal/index.js +0 -1
- package/lib/evaluate/index.js +0 -1
- package/lib/index.js +1 -1
- package/lib/remove-boolean/index.js +2 -7
- package/lib/remove-constant/index.js +3 -4
- package/lib/remove-zero/index.js +27 -0
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -22,7 +22,8 @@ npm i @putout/plugin-conditions -D
|
|
|
22
22
|
"conditions/convert-equal-to-strict-equal": "on",
|
|
23
23
|
"conditions/evaluate": "on",
|
|
24
24
|
"conditions/remove-boolean": "on",
|
|
25
|
-
"conditions/remove-constant": "on"
|
|
25
|
+
"conditions/remove-constant": "on",
|
|
26
|
+
"conditions/remove-zero": "on"
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
```
|
|
@@ -101,15 +102,13 @@ const t = false;
|
|
|
101
102
|
### ❌ Example of incorrect code
|
|
102
103
|
|
|
103
104
|
```js
|
|
104
|
-
if (a == b) {
|
|
105
|
-
}
|
|
105
|
+
if (a == b) {}
|
|
106
106
|
```
|
|
107
107
|
|
|
108
108
|
### ✅ Example of correct code
|
|
109
109
|
|
|
110
110
|
```js
|
|
111
|
-
if (a === b) {
|
|
112
|
-
}
|
|
111
|
+
if (a === b) {}
|
|
113
112
|
```
|
|
114
113
|
|
|
115
114
|
## evaluate
|
|
@@ -176,6 +175,24 @@ function hi(b) {
|
|
|
176
175
|
}
|
|
177
176
|
```
|
|
178
177
|
|
|
178
|
+
## remove-zero-
|
|
179
|
+
|
|
180
|
+
### ❌ Example of incorrect code
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
if (b === 0) {}
|
|
184
|
+
|
|
185
|
+
if (b !== 0) {}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### ✅ Example of correct code
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
if (!b) {}
|
|
192
|
+
|
|
193
|
+
if (b) {}
|
|
194
|
+
```
|
|
195
|
+
|
|
179
196
|
## simplify
|
|
180
197
|
|
|
181
198
|
## ❌ Example of incorrect code
|
|
@@ -186,8 +203,7 @@ if (zone?.tooltipCallback) {
|
|
|
186
203
|
}
|
|
187
204
|
|
|
188
205
|
if (a)
|
|
189
|
-
alert('hello');
|
|
190
|
-
else
|
|
206
|
+
alert('hello');else
|
|
191
207
|
alert('hello');
|
|
192
208
|
```
|
|
193
209
|
|
package/lib/apply-if/index.js
CHANGED
|
@@ -10,10 +10,14 @@ module.exports.filter = (path) => {
|
|
|
10
10
|
if (!nextPath.node)
|
|
11
11
|
return false;
|
|
12
12
|
|
|
13
|
-
return path
|
|
13
|
+
return path
|
|
14
|
+
.get('consequent')
|
|
15
|
+
.isEmptyStatement();
|
|
14
16
|
};
|
|
15
17
|
|
|
16
|
-
module.exports.include = () => [
|
|
18
|
+
module.exports.include = () => [
|
|
19
|
+
'IfStatement',
|
|
20
|
+
];
|
|
17
21
|
|
|
18
22
|
module.exports.fix = (path) => {
|
|
19
23
|
const nextPath = path.getNextSibling();
|
package/lib/evaluate/index.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -5,19 +5,14 @@ module.exports.report = () => 'Avoid boolean in assertions';
|
|
|
5
5
|
module.exports.replace = () => ({
|
|
6
6
|
'return __a === true': 'return Boolean(__a)',
|
|
7
7
|
'return __a == true': 'return Boolean(__a)',
|
|
8
|
-
|
|
9
|
-
'const __a = __b
|
|
10
|
-
'const __a = __b == true': 'const __a = __b == Boolean(__a)',
|
|
11
|
-
|
|
8
|
+
'const __a = __b === true': 'const __a = __b',
|
|
9
|
+
'const __a = __b == true': 'const __a = __b',
|
|
12
10
|
'__a = __b === true': '__a = __b === Boolean(__a)',
|
|
13
11
|
'__a = __b == true': '__a = __b == Boolean(__a)',
|
|
14
|
-
|
|
15
12
|
'__a === true': '__a',
|
|
16
13
|
'__a === false': '!__a',
|
|
17
14
|
'__a == true': '__a',
|
|
18
15
|
'__a == false': '!__a',
|
|
19
|
-
|
|
20
16
|
'__a !== true': '!__a',
|
|
21
17
|
'__a != true': '!__a',
|
|
22
18
|
});
|
|
23
|
-
|
|
@@ -12,6 +12,7 @@ const {
|
|
|
12
12
|
replaceWithMultiple,
|
|
13
13
|
remove,
|
|
14
14
|
} = operator;
|
|
15
|
+
|
|
15
16
|
const {isIdentifier} = types;
|
|
16
17
|
|
|
17
18
|
module.exports.report = () => 'Avoid constant conditions';
|
|
@@ -22,9 +23,7 @@ module.exports.fix = ({path, result}) => {
|
|
|
22
23
|
consequent,
|
|
23
24
|
} = path.node;
|
|
24
25
|
|
|
25
|
-
const {
|
|
26
|
-
body = [consequent],
|
|
27
|
-
} = consequent;
|
|
26
|
+
const {body = [consequent]} = consequent;
|
|
28
27
|
|
|
29
28
|
if (result)
|
|
30
29
|
return replaceWithMultiple(path, body);
|
|
@@ -38,6 +37,7 @@ module.exports.fix = ({path, result}) => {
|
|
|
38
37
|
module.exports.traverse = ({push, generate}) => ({
|
|
39
38
|
IfStatement(path) {
|
|
40
39
|
const testPath = path.get('test');
|
|
40
|
+
|
|
41
41
|
const {
|
|
42
42
|
left,
|
|
43
43
|
right,
|
|
@@ -78,4 +78,3 @@ function containsIdentifiers(testPath) {
|
|
|
78
78
|
|
|
79
79
|
return is;
|
|
80
80
|
}
|
|
81
|
-
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const check = (vars, path) => {
|
|
4
|
+
const {parentPath} = path;
|
|
5
|
+
|
|
6
|
+
if (parentPath.isAssignmentExpression())
|
|
7
|
+
return false;
|
|
8
|
+
|
|
9
|
+
if (parentPath.isVariableDeclarator())
|
|
10
|
+
return false;
|
|
11
|
+
|
|
12
|
+
return true;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports.report = () => 'Avoid zero in assertions';
|
|
16
|
+
|
|
17
|
+
module.exports.match = () => ({
|
|
18
|
+
'__a !== 0': check,
|
|
19
|
+
'__a != 0': check,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
module.exports.replace = () => ({
|
|
23
|
+
'__a !== 0': '__a',
|
|
24
|
+
'__a != 0': '__a',
|
|
25
|
+
'__a === 0': '!__a',
|
|
26
|
+
'__a == 0': '!__a',
|
|
27
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-conditions",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.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",
|
|
@@ -32,13 +32,12 @@
|
|
|
32
32
|
"conditions"
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@putout/plugin-
|
|
36
|
-
"@putout/plugin-remove-useless-variables": "^7.3.0",
|
|
35
|
+
"@putout/plugin-for-of": "*",
|
|
37
36
|
"@putout/test": "^6.0.0",
|
|
38
37
|
"c8": "^7.5.0",
|
|
39
38
|
"eslint": "^8.0.1",
|
|
40
39
|
"eslint-plugin-n": "^15.2.4",
|
|
41
|
-
"eslint-plugin-putout": "^
|
|
40
|
+
"eslint-plugin-putout": "^17.0.0",
|
|
42
41
|
"lerna": "^6.0.1",
|
|
43
42
|
"madrun": "^9.0.0",
|
|
44
43
|
"montag": "^1.2.1",
|