@putout/plugin-conditions 4.3.0 → 4.4.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.
|
@@ -1,47 +1,108 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {types} = require('putout');
|
|
3
|
+
const {types, operator} = require('putout');
|
|
4
|
+
const {replaceWith} = operator;
|
|
4
5
|
const {
|
|
5
6
|
isBlockStatement,
|
|
6
|
-
|
|
7
|
+
BlockStatement,
|
|
7
8
|
} = types;
|
|
8
9
|
|
|
9
10
|
module.exports.report = () => `Use consistent blocks`;
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
module.exports.fix = (path) => {
|
|
13
|
+
const paths = getAllNodes(path);
|
|
14
|
+
|
|
15
|
+
if (isAllBlocks(paths))
|
|
16
|
+
for (const path of paths) {
|
|
17
|
+
if (isBlockStatement(path))
|
|
18
|
+
continue;
|
|
19
|
+
|
|
20
|
+
const {node} = path;
|
|
21
|
+
replaceWith(path, BlockStatement([node]));
|
|
22
|
+
}
|
|
23
|
+
else
|
|
24
|
+
for (const path of paths) {
|
|
25
|
+
if (!isBlockStatement(path))
|
|
26
|
+
continue;
|
|
27
|
+
|
|
28
|
+
const [node] = path.node.body;
|
|
29
|
+
replaceWith(path, node);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
12
32
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
'if (__a) {__b}': (vars, path) => {
|
|
19
|
-
const {parentPath} = path;
|
|
20
|
-
const __bPath = path.get('consequent.body.0');
|
|
21
|
-
const {
|
|
22
|
-
leadingComments,
|
|
23
|
-
trailingComments,
|
|
24
|
-
} = __bPath.node;
|
|
33
|
+
function isAllBlocks(paths) {
|
|
34
|
+
const counts = [];
|
|
35
|
+
|
|
36
|
+
for (const path of paths) {
|
|
37
|
+
const is = isBlockStatement(path);
|
|
25
38
|
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
39
|
+
if (is)
|
|
40
|
+
counts.push(path.node.body.length);
|
|
41
|
+
else
|
|
42
|
+
counts.push(Infinity);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const count of counts) {
|
|
46
|
+
if (count !== 1 && count !== Infinity)
|
|
30
47
|
return true;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
35
52
|
|
|
36
|
-
module.exports.
|
|
37
|
-
'
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
module.exports.include = () => [
|
|
54
|
+
'IfStatement',
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
module.exports.filter = (path) => {
|
|
58
|
+
if (path === path.parentPath.get('alternate'))
|
|
59
|
+
return;
|
|
60
|
+
|
|
61
|
+
const nodes = getAllNodes(path);
|
|
62
|
+
const blocks = [];
|
|
63
|
+
|
|
64
|
+
for (const node of nodes) {
|
|
65
|
+
const is = isBlockStatement(node);
|
|
66
|
+
blocks.push(is);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const count = blocks.filter(Boolean).length;
|
|
70
|
+
|
|
71
|
+
if (!count)
|
|
72
|
+
return false;
|
|
73
|
+
|
|
74
|
+
const couple = [];
|
|
75
|
+
|
|
76
|
+
for (const node of nodes) {
|
|
77
|
+
const is = isBlockStatement(node) && node.node.body.length > 1;
|
|
78
|
+
couple.push(is);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const coupleCount = couple.filter(Boolean).length;
|
|
82
|
+
|
|
83
|
+
if (!coupleCount)
|
|
84
|
+
return true;
|
|
85
|
+
|
|
86
|
+
if (coupleCount === nodes.length)
|
|
87
|
+
return false;
|
|
88
|
+
|
|
89
|
+
return !(count !== 1 && count === nodes.length);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function getAllNodes(path, nodes = []) {
|
|
93
|
+
const consequent = path.get('consequent');
|
|
94
|
+
nodes.push(consequent);
|
|
95
|
+
|
|
96
|
+
if (!path.node.alternate)
|
|
97
|
+
return nodes;
|
|
98
|
+
|
|
99
|
+
const alternate = path.get('alternate');
|
|
100
|
+
|
|
101
|
+
if (!alternate.isIfStatement())
|
|
102
|
+
return [
|
|
103
|
+
...nodes,
|
|
104
|
+
alternate,
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
return getAllNodes(alternate, nodes);
|
|
108
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-conditions",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.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",
|