eslint-plugin-putout 16.4.0 → 16.5.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,4 +1,4 @@
1
- # array-elements-newline
1
+ # array-element-newline
2
2
 
3
3
  This rule aims to add newlines between array elements.
4
4
  It exists because [`array-element-newline`](https://eslint.org/docs/rules/array-element-newline) requires [`array-bracket-newline`](https://eslint.org/docs/rules/array-bracket-newline) which conflicts with [`object-braces-inside-array`](https://github.com/coderaiser/putout/tree/master/packages/eslint-plugin-putout/lib/objects-braces-inside-array#readme).
@@ -2,15 +2,18 @@
2
2
 
3
3
  const {types} = require('putout');
4
4
  const {
5
+ isArrayExpression,
5
6
  isMemberExpression,
6
- isVariableDeclarator,
7
+ isCallExpression,
7
8
  } = types;
8
9
 
10
+ const isString = (a) => typeof a === 'string';
11
+ const isBool = (a) => typeof a === 'boolean';
12
+
9
13
  module.exports.category = 'array';
10
14
  module.exports.report = () => 'Add newlines between array elements';
11
15
 
12
- const regexp = /['\da-zA-Z]+, ['\da-zA-Z]/;
13
-
16
+ const regexp = /['"\da-zA-Z]+, ['"\da-zA-Z]/;
14
17
  const isSupportedNode = (a) => {
15
18
  if (!a)
16
19
  return false;
@@ -23,21 +26,34 @@ const isSupportedNode = (a) => {
23
26
 
24
27
  return false;
25
28
  };
26
-
27
29
  module.exports.filter = ({text, node}) => {
28
30
  if (isMemberExpression(node.parent))
29
31
  return false;
30
32
 
31
- const supported = node.elements
32
- .every(isSupportedNode);
33
+ const supported = node.elements.every(isSupportedNode);
33
34
 
34
35
  if (!supported)
35
36
  return false;
36
37
 
37
- if (!isVariableDeclarator(node.parent))
38
+ if (isCallExpression(node.parent))
39
+ return false;
40
+
41
+ if (node.parent.parent.type === 'Property')
38
42
  return false;
39
43
 
40
- if (node.elements.length < 5)
44
+ if (node.parent.type === 'Property' && node.parent.key.value !== 'plugins')
45
+ return false;
46
+
47
+ if (isArrayExpression(node.parent))
48
+ return false;
49
+
50
+ if (differentTypes(node))
51
+ return false;
52
+
53
+ if (/Statement/.test(node.parent.type))
54
+ return false;
55
+
56
+ if (node.elements.length < 5 && isShortValues(node.elements))
41
57
  return false;
42
58
 
43
59
  if (regexp.test(text))
@@ -45,15 +61,48 @@ module.exports.filter = ({text, node}) => {
45
61
 
46
62
  return false;
47
63
  };
48
-
49
64
  module.exports.fix = ({text}) => {
50
- return text
51
- .replace(/\[/g, '[\n')
52
- .replace(/\]/g, '\n]')
65
+ return text.replace(/\[/g, '[\n').replace(/\]/g, '\n]')
53
66
  .replace(/,/g, ',\n');
54
67
  };
68
+ module.exports.include = () => ['ArrayExpression'];
55
69
 
56
- module.exports.include = () => [
57
- 'ArrayExpression',
58
- ];
70
+ function isShortValues(elements) {
71
+ for (const {type, value} of elements) {
72
+ if (type === 'Literal' && value.length > 1)
73
+ return false;
74
+ }
75
+
76
+ return true;
77
+ }
78
+
79
+ function differentTypes({elements}) {
80
+ let hasLiteral = false;
81
+ let hasIdentifier = false;
82
+ let hasBool = false;
83
+ let hasStr = false;
84
+
85
+ for (const {type, value} of elements) {
86
+ if (type === 'Literal') {
87
+ hasLiteral = true;
88
+
89
+ if (isString(value))
90
+ hasStr = true;
91
+
92
+ if (isBool(value))
93
+ hasBool = true;
94
+
95
+ if (hasStr && hasBool)
96
+ return true;
97
+ }
98
+
99
+ if (type === 'Identifier')
100
+ hasIdentifier = true;
101
+
102
+ if (hasLiteral && hasIdentifier)
103
+ return true;
104
+ }
105
+
106
+ return false;
107
+ }
59
108
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-putout",
3
- "version": "16.4.0",
3
+ "version": "16.5.0",
4
4
  "type": "commonjs",
5
5
  "description": "ESLint plugin for 🐊Putout",
6
6
  "release": false,