Skip to content

Commit

Permalink
feat(valid-params): add exclude option (#515)
Browse files Browse the repository at this point in the history
Allows use of non-standard methods, e.g., Bluebird `catch`.
  • Loading branch information
brettz9 authored Oct 26, 2024
1 parent 907753f commit 7ff2cb9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 28 deletions.
24 changes: 12 additions & 12 deletions __tests__/no-new-statics.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ ruleTester.run('no-new-statics', rule, {
errors: [{ message: "Avoid calling 'new' on 'Promise.withResolvers()'" }],
},
{
code: [
'function foo() {',
' var a = getA()',
' return new Promise.resolve(a)',
'}',
].join('\n'),
output: [
'function foo() {',
' var a = getA()',
' return Promise.resolve(a)',
'}',
].join('\n'),
code: `
function foo() {
var a = getA()
return new Promise.resolve(a)
}
`,
output: `
function foo() {
var a = getA()
return Promise.resolve(a)
}
`,
errors: [{ message: "Avoid calling 'new' on 'Promise.resolve()'" }],
},
],
Expand Down
36 changes: 26 additions & 10 deletions __tests__/valid-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,33 @@ 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([',
' Promise.resolve(1),',
' Promise.resolve(2),',
' Promise.reject(Error()),',
'])',
' .then(console.log)',
' .catch(console.error)',
' .finally(console.log)',
].join('\n'),
`
Promise.all([
Promise.resolve(1),
Promise.resolve(2),
Promise.reject(Error()),
])
.then(console.log)
.catch(console.error)
.finally(console.log)
`,
],
invalid: [
// invalid Promise.resolve()
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/always-return.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ promise
})

// NG
var v = promise.then((x) => {
const v = promise.then((x) => {
console.log(x)
})
// NG
var v = await promise.then((x) => {
const v = await promise.then((x) => {
console.log(x)
})
function foo() {
Expand Down
6 changes: 3 additions & 3 deletions docs/rules/no-native.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ existence of a native promise implementation. Helpful if you want to use
#### Valid

```js
var Promise = require('bluebird')
var x = Promise.resolve('good')
const Promise = require('bluebird')
const x = Promise.resolve('good')
```

#### Invalid

```js
var x = Promise.resolve('bad')
const x = Promise.resolve('bad')
```
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 7ff2cb9

Please sign in to comment.