eslint-plugin-putout 11.13.0 → 11.15.2
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/lib/add-newline-before-function-call/README.md +26 -0
- package/lib/add-newline-before-function-call/index.js +78 -0
- package/lib/array-element-newline/README.md +2 -0
- package/lib/array-element-newline/index.js +1 -1
- package/lib/index.js +2 -0
- package/lib/tape-add-newline-before-assertion/README.md +5 -1
- package/package.json +1 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Add new line before function call (add-newline-before-function-call)
|
|
2
|
+
|
|
3
|
+
## Rule Details
|
|
4
|
+
|
|
5
|
+
This rule aims to add newline before function call.
|
|
6
|
+
|
|
7
|
+
Examples of **incorrect** code for this rule:
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
export function parse() {
|
|
11
|
+
const a = 1;
|
|
12
|
+
const b = 2;
|
|
13
|
+
fn();
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Examples of **correct** code for this rule:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
export function parse() {
|
|
21
|
+
const a = 1;
|
|
22
|
+
const b = 2;
|
|
23
|
+
|
|
24
|
+
fn();
|
|
25
|
+
}
|
|
26
|
+
```
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('putout');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
isBlockStatement,
|
|
7
|
+
isVariableDeclaration,
|
|
8
|
+
isExpressionStatement,
|
|
9
|
+
} = types;
|
|
10
|
+
|
|
11
|
+
const regExp = /^\n( +)?\n +$/;
|
|
12
|
+
|
|
13
|
+
module.exports.category = 'typescript';
|
|
14
|
+
module.exports.report = () => 'Add newline before function call';
|
|
15
|
+
|
|
16
|
+
module.exports.filter = ({text, node, getText, getCommentsBefore}) => {
|
|
17
|
+
if (!isExpressionStatement(node.parent))
|
|
18
|
+
return false;
|
|
19
|
+
|
|
20
|
+
if (getCommentsBefore(node.parent).length)
|
|
21
|
+
return false;
|
|
22
|
+
|
|
23
|
+
const {parent} = node.parent;
|
|
24
|
+
|
|
25
|
+
if (!isBlockStatement(parent))
|
|
26
|
+
return false;
|
|
27
|
+
|
|
28
|
+
const {body} = parent;
|
|
29
|
+
const n = body.length;
|
|
30
|
+
|
|
31
|
+
if (n < 3)
|
|
32
|
+
return false;
|
|
33
|
+
|
|
34
|
+
const spaces = getSpacesBeforeNode(node, {text, getText});
|
|
35
|
+
|
|
36
|
+
if (regExp.test(spaces))
|
|
37
|
+
return false;
|
|
38
|
+
|
|
39
|
+
for (let i = 2; i < n; i++) {
|
|
40
|
+
const current = body[i];
|
|
41
|
+
|
|
42
|
+
if (current !== node.parent)
|
|
43
|
+
continue;
|
|
44
|
+
|
|
45
|
+
const prevA = body[i - 1];
|
|
46
|
+
|
|
47
|
+
if (!isVariableDeclaration(prevA))
|
|
48
|
+
return false;
|
|
49
|
+
|
|
50
|
+
const spaces = getSpacesBeforeNode(prevA, {getText});
|
|
51
|
+
|
|
52
|
+
if (regExp.test(spaces))
|
|
53
|
+
return false;
|
|
54
|
+
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return false;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
module.exports.fix = ({text}) => {
|
|
62
|
+
return `\n${text}`;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
module.exports.include = () => [
|
|
66
|
+
'CallExpression',
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
function getSpacesBeforeNode(node, {getText, text = getText(node)}) {
|
|
70
|
+
let spaces = '';
|
|
71
|
+
let i = 0;
|
|
72
|
+
|
|
73
|
+
while (!spaces || /^[ \n]+$/.test(spaces))
|
|
74
|
+
spaces = getText(node, ++i)
|
|
75
|
+
.replace(text, '');
|
|
76
|
+
|
|
77
|
+
return spaces.slice(1);
|
|
78
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -27,6 +27,7 @@ module.exports.rules = {
|
|
|
27
27
|
...getWrapRule('newline-function-call-arguments'),
|
|
28
28
|
...getWrapRule('function-declaration-paren-newline'),
|
|
29
29
|
...getWrapRule('add-newlines-between-types-in-union'),
|
|
30
|
+
...getWrapRule('add-newline-before-function-call'),
|
|
30
31
|
...getWrapRule('remove-newline-after-default-import'),
|
|
31
32
|
...getWrapRule('remove-newline-from-empty-object'),
|
|
32
33
|
...getWrapRule('remove-empty-newline-before-first-specifier'),
|
|
@@ -62,6 +63,7 @@ const recommended = {
|
|
|
62
63
|
'putout/newline-function-call-arguments': 'error',
|
|
63
64
|
'putout/function-declaration-paren-newline': 'error',
|
|
64
65
|
'putout/add-newlines-between-types-in-union': 'error',
|
|
66
|
+
'putout/add-newline-before-function-call': 'error',
|
|
65
67
|
'putout/remove-newline-after-default-import': 'error',
|
|
66
68
|
'putout/remove-newline-from-empty-object': 'error',
|
|
67
69
|
'putout/remove-empty-newline-before-first-specifier': 'error',
|
|
@@ -10,7 +10,9 @@ Examples of **incorrect** code for this rule:
|
|
|
10
10
|
|
|
11
11
|
```js
|
|
12
12
|
test('lint: do some check', (t) => {
|
|
13
|
-
const
|
|
13
|
+
const expected = 1 + 2;
|
|
14
|
+
|
|
15
|
+
fn();
|
|
14
16
|
t.equal(result, 3);
|
|
15
17
|
t.end();
|
|
16
18
|
});
|
|
@@ -22,6 +24,8 @@ Examples of **correct** code for this rule:
|
|
|
22
24
|
test('lint: do some check', (t) => {
|
|
23
25
|
const result = 1 + 2;
|
|
24
26
|
|
|
27
|
+
fn();
|
|
28
|
+
|
|
25
29
|
t.equal(result, 3);
|
|
26
30
|
t.end();
|
|
27
31
|
});
|