Skip to content

Commit 7ff2cb9

Browse files
authored
feat(valid-params): add exclude option (#515)
Allows use of non-standard methods, e.g., Bluebird `catch`.
1 parent 907753f commit 7ff2cb9

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

__tests__/no-new-statics.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ ruleTester.run('no-new-statics', rule, {
5353
errors: [{ message: "Avoid calling 'new' on 'Promise.withResolvers()'" }],
5454
},
5555
{
56-
code: [
57-
'function foo() {',
58-
' var a = getA()',
59-
' return new Promise.resolve(a)',
60-
'}',
61-
].join('\n'),
62-
output: [
63-
'function foo() {',
64-
' var a = getA()',
65-
' return Promise.resolve(a)',
66-
'}',
67-
].join('\n'),
56+
code: `
57+
function foo() {
58+
var a = getA()
59+
return new Promise.resolve(a)
60+
}
61+
`,
62+
output: `
63+
function foo() {
64+
var a = getA()
65+
return Promise.resolve(a)
66+
}
67+
`,
6868
errors: [{ message: "Avoid calling 'new' on 'Promise.resolve()'" }],
6969
},
7070
],

__tests__/valid-params.js

+26-10
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,33 @@ ruleTester.run('valid-params', rule, {
6161
'promiseReference.finally(callback)',
6262
'promiseReference.finally(() => {})',
6363

64+
{
65+
code: `
66+
somePromise.then(function() {
67+
return sth();
68+
}).catch(TypeError, function(e) {
69+
//
70+
}).catch(function(e) {
71+
});
72+
`,
73+
options: [
74+
{
75+
exclude: ['catch'],
76+
},
77+
],
78+
},
79+
6480
// integration test
65-
[
66-
'Promise.all([',
67-
' Promise.resolve(1),',
68-
' Promise.resolve(2),',
69-
' Promise.reject(Error()),',
70-
'])',
71-
' .then(console.log)',
72-
' .catch(console.error)',
73-
' .finally(console.log)',
74-
].join('\n'),
81+
`
82+
Promise.all([
83+
Promise.resolve(1),
84+
Promise.resolve(2),
85+
Promise.reject(Error()),
86+
])
87+
.then(console.log)
88+
.catch(console.error)
89+
.finally(console.log)
90+
`,
7591
],
7692
invalid: [
7793
// invalid Promise.resolve()

docs/rules/always-return.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ promise
7878
})
7979

8080
// NG
81-
var v = promise.then((x) => {
81+
const v = promise.then((x) => {
8282
console.log(x)
8383
})
8484
// NG
85-
var v = await promise.then((x) => {
85+
const v = await promise.then((x) => {
8686
console.log(x)
8787
})
8888
function foo() {

docs/rules/no-native.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ existence of a native promise implementation. Helpful if you want to use
1212
#### Valid
1313

1414
```js
15-
var Promise = require('bluebird')
16-
var x = Promise.resolve('good')
15+
const Promise = require('bluebird')
16+
const x = Promise.resolve('good')
1717
```
1818

1919
#### Invalid
2020

2121
```js
22-
var x = Promise.resolve('bad')
22+
const x = Promise.resolve('bad')
2323
```

docs/rules/valid-params.md

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ somePromise().finally(() => {
6262
somePromise().finally(console.log)
6363
```
6464

65+
## Options
66+
67+
### `exclude`
68+
69+
Array of method names to exclude from checks. Defaults to an empty array.
70+
6571
## When Not To Use It
6672

6773
If you do not want to be notified when passing an invalid number of arguments to

rules/valid-params.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,20 @@ module.exports = {
1111
'Enforces the proper number of arguments are passed to Promise functions.',
1212
url: getDocsUrl('valid-params'),
1313
},
14-
schema: [],
14+
schema: [
15+
{
16+
type: 'object',
17+
properties: {
18+
exclude: {
19+
type: 'array',
20+
items: {
21+
type: 'string',
22+
},
23+
},
24+
},
25+
additionalProperties: false,
26+
},
27+
],
1528
messages: {
1629
requireOneOptionalArgument:
1730
'Promise.{{ name }}() requires 0 or 1 arguments, but received {{ numArgs }}',
@@ -22,6 +35,7 @@ module.exports = {
2235
},
2336
},
2437
create(context) {
38+
const { exclude = [] } = context.options[0] || {}
2539
return {
2640
CallExpression(node) {
2741
if (!isPromise(node)) {
@@ -31,6 +45,10 @@ module.exports = {
3145
const name = node.callee.property.name
3246
const numArgs = node.arguments.length
3347

48+
if (exclude.includes(name)) {
49+
return
50+
}
51+
3452
// istanbul ignore next -- `isPromise` filters out others
3553
switch (name) {
3654
case 'resolve':

0 commit comments

Comments
 (0)