From 1fdca9e40888213fa1104a7dca9a9dac30b5b522 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 27 Nov 2024 02:58:50 +0800 Subject: [PATCH] fix(`no-callback-in-promise`): false triggering by callback --- __tests__/no-callback-in-promise.js | 6 +++++ rules/no-callback-in-promise.js | 37 +++++++++++++++++++---------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/__tests__/no-callback-in-promise.js b/__tests__/no-callback-in-promise.js index 7a7a27f9..b04d1c88 100644 --- a/__tests__/no-callback-in-promise.js +++ b/__tests__/no-callback-in-promise.js @@ -46,6 +46,12 @@ ruleTester.run('no-callback-in-promise', rule, { code: 'a.then(next).catch(next)', options: [{ exceptions: ['next'] }], }, + + ` + while (!(step = call(next, iterator)).done) { + if (result !== undefined) break; + } + `, ], invalid: [ diff --git a/rules/no-callback-in-promise.js b/rules/no-callback-in-promise.js index 7bcf5cbd..b66e24ab 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,30 @@ 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', - }) + if (hasPromiseCallback(node)) { + const callingName = node.callee.name || node.callee.property?.name + const name = 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', + }) + } + return + } + if (!timeoutsErr) { + return + } + + const name = node.arguments?.[0]?.name + if (!name) { + // Will be handled elsewhere + return } - return } const ancestors = getAncestors(context, node)