Skip to content

Commit 281cd6f

Browse files
committed
feature: @putout/plugin-parens: remove-useless-for-parens: add
1 parent 384cc6f commit 281cd6f

File tree

10 files changed

+123
-34
lines changed

10 files changed

+123
-34
lines changed

docs/syntax-errors.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,11 @@ function get() {
321321
}
322322
```
323323

324+
<details><summary>remove useless <code>parens</code> for params </summary>
325+
326+
```diff
327+
-const a = ((b)) => c;
328+
+const a = (b) => c;
329+
```
330+
324331
</details>

packages/plugin-parens/README.md

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111
npm i @putout/plugin-parens
1212
```
1313

14-
## Rule
14+
## Rules
15+
16+
-[add-missing-for-await](#add-missing-for-await);
17+
-[add-missing-for-template](#add-missing-for-template);
18+
-[add-missing-for-assign](#add-missing-for-assign);
19+
-[remove-useless-for-await](#remove-useless-for-params);
20+
-[remove-useless-for-params](#remove-useless-for-params);
21+
22+
## Config
23+
24+
Short:
1525

1626
```json
1727
{
@@ -22,9 +32,21 @@ npm i @putout/plugin-parens
2232
}
2333
```
2434

25-
## add-missing
35+
Full:
2636

27-
### assign
37+
```json
38+
{
39+
"rules": {
40+
"parens/add-missing-for-await": "on",
41+
"parens/add-missing-for-template": "on",
42+
"parens/add-missing-for-assign": "on",
43+
"parens/remove-useless-for-await": "on",
44+
"parens/remove-useless-for-params": "on"
45+
}
46+
}
47+
```
48+
49+
## add-missing-for-assign
2850

2951
> The JavaScript exception "invalid assignment left-hand side" occurs when there was an unexpected assignment somewhere. It may be triggered when a single `=` sign was used instead of `==` or `===`.
3052
>
@@ -45,31 +67,21 @@ To disable use:
4567
+a && (b = a);
4668
```
4769

48-
### await
70+
## add-missing-for-await
4971

50-
To disable use:
51-
52-
```json
53-
{
54-
"rules": {
55-
"parens/add-missing-for-await": "off"
56-
}
57-
}
58-
```
59-
60-
#### ❌ Example of incorrect code
72+
### ❌ Example of incorrect code
6173

6274
```ts
6375
await asyncFn().filter(Boolean);
6476
```
6577

66-
#### ✅ Example of correct code
78+
### ✅ Example of correct code
6779

6880
```js
6981
(await asyncFn()).filter(Boolean);
7082
```
7183

72-
#### template
84+
## add-missing-for-template
7385

7486
> The JavaScript exception "tagged template cannot be used with optional chain" occurs when the tag expression of a tagged template literal is an optional chain, or if there's an optional chain between the tag and the template.
7587
>
@@ -85,50 +97,59 @@ Checkout in 🐊[**Putout Editor**](https://putout.vercel.app/#/gist/ef3f1e198a8
8597
}
8698
```
8799

88-
#### ❌ Example of incorrect code
100+
### ❌ Example of incorrect code
89101

90102
```ts
91103
getConsoleLog?.()``;
92104
String?.raw``;
93105
String?.raw!``;
94106
```
95107

96-
#### ✅ Example of correct code
108+
### ✅ Example of correct code
97109

98110
```ts
99111
(getConsoleLog?.())``;
100112
(String?.raw)``;
101113
(String?.raw)!``;
102114
```
103115

104-
## remove-useless
105-
106-
### await
107-
108-
To disable use:
109-
110-
```json
111-
{
112-
"rules": {
113-
"parens/remove-useless-for-await": "off"
114-
}
115-
}
116-
```
116+
## remove-useless-for-await
117117

118118
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/3800b0c52a199dd49a089ed4b9b37566/e2dddb75cb68811883cae640a22b340b8e1afa73).
119119

120-
#### ❌ Example of incorrect code
120+
### ❌ Example of incorrect code
121121

122122
```ts
123123
const s = (await m());
124124
```
125125

126-
#### ✅ Example of correct code
126+
### ✅ Example of correct code
127127

128128
```ts
129129
const s = await m();
130130
```
131131

132+
## remove-useless-for-params
133+
134+
> `Uncaught SyntaxError: Invalid destructuring assignment target`
135+
>
136+
> (c) Chrome
137+
138+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/8cbc3929366a2e6e5f9db64b461f5a2f/568ec40e8f4f0b0195dc44a078fa1a9ae62abe2b).
139+
140+
### ❌ Example of incorrect code
141+
142+
```
143+
const a = ((b)) => c;
144+
145+
```
146+
147+
### ✅ Example of correct code
148+
149+
```js
150+
const a = (b) => c;
151+
```
152+
132153
## License
133154

134155
MIT

packages/plugin-parens/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const addMissingForAwait = require('./add-missing-for-await');
44
const addMissingForTemplate = require('./add-missing-for-template');
55
const addMissingForAssign = require('./add-missing-for-assign');
66
const removeUselessForAwait = require('./remove-useless-for-await');
7+
const removeUselessForParams = require('./remove-useless-for-params');
78

89
module.exports.rules = {
910
'add-missing-for-awai': addMissingForAwait,
1011
'add-missing-for-template': addMissingForTemplate,
1112
'add-missing-for-assign': addMissingForAssign,
1213
'remove-useless-for-await': removeUselessForAwait,
14+
'remove-useless-for-params': removeUselessForParams,
1315
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a = (b) => c;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const a = ((b)) => c;
2+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const {operator} = require('putout');
4+
5+
const putout = require('putout');
6+
const {hasParens, removeParens} = operator;
7+
8+
module.exports.report = (path) => {
9+
const source = putout.print(path);
10+
const code = source.slice(1, -1);
11+
12+
return `Avoid useless parens: '${source}' -> '${code}'`;
13+
};
14+
15+
module.exports.fix = (path) => {
16+
removeParens(path);
17+
};
18+
19+
module.exports.traverse = ({push}) => ({
20+
Function(path) {
21+
const params = path.get('params');
22+
23+
for (const param of params) {
24+
if (hasParens(param))
25+
push(param);
26+
}
27+
},
28+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['remove-useless-for-params', plugin],
9+
],
10+
});
11+
12+
test('parens: remove-useless-for-params: report', (t) => {
13+
t.report('remove-useless-for-params', `Avoid useless parens: '(b)' -> 'b'`);
14+
t.end();
15+
});
16+
17+
test('parens: remove-useless-for-params: transform', (t) => {
18+
t.transform('remove-useless-for-params');
19+
t.end();
20+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a = (b) => c;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const a = ((b)) => c;
2+

packages/plugin-parens/test/parens.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ test('plugin-parens: transform: remove-useless-for-await', (t) => {
2828
t.transform('remove-useless-for-await');
2929
t.end();
3030
});
31+
32+
test('plugin-parens: transform: remove-useless-for-params', (t) => {
33+
t.transform('remove-useless-for-params');
34+
t.end();
35+
});

0 commit comments

Comments
 (0)