Skip to content

Commit fa482cc

Browse files
authored
feat: add strict option to disallow then or catch following await or yield (#494)
1 parent f368c5a commit fa482cc

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

__tests__/prefer-await-to-then.js

+38
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ ruleTester.run('prefer-await-to-then', rule, {
1515
'async function hi() { await thing() }',
1616
'async function hi() { await thing().then() }',
1717
'async function hi() { await thing().catch() }',
18+
'async function hi() { await thing().finally() }',
19+
'function * hi() { yield thing().then() }',
1820
'a = async () => (await something())',
1921
`a = async () => {
2022
try { await something() } catch (error) { somethingElse() }
@@ -54,5 +56,41 @@ ruleTester.run('prefer-await-to-then', rule, {
5456
code: 'function foo() { hey.finally(x => {}) }',
5557
errors: [{ message }],
5658
},
59+
{
60+
code: 'async function hi() { await thing().then() }',
61+
errors: [{ message }],
62+
options: [
63+
{
64+
strict: true,
65+
},
66+
],
67+
},
68+
{
69+
code: 'async function hi() { await thing().catch() }',
70+
errors: [{ message }],
71+
options: [
72+
{
73+
strict: true,
74+
},
75+
],
76+
},
77+
{
78+
code: 'async function hi() { await thing().finally() }',
79+
errors: [{ message }],
80+
options: [
81+
{
82+
strict: true,
83+
},
84+
],
85+
},
86+
{
87+
code: 'function * hi() { yield thing().then() }',
88+
errors: [{ message }],
89+
options: [
90+
{
91+
strict: true,
92+
},
93+
],
94+
},
5795
],
5896
})

docs/rules/prefer-await-to-then.md

+15
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ function exampleFour() {
4444
return myPromise.finally(cleanup)
4545
}
4646
```
47+
48+
## Options
49+
50+
### `strict`
51+
52+
Normally, this rule allows `then` or `catch` following an `await` (or `yield`)
53+
expression. Setting this option to `true` will err on such cases:
54+
55+
This will fail with the `strict` option:
56+
57+
```js
58+
async function hi() {
59+
await thing().then()
60+
}
61+
```

rules/prefer-await-to-then.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ module.exports = {
1616
'Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values.',
1717
url: getDocsUrl('prefer-await-to-then'),
1818
},
19-
schema: [],
19+
schema: [
20+
{
21+
type: 'object',
22+
properties: {
23+
strict: {
24+
type: 'boolean',
25+
},
26+
},
27+
},
28+
],
2029
messages: {
2130
preferAwaitToCallback: 'Prefer await to then()/catch()/finally().',
2231
},
@@ -40,9 +49,11 @@ module.exports = {
4049
return getScope(context, node).block.type === 'Program'
4150
}
4251

52+
const { strict } = context.options[0] || {}
53+
4354
return {
4455
'CallExpression > MemberExpression.callee'(node) {
45-
if (isTopLevelScoped(node) || isInsideYieldOrAwait(node)) {
56+
if (isTopLevelScoped(node) || (!strict && isInsideYieldOrAwait(node))) {
4657
return
4758
}
4859

0 commit comments

Comments
 (0)