Skip to content

Commit 1fdca9e

Browse files
committed
fix(no-callback-in-promise): false triggering by callback
1 parent 24fd90a commit 1fdca9e

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

__tests__/no-callback-in-promise.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ ruleTester.run('no-callback-in-promise', rule, {
4646
code: 'a.then(next).catch(next)',
4747
options: [{ exceptions: ['next'] }],
4848
},
49+
50+
`
51+
while (!(step = call(next, iterator)).done) {
52+
if (result !== undefined) break;
53+
}
54+
`,
4955
],
5056

5157
invalid: [

rules/no-callback-in-promise.js

Lines changed: 24 additions & 13 deletions
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,30 @@ 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+
if (hasPromiseCallback(node)) {
72+
const callingName = node.callee.name || node.callee.property?.name
73+
const name = node.arguments?.[0]?.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+
const name = node.arguments?.[0]?.name
91+
if (!name) {
92+
// Will be handled elsewhere
93+
return
8294
}
83-
return
8495
}
8596

8697
const ancestors = getAncestors(context, node)

0 commit comments

Comments
 (0)