Skip to content

Commit

Permalink
fix(no-callback-in-promise): false triggering of callback (#574)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Good <2230835+scagood@users.noreply.github.com>
  • Loading branch information
brettz9 and scagood authored Nov 26, 2024
1 parent 24fd90a commit 8324564
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
9 changes: 9 additions & 0 deletions __tests__/no-callback-in-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
36 changes: 23 additions & 13 deletions rules/no-callback-in-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8324564

Please sign in to comment.