From 83245645a1731b8720ba4b17951f0e98567f449c Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 27 Nov 2024 06:45:33 +0800 Subject: [PATCH] fix(`no-callback-in-promise`): false triggering of callback (#574) Co-authored-by: Sebastian Good <2230835+scagood@users.noreply.github.com> --- __tests__/no-callback-in-promise.js | 9 ++++++++ rules/no-callback-in-promise.js | 36 ++++++++++++++++++----------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/__tests__/no-callback-in-promise.js b/__tests__/no-callback-in-promise.js index 7a7a27f9..3a92d1ec 100644 --- a/__tests__/no-callback-in-promise.js +++ b/__tests__/no-callback-in-promise.js @@ -46,6 +46,15 @@ ruleTester.run('no-callback-in-promise', rule, { code: 'a.then(next).catch(next)', options: [{ exceptions: ['next'] }], }, + + // #572 + `while (!(step = call(next, iterator)).done) { + if (result !== undefined) break; + }`, + // https://github.com/eslint-community/eslint-plugin-promise/issues/572#issuecomment-2501505747 + `function hasCallbackArg(callback) { + console.log(callback); + }`, ], invalid: [ diff --git a/rules/no-callback-in-promise.js b/rules/no-callback-in-promise.js index 7bcf5cbd..959a63d8 100644 --- a/rules/no-callback-in-promise.js +++ b/rules/no-callback-in-promise.js @@ -7,6 +7,7 @@ const { getAncestors } = require('./lib/eslint-compat') const getDocsUrl = require('./lib/get-docs-url') +const hasPromiseCallback = require('./lib/has-promise-callback') const isInsidePromise = require('./lib/is-inside-promise') const isCallback = require('./lib/is-callback') @@ -67,20 +68,29 @@ module.exports = { const options = context.options[0] || {} const exceptions = options.exceptions || [] if (!isCallback(node, exceptions)) { - const callingName = node.callee.name || node.callee.property?.name - const name = - node.arguments && node.arguments[0] && node.arguments[0].name - if ( - !exceptions.includes(name) && - CB_BLACKLIST.includes(name) && - (timeoutsErr || !TIMEOUT_WHITELIST.includes(callingName)) - ) { - context.report({ - node: node.arguments[0], - messageId: 'callback', - }) + const name = node.arguments?.[0]?.name + if (hasPromiseCallback(node)) { + const callingName = node.callee.name || node.callee.property?.name + if ( + !exceptions.includes(name) && + CB_BLACKLIST.includes(name) && + (timeoutsErr || !TIMEOUT_WHITELIST.includes(callingName)) + ) { + context.report({ + node: node.arguments[0], + messageId: 'callback', + }) + } + return + } + if (!timeoutsErr) { + return + } + + if (!name) { + // Will be handled elsewhere + return } - return } const ancestors = getAncestors(context, node)