-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #47
- Loading branch information
Showing
4 changed files
with
276 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,8 @@ Then configure the rules you want to use under the rules section. | |
"promise/no-promise-in-callback": "warn", | ||
"promise/no-callback-in-promise": "warn", | ||
"promise/avoid-new": "warn", | ||
"promise/no-return-in-finally": "warn" | ||
"promise/no-return-in-finally": "warn", | ||
"promise/valid-params": "warn" | ||
} | ||
} | ||
``` | ||
|
@@ -81,6 +82,7 @@ or start with the recommended rule set | |
| :warning: | `no-callback-in-promise` | Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead) | | ||
| :warning: | `avoid-new` | Avoid creating `new` promises outside of utility libs (use [pify][] instead) | | ||
| :warning: | `no-return-in-finally` | Disallow return statements in `finally()` | | ||
| :warning: | `valid-params` | Ensures the proper number of arguments are passed to Promise functions | | ||
| :seven: | `prefer-await-to-then` | Prefer `await` to `then()` for reading Promise values | | ||
| :seven: | `prefer-await-to-callbacks` | Prefer async/await to the callback pattern | | ||
|
||
|
@@ -252,6 +254,55 @@ myPromise.finally(function(val) { | |
}) | ||
``` | ||
|
||
### `valid-params` | ||
|
||
Ensures the proper number of arguments are passed to Promise functions | ||
|
||
#### Valid | ||
|
||
```js | ||
// Promise.all() requires 1 argument | ||
Promise.all([p1, p2, p3]) | ||
Promise.all(iterable) | ||
|
||
// Promise.race() requires 1 argument | ||
Promise.race([p1, p2, p3]) | ||
Promise.race(iterable) | ||
|
||
// Promise.resolve() requires 0 or 1 arguments | ||
Promise.resolve() | ||
Promise.resolve({}) | ||
Promise.resolve([1, 2, 3]) | ||
Promise.resolve(referenceToObject) | ||
|
||
// Promise.reject() requires 0 or 1 arguments | ||
Promise.reject() | ||
Promise.reject(Error()) | ||
Promise.reject(referenceToError) | ||
|
||
// Promise.then() requires 1 or 2 arguments | ||
somePromise().then(value => doSomething(value)) | ||
somePromise().then(successCallback, errorCallback) | ||
|
||
// Promise.catch() requires 1 argument | ||
somePromise().catch(error => { handleError(error) }) | ||
somePromise().catch(console.error) | ||
|
||
// Promise.finally() requires 1 argument | ||
somePromise().finally(() => { console.log('done!') }) | ||
somePromise().finally(console.log) | ||
``` | ||
|
||
#### Invalid | ||
|
||
* `Promise.all()` is called with 0 or 2+ arguments | ||
* `Promise.race()` is called with 0 or 2+ arguments | ||
* `Promise.resolve()` is called with 2+ arguments | ||
* `Promise.reject()` is called with 2+ arguments | ||
* `Promise.then()` is called with 0 or 3+ arguments | ||
* `Promise.catch()` is called with 0 or 2+ arguments | ||
* `Promise.finally()` is called with 0 or 2+ arguments | ||
|
||
## Etc | ||
|
||
- (c) MMXV jden <[email protected]> - ISC license. | ||
|
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,57 @@ | ||
'use strict' | ||
|
||
const isPromise = require('./lib/is-promise') | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Ensures the proper number of arguments are passed to Promise functions', | ||
url: 'https://github.com/xjamundx/eslint-plugin-promise#valid-params' | ||
} | ||
}, | ||
create (context) { | ||
return { | ||
CallExpression (node) { | ||
if (!isPromise(node)) { | ||
return | ||
} | ||
|
||
const name = node.callee.property.name | ||
const numArgs = node.arguments.length | ||
|
||
switch (name) { | ||
case 'resolve': | ||
case 'reject': | ||
if (numArgs > 1) { | ||
context.report({ | ||
node, | ||
message: `Promise.${name}() requires 0 or 1 arguments, but received ${numArgs}` | ||
}) | ||
} | ||
break | ||
case 'then': | ||
if (numArgs < 1 || numArgs > 2) { | ||
context.report({ | ||
node, | ||
message: `Promise.${name}() requires 1 or 2 arguments, but received ${numArgs}` | ||
}) | ||
} | ||
break | ||
case 'race': | ||
case 'all': | ||
case 'catch': | ||
case 'finally': | ||
if (numArgs !== 1) { | ||
context.report({ | ||
node, | ||
message: `Promise.${name}() requires 1 argument, but received ${numArgs}` | ||
}) | ||
} | ||
break | ||
default: | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} |
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,161 @@ | ||
'use strict' | ||
|
||
const rule = require('../rules/valid-params') | ||
const RuleTester = require('eslint').RuleTester | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6 | ||
} | ||
}) | ||
|
||
ruleTester.run('valid-params', rule, { | ||
valid: [ | ||
// valid Promise.resolve() | ||
'Promise.resolve()', | ||
'Promise.resolve(1)', | ||
'Promise.resolve({})', | ||
'Promise.resolve(referenceToSomething)', | ||
|
||
// valid Promise.reject() | ||
'Promise.reject()', | ||
'Promise.reject(1)', | ||
'Promise.reject({})', | ||
'Promise.reject(referenceToSomething)', | ||
'Promise.reject(Error())', | ||
|
||
// valid Promise.race() | ||
'Promise.race([])', | ||
'Promise.race(iterable)', | ||
'Promise.race([one, two, three])', | ||
|
||
// valid Promise.all() | ||
'Promise.all([])', | ||
'Promise.all(iterable)', | ||
'Promise.all([one, two, three])', | ||
|
||
// valid Promise.then() | ||
'somePromise().then(success)', | ||
'somePromise().then(success, failure)', | ||
'promiseReference.then(() => {})', | ||
'promiseReference.then(() => {}, () => {})', | ||
|
||
// valid Promise.catch() | ||
'somePromise().catch(callback)', | ||
'somePromise().catch(err => {})', | ||
'promiseReference.catch(callback)', | ||
'promiseReference.catch(err => {})', | ||
|
||
// valid Promise.finally() | ||
'somePromise().finally(callback)', | ||
'somePromise().finally(() => {})', | ||
'promiseReference.finally(callback)', | ||
'promiseReference.finally(() => {})', | ||
|
||
// integration test | ||
[ | ||
'Promise.all([', | ||
' Promise.resolve(1),', | ||
' Promise.resolve(2),', | ||
' Promise.reject(Error()),', | ||
'])', | ||
' .then(console.log)', | ||
' .catch(console.error)', | ||
' .finally(console.log)' | ||
].join('\n') | ||
], | ||
invalid: [ | ||
// invalid Promise.resolve() | ||
{ | ||
code: 'Promise.resolve(1, 2)', | ||
errors: [{ message: 'Promise.resolve() requires 0 or 1 arguments, but received 2' }] | ||
}, | ||
{ | ||
code: 'Promise.resolve({}, function() {}, 1, 2, 3)', | ||
errors: [{ message: 'Promise.resolve() requires 0 or 1 arguments, but received 5' }] | ||
}, | ||
|
||
// invalid Promise.reject() | ||
{ | ||
code: 'Promise.reject(1, 2, 3)', | ||
errors: [{ message: 'Promise.reject() requires 0 or 1 arguments, but received 3' }] | ||
}, | ||
{ | ||
code: 'Promise.reject({}, function() {}, 1, 2)', | ||
errors: [{ message: 'Promise.reject() requires 0 or 1 arguments, but received 4' }] | ||
}, | ||
|
||
// invalid Promise.race() | ||
{ | ||
code: 'Promise.race(1, 2)', | ||
errors: [{ message: 'Promise.race() requires 1 argument, but received 2' }] | ||
}, | ||
{ | ||
code: 'Promise.race({}, function() {}, 1, 2, 3)', | ||
errors: [{ message: 'Promise.race() requires 1 argument, but received 5' }] | ||
}, | ||
|
||
// invalid Promise.all() | ||
{ | ||
code: 'Promise.all(1, 2, 3)', | ||
errors: [{ message: 'Promise.all() requires 1 argument, but received 3' }] | ||
}, | ||
{ | ||
code: 'Promise.all({}, function() {}, 1, 2)', | ||
errors: [{ message: 'Promise.all() requires 1 argument, but received 4' }] | ||
}, | ||
|
||
// invalid Promise.then() | ||
{ | ||
code: 'somePromise().then()', | ||
errors: [{ message: 'Promise.then() requires 1 or 2 arguments, but received 0' }] | ||
}, | ||
{ | ||
code: 'somePromise().then(() => {}, () => {}, () => {})', | ||
errors: [{ message: 'Promise.then() requires 1 or 2 arguments, but received 3' }] | ||
}, | ||
{ | ||
code: 'promiseReference.then()', | ||
errors: [{ message: 'Promise.then() requires 1 or 2 arguments, but received 0' }] | ||
}, | ||
{ | ||
code: 'promiseReference.then(() => {}, () => {}, () => {})', | ||
errors: [{ message: 'Promise.then() requires 1 or 2 arguments, but received 3' }] | ||
}, | ||
|
||
// invalid Promise.catch() | ||
{ | ||
code: 'somePromise().catch()', | ||
errors: [{ message: 'Promise.catch() requires 1 argument, but received 0' }] | ||
}, | ||
{ | ||
code: 'somePromise().catch(() => {}, () => {})', | ||
errors: [{ message: 'Promise.catch() requires 1 argument, but received 2' }] | ||
}, | ||
{ | ||
code: 'promiseReference.catch()', | ||
errors: [{ message: 'Promise.catch() requires 1 argument, but received 0' }] | ||
}, | ||
{ | ||
code: 'promiseReference.catch(() => {}, () => {})', | ||
errors: [{ message: 'Promise.catch() requires 1 argument, but received 2' }] | ||
}, | ||
|
||
// invalid Promise.finally() | ||
{ | ||
code: 'somePromise().finally()', | ||
errors: [{ message: 'Promise.finally() requires 1 argument, but received 0' }] | ||
}, | ||
{ | ||
code: 'somePromise().finally(() => {}, () => {})', | ||
errors: [{ message: 'Promise.finally() requires 1 argument, but received 2' }] | ||
}, | ||
{ | ||
code: 'promiseReference.finally()', | ||
errors: [{ message: 'Promise.finally() requires 1 argument, but received 0' }] | ||
}, | ||
{ | ||
code: 'promiseReference.finally(() => {}, () => {})', | ||
errors: [{ message: 'Promise.finally() requires 1 argument, but received 2' }] | ||
} | ||
] | ||
}) |