Skip to content

Commit 18d13d8

Browse files
committed
feature: eslint-plugin-putout: add remove-empty-newline-between-declarations
1 parent 560b908 commit 18d13d8

File tree

44 files changed

+155
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+155
-35
lines changed

packages/eslint-plugin-putout/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Then configure the rules you want to use under the rules section.
5151
"putout/newline-function-call-arguments": "error",
5252
"putout/function-declaration-paren-newline": "error",
5353
"putout/remove-newline-after-default-import": "error",
54+
"putout/remove-newline-between-declarations": "error",
5455
"putout/remove-newline-from-empty-object": "error",
5556
"putout/remove-empty-newline-before-first-specifier": "error",
5657
"putout/remove-empty-newline-after-last-specifier": "error",
@@ -105,6 +106,7 @@ Then configure the rules you want to use under the rules section.
105106
-[Keyword spacing](/packages/eslint-plugin-putout/lib/keyword-spacing#readme)
106107
-[Newline function call arguments](/packages/eslint-plugin-putout/lib/newline-function-call-arguments#readme)
107108
-[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)
108110
-[Remove newline after default import](/packages/eslint-plugin-putout/lib/remove-newline-after-default-import#readme)
109111
-[Remove newline from empty object](/packages/eslint-plugin-putout/lib/remove-newline-from-empty-object#readme)
110112
-[Remove empty newline before first specifier](/packages/eslint-plugin-putout/lib/remove-empty-newline-before-first-specifier#readme)

packages/eslint-plugin-putout/lib/add-newline-after-function-call/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isBlockStatement,
76
isExpressionStatement,

packages/eslint-plugin-putout/lib/add-newline-before-function-call/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isBlockStatement,
76
isVariableDeclaration,

packages/eslint-plugin-putout/lib/add-newlines-between-types-in-union/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {isTSTypeAliasDeclaration} = types;
65

76
module.exports.category = 'typescript';

packages/eslint-plugin-putout/lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports.rules = {
4848
...getWrapRule('nonblock-statement-body-newline'),
4949
...getRule('putout'),
5050
...getRule('remove-empty-newline-after-import'),
51+
...getRule('remove-empty-newline-between-declarations'),
5152
};
5253

5354
const config = require('@putout/eslint-config');
@@ -81,6 +82,7 @@ const recommended = {
8182
'putout/remove-empty-newline-after-last-specifier': 'error',
8283
'putout/remove-empty-newline-after-last-element': 'error',
8384
'putout/remove-empty-newline-after-import': 'error',
85+
'putout/remove-empty-newline-between-declarations': 'error',
8486
'putout/remove-empty-specifiers': 'error',
8587
'putout/objects-braces-inside-array': 'error',
8688
'putout/object-property-newline': 'error',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"overrides": [{
3+
"files": "*.md{js}",
4+
"rules": {
5+
"putout/remove-empty-newline-after-import": "off"
6+
}
7+
}]
8+
}
Original file line numberDiff line numberDiff line change
@@ -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+
```
Original file line numberDiff line numberDiff line change
@@ -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+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const {RuleTester} = require('eslint');
4+
const montag = require('montag');
5+
6+
const rule = require('.');
7+
8+
const ruleTester = new RuleTester({
9+
parserOptions: {
10+
ecmaVersion: 2022,
11+
sourceType: 'module',
12+
},
13+
});
14+
15+
ruleTester.run('remove-empty-newline-between-declarations', rule, {
16+
valid: [
17+
montag`
18+
const {a} = b;
19+
const {c} = a;
20+
`,
21+
montag`
22+
const {m} = b;
23+
24+
const {c} = a;
25+
`,
26+
montag`
27+
const m = 'hello';
28+
29+
const {c} = a;
30+
`,
31+
montag`
32+
const {
33+
operator,
34+
types,
35+
} = require('putout');
36+
37+
const {replaceWith} = operator;
38+
`,
39+
],
40+
41+
invalid: [{
42+
code: montag`
43+
const {a} = b;
44+
45+
const {c} = a;
46+
`,
47+
output: montag`
48+
const {a} = b;
49+
const {c} = a;
50+
`,
51+
errors: [{
52+
message: 'Remove empty newline between declarations',
53+
type: 'VariableDeclaration',
54+
}],
55+
}],
56+
});
57+

packages/eslint-plugin-putout/test/eslint.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,7 @@ test('eslint-plugin-putout: multiple-properties-destructuring', async ({process}
188188
await process('multiple-properties-destructuring');
189189
});
190190

191+
test('eslint-plugin-putout: remove-newline-between-declarations', async ({process}) => {
192+
await process('remove-newline-between-declarations');
193+
});
194+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const {a} = b;
2+
const {c} = a;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const {a} = b;
2+
3+
const {c} = a;

packages/plugin-apply-maybe/lib/noop/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {isBlockStatement} = types;
65

76
module.exports.report = () => `Use 'noop()'`;

packages/plugin-apply-template-literals/lib/apply-template-literals.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isBinaryExpression,
76
isTemplateLiteral,

packages/plugin-convert-reduce-to-for-of/lib/convert-reduce-to-for-of.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isBlockStatement,
76
isArrayExpression,

packages/plugin-convert-typeof-to-is-type/lib/convert-typeof-to-is-type.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {operator} = require('putout');
4-
54
const {compare, getBindingPath} = operator;
65

76
const NAMES = {

packages/plugin-extract-object-properties/lib/not-equal-deep/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {operator} = require('putout');
4-
54
const {
65
replaceWith,
76
compare,

packages/plugin-nextjs/lib/convert-page-to-head/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {operator} = require('putout');
4-
54
const {
65
remove,
76
traverse,

packages/plugin-nextjs/lib/remove-a-from-link/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {isJSXElement} = types;
65

76
module.exports.report = () => `Remove '<a>' from <Link>, it always renders under the hood`;

packages/plugin-nodejs/lib/declare-after-require/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {operator} = require('putout');
4-
54
const {
65
remove,
76
compareAny,

packages/plugin-promises/lib/add-missing-await/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isIdentifier,
76
AwaitExpression,

packages/plugin-putout/lib/check-replace-code/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const generateCode = require('./generate-code');
88
const noop = () => {};
99

1010
const {operator} = putout;
11-
1211
const {
1312
compare,
1413
extract,

packages/plugin-putout/lib/replace-test-message/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {isCallExpression} = types;
65

76
module.exports.report = ({correct, operatorPath}) => {

packages/plugin-react-hook-form/lib/v7-apply-form-state/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
ObjectProperty,
76
ObjectPattern,

packages/plugin-react-router/lib/convert-component-to-element/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
JSXIdentifier,
76
JSXOpeningElement,

packages/plugin-remove-empty/lib/pattern/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isObjectPattern,
76
isArrayPattern,

packages/plugin-remove-empty/lib/static-block/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {operator} = require('putout');
4-
54
const {remove} = operator;
65

76
module.exports.report = () => 'Avoid useless empty static blocks';

packages/plugin-remove-unused-for-of-variables/lib/remove-unused-for-of-variables.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isIdentifier,
76
isObjectProperty,

packages/plugin-remove-unused-variables/lib/get-vars/typescript.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isIdentifier,
76
isTSModuleDeclaration,

packages/plugin-remove-unused-variables/lib/get-vars/use-params.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isIdentifier,
76
isObjectPattern,

packages/plugin-remove-useless-continue/lib/remove-useless-continue.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {operator} = require('putout');
4-
54
const {remove} = operator;
65

76
const isLoop = (path) => path.isLoop() || path.parentPath.isLoop();

packages/plugin-remove-useless-functions/lib/remove-useless-functions.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {isIdentifier} = types;
65

76
module.exports.report = () => 'Useless functions should be avoided';

packages/plugin-remove-useless-operand/lib/remove-useless-operand.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {
65
isNumericLiteral,
76
isIdentifier,

packages/plugin-remove-useless-spread/lib/array/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('putout');
4-
54
const {isNumericLiteral} = types;
65

76
module.exports.report = () => `Avoid useless spread '...'`;

0 commit comments

Comments
 (0)