Skip to content

Commit 165e2e6

Browse files
committed
feature(eslint-plugin-putout) add add-newline-before-return
1 parent 8cd95c5 commit 165e2e6

File tree

25 files changed

+206
-5
lines changed

25 files changed

+206
-5
lines changed

Diff for: packages/babel-plugin-putout/lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = () => {
3636
generatorOverride(ast) {
3737
ast.program.directives = [];
3838
const code = print(ast);
39+
3940
return {code};
4041
},
4142
};

Diff for: packages/engine-runner/lib/merge-visitors.js

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function getStore(plugin, {fix, rule, shebang, msg, options}) {
117117
list.clear();
118118
upstore.clear();
119119
uplist.clear();
120+
120121
return placesStore.clear();
121122
};
122123

Diff for: packages/engine-runner/lib/store.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports.listStore = (list = new Set()) => {
2222
fn.clear = () => {
2323
const a = list;
2424
list = new Set();
25+
2526
return Array.from(a);
2627
};
2728

Diff for: packages/eslint-plugin-putout/.eslintrc.json

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
"@typescript-eslint/no-namespace": "off",
1212
"@typescript-eslint/no-inferrable-types": "off"
1313
}
14+
}, {
15+
"files": [
16+
"eslint-fixture/**/*.md{js}",
17+
"eslint-fixture/**/*.md{ts}"
18+
],
19+
"rules": {
20+
"putout/add-newline-before-return": "off"
21+
}
1422
}],
1523
"extends": [
1624
"plugin:node/recommended",

Diff for: packages/eslint-plugin-putout/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Then configure the rules you want to use under the rules section.
3737
"rules": {
3838
"putout/add-newlines-between-types-in-union": "error",
3939
"putout/add-newlines-between-specifiers": "error",
40+
"putout/add-newline-before-return": "error",
4041
"putout/add-newline-before-function-call": "error",
4142
"putout/add-newline-after-function-call": "error",
4243
"putout/putout": "error",
@@ -91,6 +92,7 @@ Then configure the rules you want to use under the rules section.
9192

9293
### Formatting
9394

95+
-[Add newline before return](/packages/eslint-plugin-putout/lib/add-newline-before-return#readme)
9496
-[Add newline before function call](/packages/eslint-plugin-putout/lib/add-newline-before-function-call#readme)
9597
-[Add newline after function call](/packages/eslint-plugin-putout/lib/add-newline-after-function-call#readme)
9698
-[Align spaces](/packages/eslint-plugin-putout/lib/align-spaces#readme)

Diff for: packages/eslint-plugin-putout/eslint-fixture/markdown.md

-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ throw Error('hi');
33
() => throw Error('hi');
44

55
const hello = 'world';
6-
76
return hello;
87
```
98

109
```ts
1110
throw Error('hi');
1211

1312
const hello: string = 'world';
14-
1513
return hello;
1614
```

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const {
1010

1111
const regExp = /^\n( +)?\n +$/;
1212

13-
module.exports.category = 'typescript';
1413
module.exports.report = () => 'Add newline before expression';
1514

1615
module.exports.filter = ({text, node, getCommentsBefore, getSpacesBeforeNode}) => {
@@ -57,8 +56,6 @@ module.exports.filter = ({text, node, getCommentsBefore, getSpacesBeforeNode}) =
5756

5857
return true;
5958
}
60-
61-
return false;
6259
};
6360

6461
module.exports.fix = ({text}) => {
@@ -68,4 +65,5 @@ module.exports.fix = ({text}) => {
6865
module.exports.include = () => [
6966
'CallExpression',
7067
'AssignmentExpression',
68+
'ReturnStatement',
7169
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"overrides": [{
3+
"files": "*.md{js}",
4+
"rules": {
5+
"putout/add-newline-before-return": "off"
6+
}
7+
}]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# add-newline-before-return
2+
3+
This rule aims to add newline before `return`. 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+
export function sum() {
9+
const a = 1;
10+
const b = 2;
11+
return a + b;
12+
}
13+
```
14+
15+
## ✅ Example of correct code
16+
17+
```js
18+
export function parse() {
19+
const a = 1;
20+
const b = 2;
21+
22+
return a + b;
23+
}
24+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function sum() {
2+
const a = 5;
3+
const b = 6;
4+
5+
return a + b;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function sum() {
2+
const a = 5;
3+
const b = 6;
4+
return a + b;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const regExp = /^\n( +)?\n +$/;
4+
5+
module.exports.report = () => `Add newline before 'return'`;
6+
7+
module.exports.filter = ({text, node, getCommentsBefore, getSpacesBeforeNode}) => {
8+
if (getCommentsBefore(node).length)
9+
return false;
10+
11+
const {parent} = node;
12+
const {body} = parent;
13+
14+
if (!body)
15+
return false;
16+
17+
const n = body.length;
18+
19+
if (n < 3)
20+
return false;
21+
22+
const spaces = getSpacesBeforeNode(node, text);
23+
24+
if (regExp.test(spaces))
25+
return false;
26+
27+
for (let i = 2; i < n; i++) {
28+
const prevA = body[i - 1];
29+
const spaces = getSpacesBeforeNode(prevA);
30+
31+
if (regExp.test(spaces))
32+
return false;
33+
}
34+
35+
return true;
36+
};
37+
38+
module.exports.fix = ({text}) => {
39+
return `\n${text}`;
40+
};
41+
42+
module.exports.include = () => [
43+
'ReturnStatement',
44+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
const {join} = require('path');
4+
5+
const {readFileSync} = require('fs');
6+
7+
const {RuleTester} = require('eslint');
8+
const montag = require('montag');
9+
10+
const wrap = require('../wrap');
11+
const rule = wrap(require('.'));
12+
13+
const ruleTester = new RuleTester({
14+
parserOptions: {
15+
ecmaVersion: 2022,
16+
sourceType: 'module',
17+
},
18+
});
19+
20+
const readFixture = (a) => readFileSync(join(__dirname, 'fixture', `${a}.js`), 'utf8');
21+
22+
ruleTester.run('add-newline-before-return', rule, {
23+
valid: [
24+
montag`
25+
test('hello: world', (t) => {
26+
const a = 5;
27+
return;
28+
});
29+
`,
30+
montag`
31+
test('hello: world', (t) => {
32+
return;
33+
});
34+
`,
35+
montag`
36+
test('hello: world', (t) => {
37+
const a = 5;
38+
const b = 4;
39+
40+
return a + b;
41+
});
42+
`, montag`
43+
test('hello: world', (t) => {
44+
const a = 5;
45+
const b = 4;
46+
// hello world
47+
return a + b;
48+
});
49+
`, montag`
50+
test('hello: world', (t) => {
51+
const a = 5;
52+
const x = 6;
53+
54+
fn();
55+
56+
const b = 4;
57+
return a + b;
58+
});
59+
`, montag`
60+
function x() {
61+
const a = 5;
62+
const b = 6;
63+
64+
if (a)
65+
return;
66+
}
67+
`,
68+
],
69+
70+
invalid: [{
71+
code: readFixture('return'),
72+
output: readFixture('return-fix'),
73+
errors: [{
74+
message: `Add newline before 'return'`,
75+
type: 'ReturnStatement',
76+
}],
77+
}],
78+
});
79+

Diff for: packages/eslint-plugin-putout/lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports.rules = {
2828
...getWrapRule('function-declaration-paren-newline'),
2929
...getWrapRule('add-newlines-between-types-in-union'),
3030
...getWrapRule('add-newlines-between-specifiers'),
31+
...getWrapRule('add-newline-before-return'),
3132
...getWrapRule('add-newline-before-function-call'),
3233
...getWrapRule('add-newline-after-function-call'),
3334
...getWrapRule('remove-newline-after-default-import'),
@@ -71,6 +72,7 @@ const recommended = {
7172
'putout/function-declaration-paren-newline': 'error',
7273
'putout/add-newlines-between-types-in-union': 'error',
7374
'putout/add-newlines-between-specifiers': 'error',
75+
'putout/add-newline-before-return': 'error',
7476
'putout/add-newline-before-function-call': 'error',
7577
'putout/add-newline-after-function-call': 'error',
7678
'putout/remove-newline-after-default-import': 'error',

Diff for: packages/eslint-plugin-putout/test/eslint.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,7 @@ test('eslint-plugin-putout: add-newlines-between-specifiers', async ({process})
179179
test('eslint-plugin-putout: object-property-newline', async ({process}) => {
180180
await process('object-property-newline');
181181
});
182+
183+
test('eslint-plugin-putout: add-newline-before-return', async ({process}) => {
184+
await process('add-newline-before-return');
185+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function sum() {
2+
const a = 5;
3+
const b = 6;
4+
5+
return a + b;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function sum() {
2+
const a = 5;
3+
const b = 6;
4+
return a + b;
5+
}

Diff for: packages/plugin-madrun/lib/set-lint-dot/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports.fix = ({lintPath}) => {
2323
if (isStringLiteral(node)) {
2424
node.value = dotLine;
2525
node.raw = dotLine;
26+
2627
return;
2728
}
2829

Diff for: packages/plugin-putout/lib/convert-node-to-path-in-get-template-values/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports.fix = ({path, __aPath, init}) => {
2424
if (compare(path, GET_TEMPLATE_VALUES_NODE)) {
2525
const {__a} = getTemplateValues(path, GET_TEMPLATE_VALUES_NODE);
2626
replaceWith(__aPath, __a);
27+
2728
return;
2829
}
2930

Diff for: packages/plugin-remove-useless-escape/lib/remove-useless-escape.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports.fix = (path) => {
1616
if (path.isStringLiteral()) {
1717
const {raw} = path.node;
1818
path.node.raw = unEscape(raw);
19+
1920
return;
2021
}
2122

Diff for: packages/plugin-tape/lib/apply-with-name/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ function checkStubs({__a, __b}, path) {
2626
const __array = {
2727
elements,
2828
};
29+
2930
return checkStubsArray({__array}, path);
3031
}
3132

3233
function applyWithName({__a, __b}, path) {
3334
applyWithNameToNode(__a, path);
3435
applyWithNameToNode(__b, path);
36+
3537
return path;
3638
}
3739

Diff for: packages/plugin-typescript/lib/convert-generic-to-shorthand/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports.fix = ({path, typeReference}) => {
1515
const {types} = typeReference;
1616
typeReference.types = types.map(tSArrayType);
1717
replaceWith(path, typeReference);
18+
1819
return;
1920
}
2021

Diff for: packages/plugin-typescript/lib/remove-useless-parens/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports.fix = ({path, parentPath, typeAnnotation}) => {
1616
const {types} = typeAnnotation;
1717
typeAnnotation.types = types.map(tSArrayType);
1818
replaceWith(parentPath, typeAnnotation);
19+
1920
return;
2021
}
2122

Diff for: packages/processor-ignore/lib/ignore.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const merge = (rawSource, list) => {
3636
function convertToArray(str) {
3737
const safeStr = str.replace(/\r/g, '');
3838
const lines = rmLast(safeStr).split(/\n/g);
39+
3940
return stringify(lines);
4041
}
4142

Diff for: packages/putout/lib/cli/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ module.exports = async ({argv, halt, log, write, logError, readFile, writeFile})
168168
if (args.help) {
169169
const help = require('./help');
170170
log(help());
171+
171172
return exit();
172173
}
173174

0 commit comments

Comments
 (0)