Skip to content

Commit

Permalink
feat(valid-params): add exclude option; fixes eslint-community#127
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jul 30, 2024
1 parent 94c9834 commit 20f34a2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
16 changes: 16 additions & 0 deletions __tests__/valid-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ ruleTester.run('valid-params', rule, {
'promiseReference.finally(callback)',
'promiseReference.finally(() => {})',

{
code: `
somePromise.then(function() {
return sth();
}).catch(TypeError, function(e) {
//
}).catch(function(e) {
});
`,
options: [
{
exclude: ['catch'],
},
],
},

// integration test
[
'Promise.all([',
Expand Down
6 changes: 6 additions & 0 deletions docs/rules/valid-params.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ somePromise().finally(() => {
somePromise().finally(console.log)
```

## Options

### `exclude`

Array of method names to exclude from checks. Defaults to an empty array.

## When Not To Use It

If you do not want to be notified when passing an invalid number of arguments to
Expand Down
20 changes: 19 additions & 1 deletion rules/valid-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ module.exports = {
'Enforces the proper number of arguments are passed to Promise functions.',
url: getDocsUrl('valid-params'),
},
schema: [],
schema: [
{
type: 'object',
properties: {
exclude: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
],
messages: {
requireOneOptionalArgument:
'Promise.{{ name }}() requires 0 or 1 arguments, but received {{ numArgs }}',
Expand All @@ -22,6 +35,7 @@ module.exports = {
},
},
create(context) {
const { exclude = [] } = context.options[0] || {}
return {
CallExpression(node) {
if (!isPromise(node)) {
Expand All @@ -31,6 +45,10 @@ module.exports = {
const name = node.callee.property.name
const numArgs = node.arguments.length

if (exclude.includes(name)) {
return
}

// istanbul ignore next -- `isPromise` filters out others
switch (name) {
case 'resolve':
Expand Down

0 comments on commit 20f34a2

Please sign in to comment.