Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/eslint.config_partial.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
130 changes: 130 additions & 0 deletions test/parallel/test-eslint-no-unused-promise-all-result.js
Original file line number Diff line number Diff line change
@@ -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()')],
},
],
});
95 changes: 95 additions & 0 deletions tools/eslint-rules/no-unused-promise-all-result.js
Original file line number Diff line number Diff line change
@@ -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'),
}] : [],
});
},
};
},
};
Loading