Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): add valid-params rule (#85)
Resolves #47
- Loading branch information
Showing
5 changed files
with
342 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
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,205 @@ | ||
'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' } | ||
] | ||
} | ||
] | ||
}) |
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,64 @@ | ||
# Ensures the proper number of arguments are passed to Promise functions (valid-params) | ||
|
||
Calling a Promise function with the incorrect number of arguments can lead to | ||
unexpected behavior or hard to spot bugs. | ||
|
||
## Rule Details | ||
|
||
This rule is aimed at flagging instances where a Promise function is called with | ||
the improper number of arguments. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
* `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 | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```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) | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you do not want to be notified when passing an invalid number of arguments to | ||
a Promise function (for example, when using a typechecker like Flow), you can | ||
safely disable this rule. |
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,65 @@ | ||
'use strict' | ||
|
||
const getDocsUrl = require('./lib/get-docs-url') | ||
const isPromise = require('./lib/is-promise') | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'Ensures the proper number of arguments are passed to Promise functions', | ||
url: getDocsUrl('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 }}', | ||
data: { name, numArgs } | ||
}) | ||
} | ||
break | ||
case 'then': | ||
if (numArgs < 1 || numArgs > 2) { | ||
context.report({ | ||
node, | ||
message: | ||
'Promise.{{ name }}() requires 1 or 2 arguments, but received {{ numArgs }}', | ||
data: { name, 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 }}', | ||
data: { name, numArgs } | ||
}) | ||
} | ||
break | ||
default: | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} |