@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
- isIfStatement,
7
+ BlockStatement,
7
8
  } = types;
8
9
 
9
10
  module.exports.report = () => `Use consistent blocks`;
10
11
 
11
- const notBlock = ({__b}) => !isBlockStatement(__b);
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
- module.exports.match = () => ({
14
- 'if (__a) {__b} else {__c}': () => true,
15
- 'if (__a) {__b} else __c': () => true,
16
- 'if (__a) __b; else __body': notBlock,
17
- 'if (__a) __body; else __b': ({__b}) => !isIfStatement(__b),
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 (leadingComments || trailingComments)
27
- return false;
28
-
29
- if (!parentPath.isIfStatement())
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
- return path !== parentPath.get('alternate');
33
- },
34
- });
48
+ }
49
+
50
+ return false;
51
+ }
35
52
 
36
- module.exports.replace = () => ({
37
- 'if (__a) {if (__b) {__c}}': 'if (__a) if (__b) __c',
38
- 'if (__a) {__b}': 'if (__a) __b',
39
- 'if (__a) {__b} else {__c}': 'if (__a) __b; else __c',
40
- 'if (__a) __b; else __body': 'if (__a) {__b} else __body',
41
- 'if (__a) __body; else __b': ({__b}, path) => {
42
- if (isBlockStatement(__b))
43
- return path;
44
-
45
- return 'if (__a) __body; else {__b}';
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.0",
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",