Skip to content

Commit 8324564

Browse files
brettz9scagood
andauthored
fix(no-callback-in-promise): false triggering of callback (#574)
Co-authored-by: Sebastian Good <[email protected]>
1 parent 24fd90a commit 8324564

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

__tests__/no-callback-in-promise.js

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ ruleTester.run('no-callback-in-promise', rule, {
4646
code: 'a.then(next).catch(next)',
4747
options: [{ exceptions: ['next'] }],
4848
},
49+
50+
// #572
51+
`while (!(step = call(next, iterator)).done) {
52+
if (result !== undefined) break;
53+
}`,
54+
// https://github.com/eslint-community/eslint-plugin-promise/issues/572#issuecomment-2501505747
55+
`function hasCallbackArg(callback) {
56+
console.log(callback);
57+
}`,
4958
],
5059

5160
invalid: [

rules/no-callback-in-promise.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const { getAncestors } = require('./lib/eslint-compat')
99
const getDocsUrl = require('./lib/get-docs-url')
10+
const hasPromiseCallback = require('./lib/has-promise-callback')
1011
const isInsidePromise = require('./lib/is-inside-promise')
1112
const isCallback = require('./lib/is-callback')
1213

@@ -67,20 +68,29 @@ module.exports = {
6768
const options = context.options[0] || {}
6869
const exceptions = options.exceptions || []
6970
if (!isCallback(node, exceptions)) {
70-
const callingName = node.callee.name || node.callee.property?.name
71-
const name =
72-
node.arguments && node.arguments[0] && node.arguments[0].name
73-
if (
74-
!exceptions.includes(name) &&
75-
CB_BLACKLIST.includes(name) &&
76-
(timeoutsErr || !TIMEOUT_WHITELIST.includes(callingName))
77-
) {
78-
context.report({
79-
node: node.arguments[0],
80-
messageId: 'callback',
81-
})
71+
const name = node.arguments?.[0]?.name
72+
if (hasPromiseCallback(node)) {
73+
const callingName = node.callee.name || node.callee.property?.name
74+
if (
75+
!exceptions.includes(name) &&
76+
CB_BLACKLIST.includes(name) &&
77+
(timeoutsErr || !TIMEOUT_WHITELIST.includes(callingName))
78+
) {
79+
context.report({
80+
node: node.arguments[0],
81+
messageId: 'callback',
82+
})
83+
}
84+
return
85+
}
86+
if (!timeoutsErr) {
87+
return
88+
}
89+
90+
if (!name) {
91+
// Will be handled elsewhere
92+
return
8293
}
83-
return
8494
}
8595

8696
const ancestors = getAncestors(context, node)

0 commit comments

Comments
 (0)