Skip to content

Commit

Permalink
feat(catch-or-return,no-new-statics,no-promise-in-callback,valid-para…
Browse files Browse the repository at this point in the history
…ms): add support for `Promise.allSettled()` & `Promise.any()` (#370)
  • Loading branch information
ota-meshi authored Oct 1, 2022
1 parent 3a6fdbe commit e080f82
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 0 deletions.
16 changes: 16 additions & 0 deletions __tests__/catch-or-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ ruleTester.run('catch-or-return', rule, {
code: 'Promise.resolve(frank)',
errors: [{ message: catchMessage }],
},
{
code: 'Promise.all([])',
errors: [{ message: catchMessage }],
},
{
code: 'Promise.allSettled([])',
errors: [{ message: catchMessage }],
},
{
code: 'Promise.any([])',
errors: [{ message: catchMessage }],
},
{
code: 'Promise.race([])',
errors: [{ message: catchMessage }],
},
{
code: 'frank().then(to).catch(fn).then(foo)',
errors: [{ message: catchMessage }],
Expand Down
10 changes: 10 additions & 0 deletions __tests__/no-new-statics.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ ruleTester.run('no-new-statics', rule, {
output: 'Promise.all()',
errors: [{ message: "Avoid calling 'new' on 'Promise.all()'" }],
},
{
code: 'new Promise.allSettled()',
output: 'Promise.allSettled()',
errors: [{ message: "Avoid calling 'new' on 'Promise.allSettled()'" }],
},
{
code: 'new Promise.any()',
output: 'Promise.any()',
errors: [{ message: "Avoid calling 'new' on 'Promise.any()'" }],
},
{
code: 'new Promise.race()',
output: 'Promise.race()',
Expand Down
8 changes: 8 additions & 0 deletions __tests__/no-promise-in-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ ruleTester.run('no-promise-in-callback', rule, {
code: 'function x(err) { Promise.all() }',
errors: [{ message: errorMessage }],
},
{
code: 'function x(err) { Promise.allSettled() }',
errors: [{ message: errorMessage }],
},
{
code: 'function x(err) { Promise.any() }',
errors: [{ message: errorMessage }],
},
{
code: 'let x = (err) => doThingWith(err).then(a)',
errors: [{ message: errorMessage }],
Expand Down
36 changes: 36 additions & 0 deletions __tests__/valid-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ ruleTester.run('valid-params', rule, {
'Promise.all(iterable)',
'Promise.all([one, two, three])',

// valid Promise.allSettled()
'Promise.allSettled([])',
'Promise.allSettled(iterable)',
'Promise.allSettled([one, two, three])',

// valid Promise.any()
'Promise.any([])',
'Promise.any(iterable)',
'Promise.any([one, two, three])',

// valid Promise.then()
'somePromise().then(success)',
'somePromise().then(success, failure)',
Expand Down Expand Up @@ -129,6 +139,32 @@ ruleTester.run('valid-params', rule, {
{ message: 'Promise.all() requires 1 argument, but received 4' },
],
},
// invalid Promise.allSettled()
{
code: 'Promise.allSettled(1, 2, 3)',
errors: [
{ message: 'Promise.allSettled() requires 1 argument, but received 3' },
],
},
{
code: 'Promise.allSettled({}, function() {}, 1, 2)',
errors: [
{ message: 'Promise.allSettled() requires 1 argument, but received 4' },
],
},
// invalid Promise.any()
{
code: 'Promise.any(1, 2, 3)',
errors: [
{ message: 'Promise.any() requires 1 argument, but received 3' },
],
},
{
code: 'Promise.any({}, function() {}, 1, 2)',
errors: [
{ message: 'Promise.any() requires 1 argument, but received 4' },
],
},

// invalid Promise.then()
{
Expand Down
2 changes: 2 additions & 0 deletions rules/lib/promise-statics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module.exports = {
all: true,
allSettled: true,
any: true,
race: true,
reject: true,
resolve: true,
Expand Down
2 changes: 2 additions & 0 deletions rules/valid-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module.exports = {
break
case 'race':
case 'all':
case 'allSettled':
case 'any':
case 'catch':
case 'finally':
if (numArgs !== 1) {
Expand Down

0 comments on commit e080f82

Please sign in to comment.