forked from eslint-community/eslint-plugin-promise
/
no-callback-in-promise.js
71 lines (68 loc) · 1.76 KB
/
no-callback-in-promise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Rule: no-callback-in-promise
* Avoid calling back inside of a promise
*/
'use strict'
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')
module.exports = {
meta: {
type: 'suggestion',
docs: {
url: getDocsUrl('no-callback-in-promise'),
},
messages: {
callback: 'Avoid calling back inside of a promise.',
},
schema: [
{
type: 'object',
properties: {
exceptions: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
],
},
create(context) {
return {
CallExpression(node) {
const options = context.options[0] || {}
const exceptions = options.exceptions || []
if (!isCallback(node, exceptions)) {
// in general we send you packing if you're not a callback
// but we also need to watch out for whatever.then(cb)
if (hasPromiseCallback(node)) {
const name =
node.arguments && node.arguments[0] && node.arguments[0].name
if (
name === 'callback' ||
name === 'cb' ||
name === 'next' ||
name === 'done'
) {
context.report({
node: node.arguments[0],
messageId: 'callback',
})
}
}
return
}
if (context.getAncestors().some(isInsidePromise)) {
context.report({
node,
messageId: 'callback',
})
}
},
}
},
}