eslint-plugin-putout 16.3.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.
- package/README.md +2 -0
- package/lib/add-newline-after-function-call/index.js +0 -1
- package/lib/add-newline-before-function-call/index.js +0 -1
- package/lib/add-newlines-between-types-in-union/index.js +0 -1
- package/lib/array-element-newline/README.md +1 -1
- package/lib/array-element-newline/index.js +64 -15
- package/lib/index.js +2 -0
- package/lib/remove-empty-newline-between-declarations/README.md +17 -0
- package/lib/remove-empty-newline-between-declarations/index.js +60 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -51,6 +51,7 @@ Then configure the rules you want to use under the rules section.
|
|
|
51
51
|
"putout/newline-function-call-arguments": "error",
|
|
52
52
|
"putout/function-declaration-paren-newline": "error",
|
|
53
53
|
"putout/remove-newline-after-default-import": "error",
|
|
54
|
+
"putout/remove-newline-between-declarations": "error",
|
|
54
55
|
"putout/remove-newline-from-empty-object": "error",
|
|
55
56
|
"putout/remove-empty-newline-before-first-specifier": "error",
|
|
56
57
|
"putout/remove-empty-newline-after-last-specifier": "error",
|
|
@@ -105,6 +106,7 @@ Then configure the rules you want to use under the rules section.
|
|
|
105
106
|
- ✅ [Keyword spacing](/packages/eslint-plugin-putout/lib/keyword-spacing#readme)
|
|
106
107
|
- ✅ [Newline function call arguments](/packages/eslint-plugin-putout/lib/newline-function-call-arguments#readme)
|
|
107
108
|
- ✅ [Function declaration paren newline](/packages/eslint-plugin-putout/lib/function-declaration-paren-newline#readme)
|
|
109
|
+
- ✅ [Remove newline between declarations](/packages/eslint-plugin-putout/lib/remove-newline-between-declarations#readme)
|
|
108
110
|
- ✅ [Remove newline after default import](/packages/eslint-plugin-putout/lib/remove-newline-after-default-import#readme)
|
|
109
111
|
- ✅ [Remove newline from empty object](/packages/eslint-plugin-putout/lib/remove-newline-from-empty-object#readme)
|
|
110
112
|
- ✅ [Remove empty newline before first specifier](/packages/eslint-plugin-putout/lib/remove-empty-newline-before-first-specifier#readme)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# array-
|
|
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
|
-
|
|
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 (
|
|
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.
|
|
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
|
-
|
|
57
|
-
|
|
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/lib/index.js
CHANGED
|
@@ -48,6 +48,7 @@ module.exports.rules = {
|
|
|
48
48
|
...getWrapRule('nonblock-statement-body-newline'),
|
|
49
49
|
...getRule('putout'),
|
|
50
50
|
...getRule('remove-empty-newline-after-import'),
|
|
51
|
+
...getRule('remove-empty-newline-between-declarations'),
|
|
51
52
|
};
|
|
52
53
|
|
|
53
54
|
const config = require('@putout/eslint-config');
|
|
@@ -81,6 +82,7 @@ const recommended = {
|
|
|
81
82
|
'putout/remove-empty-newline-after-last-specifier': 'error',
|
|
82
83
|
'putout/remove-empty-newline-after-last-element': 'error',
|
|
83
84
|
'putout/remove-empty-newline-after-import': 'error',
|
|
85
|
+
'putout/remove-empty-newline-between-declarations': 'error',
|
|
84
86
|
'putout/remove-empty-specifiers': 'error',
|
|
85
87
|
'putout/objects-braces-inside-array': 'error',
|
|
86
88
|
'putout/object-property-newline': 'error',
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# remove-empty-newline-between-declarations
|
|
2
|
+
|
|
3
|
+
This rule aims to remove empty newline between Variable Declarations. Part of [**eslint-plugin-putout**](https://github.com/coderaiser/putout/tree/master/packages/eslint-plugin-putout#rules).
|
|
4
|
+
|
|
5
|
+
## ❌ Example of incorrect code
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
const {a} = b;
|
|
9
|
+
const {c} = a;
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## ✅ Example of correct code
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
const {a} = b;
|
|
16
|
+
const {c} = a;
|
|
17
|
+
```
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {isObjectPattern} = require('putout').types;
|
|
4
|
+
|
|
5
|
+
module.exports = {
|
|
6
|
+
meta: {
|
|
7
|
+
type: 'suggestion',
|
|
8
|
+
docs: {
|
|
9
|
+
description: 'Remove newline between declarations',
|
|
10
|
+
category: 'putout',
|
|
11
|
+
recommended: true,
|
|
12
|
+
},
|
|
13
|
+
fixable: 'code',
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
create(context) {
|
|
17
|
+
return {
|
|
18
|
+
VariableDeclaration(node) {
|
|
19
|
+
const source = context.getSourceCode();
|
|
20
|
+
const text = source.getText(node);
|
|
21
|
+
const newline = source.getText(node, 0, 2).replace(text, '');
|
|
22
|
+
|
|
23
|
+
if (newline !== '\n\n')
|
|
24
|
+
return;
|
|
25
|
+
|
|
26
|
+
const nextNode = context.getNodeByRangeIndex(node.range[1] + 2);
|
|
27
|
+
|
|
28
|
+
if (!nextNode || nextNode.type !== 'VariableDeclaration')
|
|
29
|
+
return;
|
|
30
|
+
|
|
31
|
+
const nodeId = node.declarations[0].id;
|
|
32
|
+
|
|
33
|
+
if (!isObjectPattern(nodeId))
|
|
34
|
+
return;
|
|
35
|
+
|
|
36
|
+
if (nodeId.properties.length !== 1)
|
|
37
|
+
return;
|
|
38
|
+
|
|
39
|
+
const textId = source.getText(nodeId.properties[0].value);
|
|
40
|
+
|
|
41
|
+
const nextNodeInit = nextNode.declarations[0].init;
|
|
42
|
+
const nextTextInit = source.getText(nextNodeInit);
|
|
43
|
+
|
|
44
|
+
if (textId !== nextTextInit)
|
|
45
|
+
return;
|
|
46
|
+
|
|
47
|
+
context.report({
|
|
48
|
+
node,
|
|
49
|
+
message: 'Remove empty newline between declarations',
|
|
50
|
+
|
|
51
|
+
fix(fixer) {
|
|
52
|
+
return [
|
|
53
|
+
fixer.removeRange([node.range[1], node.range[1] + 1]),
|
|
54
|
+
];
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-putout",
|
|
3
|
-
"version": "16.
|
|
3
|
+
"version": "16.5.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "ESLint plugin for 🐊Putout",
|
|
6
6
|
"release": false,
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"@putout/test": "^5.0.0",
|
|
60
60
|
"c8": "^7.5.0",
|
|
61
61
|
"eslint": "^8.0.1",
|
|
62
|
-
"eslint-plugin-eslint-plugin": "^
|
|
62
|
+
"eslint-plugin-eslint-plugin": "^5.0.6",
|
|
63
63
|
"madrun": "^9.0.0",
|
|
64
64
|
"mocha": "^10.0.0",
|
|
65
65
|
"montag": "^1.0.0",
|