eslint-plugin-putout 14.2.0 → 14.3.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.
@@ -8,6 +8,18 @@ Remove newline inside statement body. Part of [**eslint-plugin-putout**](https:/
8
8
  if (a)
9
9
 
10
10
  b();
11
+
12
+ for (a of b)
13
+
14
+ a();
15
+
16
+ while (a)
17
+
18
+ b();
19
+
20
+ for (;;)
21
+
22
+ a();
11
23
  ```
12
24
 
13
25
  ## ✅ Example of correct code
@@ -15,4 +27,13 @@ if (a)
15
27
  ```js
16
28
  if (a)
17
29
  b();
30
+
31
+ for (a of b)
32
+ a();
33
+
34
+ while (a)
35
+ b();
36
+
37
+ for (;;)
38
+ a();
18
39
  ```
@@ -1,26 +1,55 @@
1
1
  'use strict';
2
2
 
3
3
  const {types} = require('putout');
4
- const {isExpressionStatement} = types;
4
+ const {
5
+ isBlockStatement,
6
+ isIfStatement,
7
+ isExpressionStatement,
8
+ } = types;
5
9
 
6
10
  const reg = /\)\n(\s+)?\n/;
7
11
 
8
- module.exports.report = () => `Remove newline`;
12
+ module.exports.report = () => `Remove useless newline`;
9
13
 
10
14
  module.exports.fix = ({text}) => {
11
- return text.replace(reg, ')\n');
15
+ const result = text.replace(reg, ')\n');
16
+ return result;
12
17
  };
13
18
 
14
19
  module.exports.include = () => [
15
20
  'IfStatement',
21
+ 'ForOfStatement',
22
+ 'ForStatement',
23
+ 'WhileStatement',
16
24
  ];
17
25
 
18
- module.exports.filter = ({node, text}) => {
19
- const {consequent} = node;
26
+ module.exports.filter = ({text, node}) => {
27
+ if (isIf(text, node))
28
+ return true;
20
29
 
21
- if (!isExpressionStatement(consequent))
30
+ if (isBody(text, node))
31
+ return true;
32
+
33
+ return false;
34
+ };
35
+
36
+ function isIf(text, node) {
37
+ if (!isIfStatement(node))
38
+ return false;
39
+
40
+ const {consequent, alternate} = node;
41
+
42
+ if (!isExpressionStatement(alternate) && !isExpressionStatement(consequent))
22
43
  return false;
23
44
 
24
45
  return reg.test(text);
25
- };
46
+ }
26
47
 
48
+ function isBody(text, node) {
49
+ const {body} = node;
50
+
51
+ if (isBlockStatement(body))
52
+ return false;
53
+
54
+ return reg.test(text);
55
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-putout",
3
- "version": "14.2.0",
3
+ "version": "14.3.0",
4
4
  "type": "commonjs",
5
5
  "description": "eslint plugin for putout",
6
6
  "release": false,