Skip to content

Commit

Permalink
fix(no-callback-in-promise): false triggering by callback
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Nov 26, 2024
1 parent 24fd90a commit 1fdca9e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
6 changes: 6 additions & 0 deletions __tests__/no-callback-in-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
37 changes: 24 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,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)
Expand Down

0 comments on commit 1fdca9e

Please sign in to comment.