@putout/plugin-conditions 3.0.0 β 3.0.2
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
|
@@ -25,6 +25,7 @@ npm i @putout/plugin-conditions -D
|
|
|
25
25
|
"conditions/remove-constant": "on",
|
|
26
26
|
"conditions/remove-zero": "on",
|
|
27
27
|
"conditions/remove-useless-else": "on",
|
|
28
|
+
"conditions/remove-same-values-condition": "on",
|
|
28
29
|
"conditions/merge-if-statements": "on"
|
|
29
30
|
}
|
|
30
31
|
}
|
|
@@ -272,6 +273,45 @@ if (x)
|
|
|
272
273
|
console.log();
|
|
273
274
|
```
|
|
274
275
|
|
|
276
|
+
## remove-same-values-condition"
|
|
277
|
+
|
|
278
|
+
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e537d4ec636d4a9b849063a8326b70ae/661041b3fbb1e3678bf7f828e4c8bf6ca723f89d).
|
|
279
|
+
|
|
280
|
+
### β Example of incorrect code
|
|
281
|
+
|
|
282
|
+
```js
|
|
283
|
+
for (const [i, el] of entries(elements)) {
|
|
284
|
+
if (el !== path)
|
|
285
|
+
continue;
|
|
286
|
+
|
|
287
|
+
if (!Number(i) && n) {
|
|
288
|
+
path.parentPath.node.elements[i] = null;
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (el === path) {
|
|
293
|
+
remove(path);
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### β
Example of correct code
|
|
300
|
+
|
|
301
|
+
```js
|
|
302
|
+
for (const [i, el] of entries(elements)) {
|
|
303
|
+
if (el !== path)
|
|
304
|
+
continue;
|
|
305
|
+
|
|
306
|
+
if (!Number(i) && n) {
|
|
307
|
+
path.parentPath.node.elements[i] = null;
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
remove(path);
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
275
315
|
### Comparison
|
|
276
316
|
|
|
277
317
|
Linter | Rule | Fix
|
package/lib/index.js
CHANGED
|
@@ -10,6 +10,7 @@ const removeBoolean = require('./remove-boolean');
|
|
|
10
10
|
const removeZero = require('./remove-zero');
|
|
11
11
|
const removeUselessElse = require('./remove-useless-else');
|
|
12
12
|
const simplify = require('./simplify');
|
|
13
|
+
const removeSameValuesCondition = require('./remove-same-values-condition');
|
|
13
14
|
|
|
14
15
|
module.exports.rules = {
|
|
15
16
|
'apply-comparison-order': applyComparisonOrder,
|
|
@@ -22,4 +23,5 @@ module.exports.rules = {
|
|
|
22
23
|
'remove-zero': removeZero,
|
|
23
24
|
'remove-useless-else': removeUselessElse,
|
|
24
25
|
simplify,
|
|
26
|
+
'remove-same-values-condition': removeSameValuesCondition,
|
|
25
27
|
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {operator} = require('putout');
|
|
4
|
+
const {
|
|
5
|
+
replaceWith,
|
|
6
|
+
getTemplateValues,
|
|
7
|
+
compare,
|
|
8
|
+
} = operator;
|
|
9
|
+
|
|
10
|
+
module.exports.report = () => `Avoid condition with the same value`;
|
|
11
|
+
|
|
12
|
+
module.exports.match = () => ({
|
|
13
|
+
'if (__a === __b) __c'({__a, __b}, prev) {
|
|
14
|
+
while (prev = prev.getPrevSibling(), prev.node) {
|
|
15
|
+
if (!compare(prev, 'if (__a !== __b) __c'))
|
|
16
|
+
continue;
|
|
17
|
+
|
|
18
|
+
const values = getTemplateValues(prev, 'if (__a !== __b) __c');
|
|
19
|
+
|
|
20
|
+
if (!compare(__a, values.__a))
|
|
21
|
+
continue;
|
|
22
|
+
|
|
23
|
+
if (!compare(__b, values.__b))
|
|
24
|
+
continue;
|
|
25
|
+
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return false;
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
module.exports.replace = () => ({
|
|
34
|
+
'if (__a === __b) __c': (vars, path) => {
|
|
35
|
+
replaceWith(path, path.get('consequent'));
|
|
36
|
+
|
|
37
|
+
return path;
|
|
38
|
+
},
|
|
39
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-conditions",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
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",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"c8": "^8.0.0",
|
|
38
38
|
"eslint": "^8.0.1",
|
|
39
39
|
"eslint-plugin-n": "^16.0.0",
|
|
40
|
-
"eslint-plugin-putout": "^
|
|
40
|
+
"eslint-plugin-putout": "^21.0.0",
|
|
41
41
|
"lerna": "^6.0.1",
|
|
42
42
|
"madrun": "^9.0.0",
|
|
43
43
|
"montag": "^1.2.1",
|