-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-single-promise-in-promise-methods
rule
- Loading branch information
1 parent
eb5af8b
commit f312b71
Showing
6 changed files
with
251 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,44 @@ | ||
# Disallow using `Promise` method with a single element array as parameter | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs). | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> | ||
|
||
Single element array parameter in a Promise.all(), Promise.any() or Promise.race() method is probably a mistake. | ||
|
||
## Fail | ||
|
||
```js | ||
Promise.all([promise]) | ||
Promise['all']([promise]) | ||
|
||
Promise.any([promise]) | ||
Promise['any']([promise]) | ||
|
||
Promise.race([promise]) | ||
Promise['race']([promise]) | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
Promise.all([promise, anotherPromise]) | ||
Promise['all']([promise, anotherPromise]) | ||
Promise.all(notArrayLiteral) | ||
Promise.all([...promises]) | ||
|
||
Promise.any([promise, anotherPromise]) | ||
Promise['any']([promise, anotherPromise]) | ||
Promise.any(notArrayLiteral) | ||
Promise.any([...promises]) | ||
|
||
Promise.race([promise, anotherPromise]) | ||
Promise['race']([promise, anotherPromise]) | ||
Promise.race(notArrayLiteral) | ||
Promise.race([...promises]) | ||
|
||
Promise.allSettled([promise]) | ||
``` |
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,54 @@ | ||
'use strict'; | ||
const isPromiseMethodWithArray = require('./utils/is-promise-method-with-array.js'); | ||
|
||
const MESSAGE_ID = 'no-single-promise-in-promise-methods'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Parameter in `Promise.{{method}}` should not be a single element array.', | ||
}; | ||
const METHODS = ['all', 'any', 'race']; | ||
|
||
const isPromiseMethodWithSinglePromise = (node, methods) => | ||
isPromiseMethodWithArray(node, methods) | ||
&& node.arguments[0].elements.length === 1 | ||
&& node.arguments[0].elements[0].type !== 'SpreadElement'; | ||
|
||
const getMethodName = node => node.callee.property.name; | ||
|
||
const getFixer = ({sourceCode}, node) => fixer => { | ||
const [element] = node.arguments[0].elements; | ||
const text = sourceCode.getText(element); | ||
const replacement = element.type === 'SequenceExpression' ? `(${text})` : text; | ||
|
||
return fixer.replaceText(node, replacement); | ||
}; | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => ({ | ||
CallExpression(node) { | ||
if (!isPromiseMethodWithSinglePromise(node, METHODS)) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID, | ||
data: { | ||
method: getMethodName(node), | ||
}, | ||
fix: getFixer(context, node), | ||
}); | ||
}, | ||
}); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow using `Promise` method with a single element array as parameter.', | ||
}, | ||
fixable: 'code', | ||
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,15 @@ | ||
'use strict'; | ||
const isMethodCall = (property, methods) => | ||
(property.type === 'Identifier' && methods.includes(property.name)) | ||
|| (property.type === 'Literal' && methods.includes(property.value)); | ||
|
||
const isPromiseMethodWithArray = (node, methods) => | ||
node.callee.type === 'MemberExpression' | ||
&& node.callee.object.type === 'Identifier' | ||
&& node.callee.object.name === 'Promise' | ||
&& node.callee.property.type === 'Identifier' | ||
&& isMethodCall(node.callee.property, methods) | ||
&& node.arguments.length === 1 | ||
&& node.arguments[0].type === 'ArrayExpression'; | ||
|
||
module.exports = isPromiseMethodWithArray; |
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,136 @@ | ||
import outdent from 'outdent'; | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
const error = { | ||
messageId: 'no-single-promise-in-promise-methods', | ||
}; | ||
|
||
test({ | ||
valid: [ | ||
outdent` | ||
Promise.all([promise, anotherPromise]) | ||
`, | ||
outdent` | ||
Promise[all]([promise, anotherPromise]) | ||
`, | ||
outdent` | ||
Promise.all(notArrayLiteral) | ||
`, | ||
outdent` | ||
Promise.all([...promises]) | ||
`, | ||
outdent` | ||
Promise.any([promise, anotherPromise]) | ||
`, | ||
outdent` | ||
Promise[any]([promise, anotherPromise]) | ||
`, | ||
outdent` | ||
Promise.any(notArrayLiteral) | ||
`, | ||
outdent` | ||
Promise.any([...promises]) | ||
`, | ||
outdent` | ||
Promise.race([promise, anotherPromise]) | ||
`, | ||
outdent` | ||
Promise[race]([promise, anotherPromise]) | ||
`, | ||
outdent` | ||
Promise.race(notArrayLiteral) | ||
`, | ||
outdent` | ||
Promise.race([...promises]) | ||
`, | ||
outdent` | ||
Promise.allSettled([promise]) | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: outdent` | ||
Promise.all([promise]) | ||
`, | ||
errors: [error], | ||
output: outdent` | ||
promise | ||
`, | ||
}, | ||
{ | ||
code: outdent` | ||
Promise[all]([promise]) | ||
`, | ||
errors: [error], | ||
output: outdent` | ||
promise | ||
`, | ||
}, | ||
{ | ||
code: outdent` | ||
Promise.all([(promise, anotherPromise)]) | ||
`, | ||
errors: [error], | ||
output: outdent` | ||
(promise, anotherPromise) | ||
`, | ||
}, | ||
{ | ||
code: outdent` | ||
Promise.any([promise]) | ||
`, | ||
errors: [error], | ||
output: outdent` | ||
promise | ||
`, | ||
}, | ||
{ | ||
code: outdent` | ||
Promise[any]([promise]) | ||
`, | ||
errors: [error], | ||
output: outdent` | ||
promise | ||
`, | ||
}, | ||
{ | ||
code: outdent` | ||
Promise.any([(promise, anotherPromise)]) | ||
`, | ||
errors: [error], | ||
output: outdent` | ||
(promise, anotherPromise) | ||
`, | ||
}, | ||
{ | ||
code: outdent` | ||
Promise.race([promise]) | ||
`, | ||
errors: [error], | ||
output: outdent` | ||
promise | ||
`, | ||
}, | ||
{ | ||
code: outdent` | ||
Promise[race]([promise]) | ||
`, | ||
errors: [error], | ||
output: outdent` | ||
promise | ||
`, | ||
}, | ||
{ | ||
code: outdent` | ||
Promise.race([(promise, anotherPromise)]) | ||
`, | ||
errors: [error], | ||
output: outdent` | ||
(promise, anotherPromise) | ||
`, | ||
}, | ||
], | ||
}); |