Skip to content

Commit 6f8f828

Browse files
committed
feature: @putout/plugin-parens: remove-useless: add
1 parent e0ef81c commit 6f8f828

File tree

11 files changed

+81
-10
lines changed

11 files changed

+81
-10
lines changed

packages/plugin-parens/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"rules": {
55
"@typescript-eslint/no-unused-expressions": "off",
66
"no-unsafe-optional-chaining": "off",
7+
"@stylistic/ts/no-extra-parens": "off",
78
"@typescript-eslint/no-non-null-asserted-optional-chain": "off"
89
}
910
}],

packages/plugin-parens/README.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ npm i @putout/plugin-parens
2020
```json
2121
{
2222
"rules": {
23-
"parens/add-missing": "on"
23+
"parens/add-missing": "on",
24+
"parens/remove-useless": "on"
2425
}
2526
}
2627
```
2728

28-
## assign
29+
## add-missing
30+
31+
### assign
2932

3033
To disable use:
3134

@@ -37,14 +40,12 @@ To disable use:
3740
}
3841
```
3942

40-
## ❌ Example of incorrect code
41-
4243
```diff
4344
-a && b = a;
4445
+a && (b = a);
4546
```
4647

47-
## await
48+
### await
4849

4950
To disable use:
5051

@@ -56,19 +57,19 @@ To disable use:
5657
}
5758
```
5859

59-
## ❌ Example of incorrect code
60+
#### ❌ Example of incorrect code
6061

6162
```ts
6263
await asyncFn().filter(Boolean);
6364
```
6465

65-
## ✅ Example of correct code
66+
#### ✅ Example of correct code
6667

6768
```js
6869
(await asyncFn()).filter(Boolean);
6970
```
7071

71-
## template
72+
#### template
7273

7374
Checkout in 🐊[**Putout Editor**](https://putout.vercel.app/#/gist/ef3f1e198a8d5ebeb9dd3fd1fef8f305/c6b46a34037f5cb095b5419b748a24b6dc8e2933).
7475

@@ -80,22 +81,40 @@ Checkout in 🐊[**Putout Editor**](https://putout.vercel.app/#/gist/ef3f1e198a8
8081
}
8182
```
8283

83-
## ❌ Example of incorrect code
84+
#### ❌ Example of incorrect code
8485

8586
```ts
8687
getConsoleLog?.()``;
8788
String?.raw``;
8889
String?.raw!``;
8990
```
9091

91-
## ✅ Example of correct code
92+
#### ✅ Example of correct code
9293

9394
```ts
9495
(getConsoleLog?.())``;
9596
(String?.raw)``;
9697
(String?.raw)!``;
9798
```
9899

100+
## remove-useless
101+
102+
### await
103+
104+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/3800b0c52a199dd49a089ed4b9b37566/e2dddb75cb68811883cae640a22b340b8e1afa73).
105+
106+
#### ❌ Example of incorrect code
107+
108+
```ts
109+
const s = (await m());
110+
```
111+
112+
#### ✅ Example of correct code
113+
114+
```ts
115+
const s = await m();
116+
```
117+
99118
## License
100119

101120
MIT

packages/plugin-parens/lib/add-missing-for-await/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
types,
66
operator,
77
} = require('putout');
8+
89
const {AwaitExpression} = types;
910
const {replaceWith} = operator;
1011

packages/plugin-parens/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
const addMissingForAwait = require('./add-missing-for-await');
44
const addMissingForTemplate = require('./add-missing-for-template');
55
const addMissingForAssign = require('./add-missing-for-assign');
6+
const removeUselessForAwait = require('./remove-useless-for-await');
67

78
module.exports.rules = {
89
'add-missing-for-awai': addMissingForAwait,
910
'add-missing-for-template': addMissingForTemplate,
1011
'add-missing-for-assign': addMissingForAssign,
12+
'remove-useless-for-await': removeUselessForAwait,
1113
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var s = await m();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var s = (await m());
2+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
module.exports.report = () => `Remove useless parens around 'await'`;
4+
5+
module.exports.fix = (path) => {
6+
path.node.extra = {
7+
parenthesized: false,
8+
};
9+
};
10+
11+
module.exports.traverse = ({push}) => ({
12+
AwaitExpression(path) {
13+
if (path.parentPath.isVariableDeclarator())
14+
push(path);
15+
},
16+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
printer: 'putout',
8+
plugins: [
9+
['remove-useless-for-await', plugin],
10+
],
11+
});
12+
13+
test('parens: remove-useless-for-await: report', (t) => {
14+
t.report('remove-useless-for-await', `Remove useless parens around 'await'`);
15+
t.end();
16+
});
17+
18+
test('parens: remove-useless-for-await: transform', (t) => {
19+
t.transform('remove-useless-for-await');
20+
t.end();
21+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var s = await m();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var s = (await m());
2+

0 commit comments

Comments
 (0)