diff --git a/lib/eslint.config_partial.mjs b/lib/eslint.config_partial.mjs index d9183423ed1e..41873397ba4b 100644 --- a/lib/eslint.config_partial.mjs +++ b/lib/eslint.config_partial.mjs @@ -432,6 +432,7 @@ export default [ 'node-core/no-crypto-class-instanceof': 'error', 'node-core/no-cryptokey-public-accessors': 'error', 'node-core/no-keyobject-public-accessors': 'error', + 'node-core/no-unused-promise-all-result': 'error', 'node-core/prefer-primordials': [ 'error', { name: 'AggregateError' }, diff --git a/test/parallel/test-eslint-no-unused-promise-all-result.js b/test/parallel/test-eslint-no-unused-promise-all-result.js new file mode 100644 index 000000000000..a83a6d46d0d1 --- /dev/null +++ b/test/parallel/test-eslint-no-unused-promise-all-result.js @@ -0,0 +1,130 @@ +'use strict'; + +const common = require('../common'); +if ((!common.hasCrypto) || (!common.hasIntl)) { + common.skip('ESLint tests require crypto and Intl'); +} + +common.skipIfEslintMissing(); + +const RuleTester = require('../../tools/eslint/node_modules/eslint').RuleTester; +const rule = require('../../tools/eslint-rules/no-unused-promise-all-result'); + +function error(name, output) { + return { + message: `The fulfill value of \`${name}\` is not used, use \`SafePromiseAllReturnVoid\` instead.`, + suggestions: output === undefined ? [] : [{ + desc: `Replace \`${name}\` with \`SafePromiseAllReturnVoid\`.`, + output, + }], + }; +} + +new RuleTester({ + languageOptions: { + ecmaVersion: 'latest', + sourceType: 'script', + }, +}).run('no-unused-promise-all-result', rule, { + valid: [ + 'SafePromiseAllReturnVoid(promises)', + 'async function f() { await SafePromiseAllReturnVoid(promises); }', + 'const values = SafePromiseAll(promises)', + 'async function f() { const values = await SafePromiseAll(promises); }', + 'async function f() { return SafePromiseAll(promises); }', + 'async function f() { return await SafePromiseAllReturnArrayLike(promises); }', + 'async function f() { const { 0: a } = await PromiseAll(promises); }', + 'const f = () => SafePromiseAll(promises)', + 'foo(SafePromiseAll(promises))', + 'PromisePrototypeThen(SafePromiseAll(promises), (values) => {})', + 'PromisePrototypeThen(SafePromiseAll(promises), function({ 0: a }) {})', + 'const p = PromisePrototypeThen(SafePromiseAll(promises), undefined, onError)', + 'const p = PromisePrototypeCatch(SafePromiseAll(promises), onError)', + 'SafePromiseAll(promises).then((values) => {})', + 'const p = SafePromiseAll(promises).catch(onError)', + 'if (SafePromiseAll(promises)) {}', + 'SafePromiseAll(promises) ? a : b', + 'const x = (a, SafePromiseAll(promises))', + 'const x = a || SafePromiseAll(promises)', + 'SafePromiseAllSettled(promises)', + 'Promise.all(promises)', + 'PromisePrototypeThen(promise, () => SafePromiseAll(promises))', + ], + invalid: [ + { + code: 'SafePromiseAll(promises)', + errors: [error('SafePromiseAll', 'SafePromiseAllReturnVoid(promises)')], + }, + { + code: 'SafePromiseAllReturnArrayLike(promises, fn)', + errors: [error('SafePromiseAllReturnArrayLike', 'SafePromiseAllReturnVoid(promises, fn)')], + }, + { + code: 'PromiseAll([a, b])', + errors: [error('PromiseAll', 'SafePromiseAllReturnVoid([a, b])')], + }, + { + code: 'PromiseAll(iterable)', + errors: [error('PromiseAll')], + }, + { + code: 'async function f() { await SafePromiseAll(promises); }', + errors: [error('SafePromiseAll', + 'async function f() { await SafePromiseAllReturnVoid(promises); }')], + }, + { + code: 'void SafePromiseAll(promises)', + errors: [error('SafePromiseAll', 'void SafePromiseAllReturnVoid(promises)')], + }, + { + code: 'async function f() { cond ? await SafePromiseAll(a) : await PromiseAll([b]); }', + errors: [ + error('SafePromiseAll', + 'async function f() { cond ? await SafePromiseAllReturnVoid(a) : await PromiseAll([b]); }'), + error('PromiseAll', + 'async function f() { cond ? await SafePromiseAll(a) : await SafePromiseAllReturnVoid([b]); }'), + ], + }, + { + code: 'cond && SafePromiseAll(promises)', + errors: [error('SafePromiseAll', 'cond && SafePromiseAllReturnVoid(promises)')], + }, + { + code: 'const x = (SafePromiseAll(promises), 1)', + errors: [error('SafePromiseAll', 'const x = (SafePromiseAllReturnVoid(promises), 1)')], + }, + { + code: 'PromisePrototypeThen(SafePromiseAll(promises), () => {})', + errors: [error('SafePromiseAll', 'PromisePrototypeThen(SafePromiseAllReturnVoid(promises), () => {})')], + }, + { + code: 'const p = PromisePrototypeThen(SafePromiseAll(promises), function() {}, onError)', + errors: [error('SafePromiseAll', + 'const p = PromisePrototypeThen(SafePromiseAllReturnVoid(promises), function() {}, onError)')], + }, + { + code: 'PromisePrototypeThen(SafePromiseAll(promises), undefined, onError)', + errors: [error('SafePromiseAll', + 'PromisePrototypeThen(SafePromiseAllReturnVoid(promises), undefined, onError)')], + }, + { + code: 'async function f() { await SafePromisePrototypeFinally(SafePromiseAll(promises), cleanup); }', + errors: [error( + 'SafePromiseAll', + `async function f() { await SafePromisePrototypeFinally(SafePromiseAllReturnVoid(promises), cleanup); }`, + )], + }, + { + code: 'SafePromiseAll(promises).then(() => {})', + errors: [error('SafePromiseAll', 'SafePromiseAllReturnVoid(promises).then(() => {})')], + }, + { + code: 'SafePromiseAll(promises).catch(onError)', + errors: [error('SafePromiseAll', 'SafePromiseAllReturnVoid(promises).catch(onError)')], + }, + { + code: 'SafePromiseAll(promises)?.then()', + errors: [error('SafePromiseAll', 'SafePromiseAllReturnVoid(promises)?.then()')], + }, + ], +}); diff --git a/tools/eslint-rules/no-unused-promise-all-result.js b/tools/eslint-rules/no-unused-promise-all-result.js new file mode 100644 index 000000000000..cd0c4b3de4fc --- /dev/null +++ b/tools/eslint-rules/no-unused-promise-all-result.js @@ -0,0 +1,95 @@ +'use strict'; + +const PASSTHROUGH_METHODS = new Set(['catch', 'finally']); + +function isUndefined(node) { + return node == null || (node.type === 'Identifier' && node.name === 'undefined'); +} + +function isFunctionWithoutParams(node) { + return (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression') && + node.params.length === 0; +} + +// `onFulfilled` is the then-callback when `promise` is passed to +// `PromisePrototypeThen` or used as `promise.then(...)`. +function isFulfillValueIgnoredByThen(onFulfilled, thenCall) { + if (isUndefined(onFulfilled)) return isValueIgnored(thenCall); + return isFunctionWithoutParams(onFulfilled); +} + +function isValueIgnored(node) { + const { parent } = node; + switch (parent.type) { + case 'ExpressionStatement': + return true; + case 'AwaitExpression': + case 'ChainExpression': + return isValueIgnored(parent); + case 'UnaryExpression': + return parent.operator === 'void'; + case 'SequenceExpression': + return parent.expressions.at(-1) !== node || isValueIgnored(parent); + case 'ConditionalExpression': + return parent.test !== node && isValueIgnored(parent); + case 'LogicalExpression': + return parent.right === node && isValueIgnored(parent); + case 'CallExpression': { + if (parent.arguments[0] !== node || parent.callee.type !== 'Identifier') return false; + const { name } = parent.callee; + if (name === 'PromisePrototypeThen') { + return isFulfillValueIgnoredByThen(parent.arguments[1], parent); + } + return name === 'SafePromisePrototypeFinally' && isValueIgnored(parent); + } + case 'MemberExpression': { + const call = parent.parent; + if (parent.object !== node || parent.computed || + call.type !== 'CallExpression' || call.callee !== parent) { + return false; + } + const { name } = parent.property; + if (name === 'then') { + return isFulfillValueIgnoredByThen(call.arguments[0], call); + } + return PASSTHROUGH_METHODS.has(name) && isValueIgnored(call); + } + default: + return false; + } +} + +module.exports = { + meta: { + type: 'suggestion', + hasSuggestions: true, + schema: [], + messages: { + unusedResult: 'The fulfill value of `{{name}}` is not used, use `SafePromiseAllReturnVoid` instead.', + useReturnVoid: 'Replace `{{name}}` with `SafePromiseAllReturnVoid`.', + }, + }, + + create(context) { + return { + 'CallExpression[callee.name=/^(Safe)?PromiseAll(ReturnArrayLike)?$/]'(node) { + const { callee } = node; + if (!isValueIgnored(node)) return; + + const { name } = callee; + // `PromiseAll` accepts any iterable, `SafePromiseAllReturnVoid` only array-likes. + const canSuggest = name !== 'PromiseAll' || node.arguments[0]?.type === 'ArrayExpression'; + context.report({ + node, + messageId: 'unusedResult', + data: { name }, + suggest: canSuggest ? [{ + messageId: 'useReturnVoid', + data: { name }, + fix: (fixer) => fixer.replaceText(callee, 'SafePromiseAllReturnVoid'), + }] : [], + }); + }, + }; + }, +};