-
-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-await-in-promise-methods
rule (#2259)
Co-authored-by: fisker <lionkay@gmail.com> Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
4594020
commit a3be554
Showing
7 changed files
with
390 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Disallow using `await` in `Promise` method parameters | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs). | ||
|
||
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). | ||
|
||
<!-- end auto-generated rule header --> | ||
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> | ||
|
||
Using `await` on promises passed as arguments to `Promise.all()`, `Promise.allSettled()`, `Promise.any()`, or `Promise.race()` is likely a mistake. | ||
|
||
## Fail | ||
|
||
```js | ||
Promise.all([await promise, anotherPromise]); | ||
|
||
Promise.allSettled([await promise, anotherPromise]); | ||
|
||
Promise.any([await promise, anotherPromise]); | ||
|
||
Promise.race([await promise, anotherPromise]); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
Promise.all([promise, anotherPromise]); | ||
|
||
Promise.allSettled([promise, anotherPromise]); | ||
|
||
Promise.any([promise, anotherPromise]); | ||
|
||
Promise.race([promise, anotherPromise]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
const {isMethodCall} = require('./ast/index.js'); | ||
const {removeSpacesAfter} = require('./fix/index.js'); | ||
|
||
const MESSAGE_ID_ERROR = 'no-await-in-promise-methods/error'; | ||
const MESSAGE_ID_SUGGESTION = 'no-await-in-promise-methods/suggestion'; | ||
const messages = { | ||
[MESSAGE_ID_ERROR]: 'Promise in `Promise.{{method}}()` should not be awaited.', | ||
[MESSAGE_ID_SUGGESTION]: 'Remove `await`.', | ||
}; | ||
const METHODS = ['all', 'allSettled', 'any', 'race']; | ||
|
||
const isPromiseMethodCallWithArrayExpression = node => | ||
isMethodCall(node, { | ||
object: 'Promise', | ||
methods: METHODS, | ||
optionalMember: false, | ||
optionalCall: false, | ||
argumentsLength: 1, | ||
}) | ||
&& node.arguments[0].type === 'ArrayExpression'; | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => ({ | ||
* CallExpression(callExpression) { | ||
if (!isPromiseMethodCallWithArrayExpression(callExpression)) { | ||
return; | ||
} | ||
|
||
for (const element of callExpression.arguments[0].elements) { | ||
if (element?.type !== 'AwaitExpression') { | ||
continue; | ||
} | ||
|
||
yield { | ||
node: element, | ||
messageId: MESSAGE_ID_ERROR, | ||
data: { | ||
method: callExpression.callee.property.name, | ||
}, | ||
suggest: [ | ||
{ | ||
messageId: MESSAGE_ID_SUGGESTION, | ||
* fix(fixer) { | ||
const {sourceCode} = context; | ||
const awaitToken = context.sourceCode.getFirstToken(element); | ||
yield fixer.remove(awaitToken); | ||
yield removeSpacesAfter(awaitToken, sourceCode, fixer); | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
}, | ||
}); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow using `await` in `Promise` method parameters.', | ||
}, | ||
hasSuggestions: true, | ||
messages, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'Promise.all([promise1, promise2, promise3, promise4])', | ||
'Promise.allSettled([promise1, promise2, promise3, promise4])', | ||
'Promise.any([promise1, promise2, promise3, promise4])', | ||
'Promise.race([promise1, promise2, promise3, promise4])', | ||
'Promise.all(...[await promise])', | ||
'Promise.all([await promise], extraArguments)', | ||
'Promise.all()', | ||
'Promise.all(notArrayExpression)', | ||
'Promise.all([,])', | ||
'Promise[all]([await promise])', | ||
'Promise.all?.([await promise])', | ||
'Promise?.all([await promise])', | ||
'Promise.notListedMethod([await promise])', | ||
'NotPromise.all([await promise])', | ||
'Promise.all([(await promise, 0)])', | ||
'new Promise.all([await promise])', | ||
|
||
// We are not checking these cases | ||
'globalThis.Promise.all([await promise])', | ||
'Promise["all"]([await promise])', | ||
], | ||
invalid: [ | ||
'Promise.all([await promise])', | ||
'Promise.allSettled([await promise])', | ||
'Promise.any([await promise])', | ||
'Promise.race([await promise])', | ||
'Promise.all([, await promise])', | ||
'Promise.all([await promise,])', | ||
'Promise.all([await promise],)', | ||
'Promise.all([await (0, promise)],)', | ||
'Promise.all([await (( promise ))])', | ||
'Promise.all([await await promise])', | ||
'Promise.all([...foo, await promise1, await promise2])', | ||
'Promise.all([await /* comment*/ promise])', | ||
], | ||
}); |
Oops, something went wrong.