@putout/plugin-conditions 3.0.2 → 3.2.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.
|
@@ -2,38 +2,54 @@
|
|
|
2
2
|
|
|
3
3
|
const {operator} = require('putout');
|
|
4
4
|
const {
|
|
5
|
-
replaceWith,
|
|
6
5
|
getTemplateValues,
|
|
7
6
|
compare,
|
|
7
|
+
traverse,
|
|
8
8
|
} = operator;
|
|
9
9
|
|
|
10
10
|
module.exports.report = () => `Avoid condition with the same value`;
|
|
11
11
|
|
|
12
12
|
module.exports.match = () => ({
|
|
13
|
-
'if (__a === __b) __c'(
|
|
14
|
-
|
|
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
|
-
},
|
|
13
|
+
'if (__a === __b) __c': check('if (__a !== __b) __c'),
|
|
14
|
+
'if (__a !== __b) __c': check('if (__a === __b) __c'),
|
|
31
15
|
});
|
|
32
16
|
|
|
33
17
|
module.exports.replace = () => ({
|
|
34
|
-
'if (__a === __b) __c':
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return path;
|
|
38
|
-
},
|
|
18
|
+
'if (__a === __b) __c': '__c',
|
|
19
|
+
'if (__a !== __b) __c': '__c',
|
|
39
20
|
});
|
|
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
|
+
|
|
44
|
+
function hasContinue(path) {
|
|
45
|
+
let is = false;
|
|
46
|
+
|
|
47
|
+
traverse(path, {
|
|
48
|
+
ContinueStatement(path) {
|
|
49
|
+
is = true;
|
|
50
|
+
path.stop();
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return is;
|
|
55
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-conditions",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.2.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",
|