Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-callback-in-promise): false triggering of callback #574

Merged
merged 3 commits into from
Nov 26, 2024
Merged
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
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