From 72c3d5b59d7093d316978bef75383fadde387093 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 2 Oct 2022 00:33:32 +0900 Subject: [PATCH] feat: add support for Promise.allSettled() and Promise.any() --- __tests__/catch-or-return.js | 16 +++++++++++++ __tests__/no-new-statics.js | 10 ++++++++ __tests__/no-promise-in-callback.js | 8 +++++++ __tests__/valid-params.js | 36 +++++++++++++++++++++++++++++ rules/lib/promise-statics.js | 2 ++ rules/valid-params.js | 2 ++ 6 files changed, 74 insertions(+) diff --git a/__tests__/catch-or-return.js b/__tests__/catch-or-return.js index 62d717e7..68d4135f 100644 --- a/__tests__/catch-or-return.js +++ b/__tests__/catch-or-return.js @@ -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 }], diff --git a/__tests__/no-new-statics.js b/__tests__/no-new-statics.js index 0eb9d12f..e0fbe214 100644 --- a/__tests__/no-new-statics.js +++ b/__tests__/no-new-statics.js @@ -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()', diff --git a/__tests__/no-promise-in-callback.js b/__tests__/no-promise-in-callback.js index 37830c3e..5f766640 100644 --- a/__tests__/no-promise-in-callback.js +++ b/__tests__/no-promise-in-callback.js @@ -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 }], diff --git a/__tests__/valid-params.js b/__tests__/valid-params.js index 88b406cf..ca671b95 100644 --- a/__tests__/valid-params.js +++ b/__tests__/valid-params.js @@ -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)', @@ -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() { diff --git a/rules/lib/promise-statics.js b/rules/lib/promise-statics.js index 8186ddd1..6aa8646e 100644 --- a/rules/lib/promise-statics.js +++ b/rules/lib/promise-statics.js @@ -2,6 +2,8 @@ module.exports = { all: true, + allSettled: true, + any: true, race: true, reject: true, resolve: true, diff --git a/rules/valid-params.js b/rules/valid-params.js index c01c333a..026fe17f 100644 --- a/rules/valid-params.js +++ b/rules/valid-params.js @@ -48,6 +48,8 @@ module.exports = { break case 'race': case 'all': + case 'allSettled': + case 'any': case 'catch': case 'finally': if (numArgs !== 1) {