Commit 70e1a6a613e for nodejs
commit 70e1a6a613ee451fea6a77af7626c51605caefa5
Author: Antoine du Hamel <duhamelantoine1995@gmail.com>
Date: Mon Nov 30 23:01:03 2026 +0100
tools: add linter rule to enforce use of `SafePromiseAllReturnArrayLike`
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67791
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
diff --git a/test/parallel/test-eslint-no-unused-promise-all-result.js b/test/parallel/test-eslint-no-unused-promise-all-result.js
index a83a6d46d0d..6689c550d42 100644
--- a/test/parallel/test-eslint-no-unused-promise-all-result.js
+++ b/test/parallel/test-eslint-no-unused-promise-all-result.js
@@ -13,10 +13,7 @@ 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,
- }],
+ suggestions: Number(output !== undefined),
};
}
@@ -30,22 +27,21 @@ new RuleTester({
'SafePromiseAllReturnVoid(promises)',
'async function f() { await SafePromiseAllReturnVoid(promises); }',
'const values = SafePromiseAll(promises)',
- 'async function f() { const values = await SafePromiseAll(promises); }',
+ 'async function f() { const values = await SafePromiseAllReturnArrayLike(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)',
+ 'PromisePrototypeThen(SafePromiseAllReturnArrayLike(promises), (values) => {})',
+ 'PromisePrototypeThen(SafePromiseAllReturnArrayLike(promises), function({ 0: a }) {})',
+ 'const p = PromisePrototypeThen(SafePromiseAllReturnArrayLike(promises), undefined, onError)',
+ 'const p = PromisePrototypeCatch(SafePromiseAllReturnArrayLike(promises), onError)',
+ 'SafePromiseAllReturnArrayLike(promises).then((values) => {})',
+ 'const p = SafePromiseAllReturnArrayLike(promises).catch(onError)',
+ 'if (SafePromiseAllReturnArrayLike(promises)) {}',
+ 'SafePromiseAllReturnArrayLike(promises) ? a : b',
+ 'const x = (a, SafePromiseAllReturnArrayLike(promises))',
+ 'const x = a || SafePromiseAllReturnArrayLike(promises)',
'SafePromiseAllSettled(promises)',
'Promise.all(promises)',
'PromisePrototypeThen(promise, () => SafePromiseAll(promises))',
@@ -126,5 +122,95 @@ new RuleTester({
code: 'SafePromiseAll(promises)?.then()',
errors: [error('SafePromiseAll', 'SafePromiseAllReturnVoid(promises)?.then()')],
},
+
+ {
+ code: 'async function f() { const { 0: a, 1: b } = await SafePromiseAll(promises); }',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'async function f() { const { 0: { 0: code, 1: signal } } = await SafePromiseAll(promises); }',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'async function f() { const { length, ...rest } = await SafePromiseAll(promises); }',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'async function f() { const values = await SafePromiseAll(promises); return values[0]; }',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'async function f() { const values = await SafePromiseAll(promises, fn); ' +
+ 'for (let i = 0; i < values.length; i++) foo(values[i]); }',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'async function f() { const values = await SafePromiseAll(promises); values[0].bar(); }',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'async function f() { const values = await SafePromiseAll(promises); values["1"]; }',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'async function f() { return (await SafePromiseAll(promises))[0]; }',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'PromisePrototypeThen(SafePromiseAll(promises), ({ 0: a }) => a)',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'PromisePrototypeThen(SafePromiseAll(promises), function(values) { return values[0]; })',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
+ {
+ code: 'SafePromiseAll(promises).then((values) => values.length)',
+ errors: [{
+ message: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ suggestions: 1,
+ }],
+ },
],
});
diff --git a/tools/eslint-rules/no-unused-promise-all-result.js b/tools/eslint-rules/no-unused-promise-all-result.js
index cd0c4b3de4f..7faef476253 100644
--- a/tools/eslint-rules/no-unused-promise-all-result.js
+++ b/tools/eslint-rules/no-unused-promise-all-result.js
@@ -1,14 +1,31 @@
'use strict';
+const CANONICAL_INDEX = /^(?:0|[1-9]\d*)$/;
const PASSTHROUGH_METHODS = new Set(['catch', 'finally']);
+function isIndexOrLengthKey(key, computed) {
+ if (!computed) {
+ if (key.type === 'Identifier') return key.name === 'length';
+ if (key.type !== 'Literal') return false;
+ }
+ if (key.type !== 'Literal') {
+ // Computed keys such as `values[i]` are assumed to be indices, unless they
+ // look like a well-known symbol (e.g. `values[SymbolIterator]`).
+ return !(key.type === 'Identifier' && key.name.startsWith('Symbol'));
+ }
+ return key.value === 'length' || CANONICAL_INDEX.test(String(key.value));
+}
+
function isUndefined(node) {
return node == null || (node.type === 'Identifier' && node.name === 'undefined');
}
+function isFunction(node) {
+ return node?.type === 'ArrowFunctionExpression' || node?.type === 'FunctionExpression';
+}
+
function isFunctionWithoutParams(node) {
- return (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression') &&
- node.params.length === 0;
+ return isFunction(node) && node.params.length === 0;
}
// `onFulfilled` is the then-callback when `promise` is passed to
@@ -59,6 +76,65 @@ function isValueIgnored(node) {
}
}
+// Returns whether the value of `node` is only used to read its elements or its
+// `length`, so it never needs to be a genuine Array.
+function isOnlyReadByIndex(node, sourceCode) {
+ const { parent } = node;
+ switch (parent.type) {
+ case 'MemberExpression':
+ return parent.object === node &&
+ isIndexOrLengthKey(parent.property, parent.computed) &&
+ // `values[0]()` would expose the array as `this`.
+ !(parent.parent.type === 'CallExpression' && parent.parent.callee === parent);
+ case 'VariableDeclarator':
+ return parent.init === node && isArrayLikeCompatiblePattern(parent.id, parent, sourceCode);
+ default:
+ return false;
+ }
+}
+
+function isArrayLikeCompatiblePattern(pattern, declaringNode, sourceCode) {
+ switch (pattern.type) {
+ case 'AssignmentPattern':
+ return isArrayLikeCompatiblePattern(pattern.left, declaringNode, sourceCode);
+ case 'ObjectPattern':
+ return pattern.properties.every((property) =>
+ property.type === 'RestElement' ||
+ isIndexOrLengthKey(property.key, property.computed));
+ case 'Identifier': {
+ const variable = sourceCode.getDeclaredVariables(declaringNode)
+ .find(({ identifiers }) => identifiers.includes(pattern));
+ return variable != null && variable.references.every((reference) => {
+ if (reference.init) return true;
+ return !reference.isWrite() && isOnlyReadByIndex(reference.identifier, sourceCode);
+ });
+ }
+ default:
+ return false;
+ }
+}
+
+function isFulfillValueOnlyReadByIndex(node, sourceCode) {
+ const { parent } = node;
+ if (parent.type === 'AwaitExpression') {
+ return isOnlyReadByIndex(parent, sourceCode);
+ }
+
+ let onFulfilled;
+ if (parent.type === 'CallExpression' && parent.arguments[0] === node &&
+ parent.callee.type === 'Identifier' && parent.callee.name === 'PromisePrototypeThen') {
+ onFulfilled = parent.arguments[1];
+ } else if (parent.type === 'MemberExpression' && parent.object === node && !parent.computed &&
+ parent.property.name === 'then' && parent.parent.type === 'CallExpression' &&
+ parent.parent.callee === parent) {
+ onFulfilled = parent.parent.arguments[0];
+ }
+ // A then-callback without parameters ignores the value, which is reported
+ // by `no-unused-promise-all-result`.
+ return isFunction(onFulfilled) && onFulfilled.params.length > 0 &&
+ isArrayLikeCompatiblePattern(onFulfilled.params[0], onFulfilled, sourceCode);
+}
+
module.exports = {
meta: {
type: 'suggestion',
@@ -67,10 +143,16 @@ module.exports = {
messages: {
unusedResult: 'The fulfill value of `{{name}}` is not used, use `SafePromiseAllReturnVoid` instead.',
useReturnVoid: 'Replace `{{name}}` with `SafePromiseAllReturnVoid`.',
+
+ preferArrayLike: 'The fulfill value of `SafePromiseAll` is only read by index, ' +
+ 'use `SafePromiseAllReturnArrayLike` instead.',
+ useArrayLike: 'Replace `SafePromiseAll` with `SafePromiseAllReturnArrayLike`.',
},
},
create(context) {
+ const { sourceCode } = context;
+
return {
'CallExpression[callee.name=/^(Safe)?PromiseAll(ReturnArrayLike)?$/]'(node) {
const { callee } = node;
@@ -90,6 +172,20 @@ module.exports = {
}] : [],
});
},
+
+ 'CallExpression[callee.name=/^(Safe)?PromiseAll$/]'(node) {
+ const { callee } = node;
+ if (!isFulfillValueOnlyReadByIndex(node, sourceCode)) return;
+
+ context.report({
+ node,
+ messageId: 'preferArrayLike',
+ suggest: [{
+ messageId: 'useArrayLike',
+ fix: (fixer) => fixer.replaceText(callee, 'SafePromiseAllReturnArrayLike'),
+ }],
+ });
+ },
};
},
};