eslint-plugin-putout 11.15.2 → 11.18.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 CHANGED
@@ -35,6 +35,9 @@ Then configure the rules you want to use under the rules section.
35
35
  ```json
36
36
  {
37
37
  "rules": {
38
+ "putout/add-newlines-between-types-in-union": "error",
39
+ "putout/add-newline-before-function-call": "error",
40
+ "putout/add-newline-after-function-call": "error",
38
41
  "putout/putout": "error",
39
42
  "putout/array-element-newline": "error",
40
43
  "putout/single-property-destructuring": "error",
@@ -45,7 +48,6 @@ Then configure the rules you want to use under the rules section.
45
48
  "putout/keyword-spacing": "error",
46
49
  "putout/newline-function-call-arguments": "error",
47
50
  "putout/function-declaration-paren-newline": "error",
48
- "putout/add-newlines-between-types-in-union": "error",
49
51
  "putout/remove-newline-after-default-import": "error",
50
52
  "putout/remove-newline-from-empty-object": "error",
51
53
  "putout/remove-empty-newline-before-first-specifier": "error",
@@ -58,18 +60,21 @@ Then configure the rules you want to use under the rules section.
58
60
 
59
61
  ## Supported Rules
60
62
 
61
- - [Putout](lib/putout)
63
+ - [Add newlines between types in union](/packages/eslint-plugin-putout/lib/add-newlines-between-types-in-union)
64
+ - [Add newline before function call](/packages/eslint-plugin-putout/lib/add-newline-before-function-call)
65
+ - [Add newline after function call](/packages/eslint-plugin-putout/lib/add-newline-after-function-call)
66
+ - [Align spaces](/packages/eslint-plugin-putout/lib/align-spaces)
62
67
  - [Array element newline](/packages/eslint-plugin-putout/lib/array-element-newline)
68
+ - [Evaluate](/packages/eslint-plugin-putout/lib/evaluate)
69
+ - [Putout](lib/putout)
63
70
  - [Single property destructuring](/packages/eslint-plugin-putout/lib/single-property-destructuring)
64
71
  - [Multiple properties destructuring](/packages/eslint-plugin-putout/lib/multiple-properties-destructuring)
65
72
  - [For-of multiple properties destructuring](/packages/eslint-plugin-putout/lib/for-of-multiple-properties-destructuring)
66
73
  - [Long properties destructuring](/packages/eslint-plugin-putout/lib/long-properties-destructuring)
67
74
  - [Destructuring as function argument](/packages/eslint-plugin-putout/lib/destructuring-as-function-argument)
68
- - [Align spaces](/packages/eslint-plugin-putout/lib/align-spaces)
69
75
  - [Keyword spacing](/packages/eslint-plugin-putout/lib/keyword-spacing)
70
76
  - [Newline function call arguments](/packages/eslint-plugin-putout/lib/newline-function-call-arguments)
71
77
  - [Function declaration paren newline](/packages/eslint-plugin-putout/lib/function-declaration-paren-newline)
72
- - [Add newlines between types in union](/packages/eslint-plugin-putout/lib/add-newlines-between-types-in-union)
73
78
  - [Remove newline after default import](/packages/eslint-plugin-putout/lib/remove-newline-after-default-import)
74
79
  - [Remove newline from empty object](/packages/eslint-plugin-putout/lib/remove-newline-from-empty-object)
75
80
  - [Remove empty newline before first specifier](/packages/eslint-plugin-putout/lib/remove-empty-newline-before-first-specifier)
@@ -77,7 +82,6 @@ Then configure the rules you want to use under the rules section.
77
82
  - [Objects braces inside array](/packages/eslint-plugin-putout/lib/objects-braces-inside-array)
78
83
  - [Object init](/packages/eslint-plugin-putout/lib/object-init)
79
84
  - [No unresolved](/packages/eslint-plugin-putout/lib/no-unresolved)
80
- - [Evaluate](/packages/eslint-plugin-putout/lib/evaluate)
81
85
  - [Tape: add newline before assertion]('/packages/eslint-plugin-putout/lib/tape-add-new-line-before-assertion)
82
86
  - [Tape: add newline between tests]('/packages/eslint-plugin-putout/lib/tape-add-new-line-between-tests)
83
87
  - [Tape: remove newline before t.end()]('/packages/eslint-plugin-putout/lib/tape-remove-newline-before-t-end)
@@ -99,14 +103,15 @@ When using `putout` in IDE with `--fix` on save, or when you want to disable the
99
103
 
100
104
  List of disabled `putout` rules:
101
105
 
102
- - [remove-empty](https://github.com/coderaiser/putout/tree/v20.0.0/packages/plugin-remove-empty);
103
- - [remove-unused-variables](https://github.com/coderaiser/putout/tree/v20.0.0/packages/remove-unused-variables);
104
- - [remove-unused-types](https://github.com/coderaiser/putout/tree/v20.0.0/packages/remove-unused-types);
105
- - [remove-unused-for-of-variables](https://github.com/coderaiser/putout/tree/v20.0.0/packages/remove-unused-for-of-variables);
106
- - [remove-unused-expressions](https://github.com/coderaiser/putout/tree/v20.0.0/packages);
106
+ - [remove-empty](https://github.com/coderaiser/putout/tree/v22.0.0/packages/plugin-remove-empty);
107
+ - [remove-unused-variables](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-unused-variables);
108
+ - [remove-unused-types](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-unused-types);
109
+ - [remove-unused-for-of-variables](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-unused-for-of-variables);
110
+ - [remove-unused-expressions](https://github.com/coderaiser/putout/tree/v22.0.0/packages);
107
111
  - [remove-useless-return](https://github.com/coderaiser/putout/tree/master/remove-useless-return);
108
- - [remove-skip](https://github.com/coderaiser/putout/tree/v20.0.0/packages/remove-skip);
109
- - [remove-only](https://github.com/coderaiser/putout/tree/v20.0.0/packages/remove-only);
110
- - [remove-console](https://github.com/coderaiser/putout/tree/v20.0.0/packages/remove-console);
111
- - [remove-debugger](https://github.com/coderaiser/putout/tree/v20.0.0/packages/remove-debugger);
112
- - [convert-for-to-for-of](https://github.com/coderaiser/putout/tree/v20.0.0/packages/convert-for-to-for-of);
112
+ - [remove-skip](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-skip);
113
+ - [remove-only](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-only);
114
+ - [remove-console](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-console);
115
+ - [remove-debugger](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-debugger);
116
+ - [remove-unreachable-code](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-unreachable-code);
117
+ - [convert-for-to-for-of](https://github.com/coderaiser/putout/tree/v22.0.0/packages/convert-for-to-for-of);
@@ -0,0 +1,28 @@
1
+ # Add new line after function call (add-newline-after-function-call)
2
+
3
+ ## Rule Details
4
+
5
+ This rule aims to add newline after function call.
6
+
7
+ Examples of **incorrect** code for this rule:
8
+
9
+ ```js
10
+ export function parse() {
11
+ const a = 1;
12
+
13
+ fn();
14
+ const b = 2;
15
+ }
16
+ ```
17
+
18
+ Examples of **correct** code for this rule:
19
+
20
+ ```js
21
+ export function parse() {
22
+ const a = 1;
23
+
24
+ fn();
25
+
26
+ const b = 2;
27
+ }
28
+ ```
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('putout');
4
+
5
+ const {
6
+ isBlockStatement,
7
+ isExpressionStatement,
8
+ isVariableDeclaration,
9
+ isObjectExpression,
10
+ isArrayExpression,
11
+ } = types;
12
+
13
+ const regExp = /^;?\n( +)?\n +$/;
14
+
15
+ module.exports.category = 'typescript';
16
+ module.exports.report = () => 'Add newline after function call';
17
+
18
+ module.exports.filter = ({text, node, getText, getCommentsAfter}) => {
19
+ if (!isExpressionStatement(node.parent))
20
+ return false;
21
+
22
+ if (getCommentsAfter(node.parent).length)
23
+ return false;
24
+
25
+ const {parent} = node.parent;
26
+
27
+ if (!isBlockStatement(parent))
28
+ return false;
29
+
30
+ const {body} = parent;
31
+ const n = body.length;
32
+
33
+ if (n < 3)
34
+ return false;
35
+
36
+ const spaces = getSpacesAfterNode(node, {text, getText});
37
+
38
+ if (regExp.test(spaces))
39
+ return false;
40
+
41
+ for (let i = 0; i < n; i++) {
42
+ const current = body[i];
43
+
44
+ if (current !== node.parent)
45
+ continue;
46
+
47
+ if (i === n - 1)
48
+ break;
49
+
50
+ const next = body[i + 1];
51
+
52
+ if (!isVariableDeclaration(next))
53
+ break;
54
+
55
+ const {init} = next.declarations[0];
56
+
57
+ if (isObjectExpression(init) && init.properties.length)
58
+ return true;
59
+
60
+ if (isArrayExpression(init) && init.elements.length)
61
+ return true;
62
+
63
+ const spacesAfterNext = getSpacesAfterNode(next, {getText});
64
+
65
+ if (!regExp.test(spacesAfterNext))
66
+ return true;
67
+ }
68
+
69
+ return false;
70
+ };
71
+
72
+ module.exports.fix = ({text}) => {
73
+ return `${text};\n`;
74
+ };
75
+
76
+ module.exports.include = () => [
77
+ 'CallExpression',
78
+ ];
79
+
80
+ function getSpacesAfterNode(node, {getText, text = getText(node)}) {
81
+ let spaces = '';
82
+ let i = 0;
83
+
84
+ while (!spaces || /^[ \n;]+$/.test(spaces))
85
+ spaces = getText(node, 0, ++i)
86
+ .replace(text, '');
87
+
88
+ return spaces.slice(0, -1);
89
+ }
90
+
package/lib/index.js CHANGED
@@ -28,6 +28,7 @@ module.exports.rules = {
28
28
  ...getWrapRule('function-declaration-paren-newline'),
29
29
  ...getWrapRule('add-newlines-between-types-in-union'),
30
30
  ...getWrapRule('add-newline-before-function-call'),
31
+ ...getWrapRule('add-newline-after-function-call'),
31
32
  ...getWrapRule('remove-newline-after-default-import'),
32
33
  ...getWrapRule('remove-newline-from-empty-object'),
33
34
  ...getWrapRule('remove-empty-newline-before-first-specifier'),
@@ -64,6 +65,7 @@ const recommended = {
64
65
  'putout/function-declaration-paren-newline': 'error',
65
66
  'putout/add-newlines-between-types-in-union': 'error',
66
67
  'putout/add-newline-before-function-call': 'error',
68
+ 'putout/add-newline-after-function-call': 'error',
67
69
  'putout/remove-newline-after-default-import': 'error',
68
70
  'putout/remove-newline-from-empty-object': 'error',
69
71
  'putout/remove-empty-newline-before-first-specifier': 'error',
@@ -103,6 +105,7 @@ const safe = {
103
105
  'remove-unused-variables': 'off',
104
106
  'remove-unused-expressions': 'off',
105
107
  'remove-unused-for-of-variables': 'off',
108
+ 'remove-unreachable-code': 'off',
106
109
  'remove-useless-return': 'off',
107
110
  'tape/remove-skip': 'off',
108
111
  'tape/remove-only': 'off',
@@ -5,6 +5,7 @@ const {
5
5
  findPlaces,
6
6
  transform,
7
7
  print,
8
+ parse,
8
9
  } = require('putout');
9
10
 
10
11
  const v8 = require('v8');
@@ -47,7 +48,12 @@ module.exports = {
47
48
  const source = context.getSourceCode();
48
49
  const {text} = source;
49
50
 
50
- const ast = toBabel(copyAST(node));
51
+ const ast = parse(text, {
52
+ parser: {
53
+ parse: () => toBabel(copyAST(node)),
54
+ },
55
+ });
56
+
51
57
  const places = findPlaces(ast, text, resultOptions);
52
58
 
53
59
  for (const {rule, message, position} of places) {
@@ -77,9 +83,9 @@ const fix = ({ast, text, node, source, resultOptions}) => (fixer) => {
77
83
  includeComments,
78
84
  });
79
85
 
80
- const [, last] = lastToken.range;
81
-
82
86
  transform(ast, text, resultOptions);
87
+
88
+ const [, last] = lastToken.range;
83
89
  const code = print(ast);
84
90
 
85
91
  return fixer.replaceTextRange([0, last], code);
package/lib/wrap.js CHANGED
@@ -7,6 +7,7 @@ const prepare = (plugin, context, options) => (node) => {
7
7
  const filename = context.getFilename();
8
8
  const getText = source.getText.bind(source);
9
9
  const getCommentsBefore = source.getCommentsBefore.bind(source);
10
+ const getCommentsAfter = source.getCommentsAfter.bind(source);
10
11
 
11
12
  const text = getText(node);
12
13
 
@@ -16,6 +17,7 @@ const prepare = (plugin, context, options) => (node) => {
16
17
  options,
17
18
  getText,
18
19
  getCommentsBefore,
20
+ getCommentsAfter,
19
21
  filename,
20
22
  });
21
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-putout",
3
- "version": "11.15.2",
3
+ "version": "11.18.0",
4
4
  "description": "eslint plugin for putout",
5
5
  "release": false,
6
6
  "tag": false,
@@ -46,6 +46,7 @@
46
46
  "try-catch": "^3.0.0"
47
47
  },
48
48
  "devDependencies": {
49
+ "@typescript-eslint/parser": "^5.4.0",
49
50
  "c8": "^7.5.0",
50
51
  "eslint": "^8.0.1",
51
52
  "eslint-plugin-eslint-plugin": "^3.2.0",
@@ -54,7 +55,8 @@
54
55
  "montag": "^1.0.0",
55
56
  "simport": "^1.2.0",
56
57
  "supertape": "^6.6.0",
57
- "try-to-catch": "^3.0.0"
58
+ "try-to-catch": "^3.0.0",
59
+ "typescript": "^4.5.2"
58
60
  },
59
61
  "engines": {
60
62
  "node": ">=14"