UNPKG

1.51 kBJavaScriptView Raw
1/**
2 * Rule: no-callback-in-promise
3 * Avoid calling back inside of a promise
4 */
5
6'use strict'
7
8const getDocsUrl = require('./lib/get-docs-url')
9const hasPromiseCallback = require('./lib/has-promise-callback')
10const isInsidePromise = require('./lib/is-inside-promise')
11const isCallback = require('./lib/is-callback')
12
13module.exports = {
14 meta: {
15 docs: {
16 url: getDocsUrl('no-callback-in-promise')
17 },
18 messages: {
19 callback: 'Avoid calling back inside of a promise.'
20 }
21 },
22 create(context) {
23 return {
24 CallExpression(node) {
25 const options = context.options[0] || {}
26 const exceptions = options.exceptions || []
27 if (!isCallback(node, exceptions)) {
28 // in general we send you packing if you're not a callback
29 // but we also need to watch out for whatever.then(cb)
30 if (hasPromiseCallback(node)) {
31 const name =
32 node.arguments && node.arguments[0] && node.arguments[0].name
33 if (
34 name === 'callback' ||
35 name === 'cb' ||
36 name === 'next' ||
37 name === 'done'
38 ) {
39 context.report({
40 node: node.arguments[0],
41 messageId: 'callback'
42 })
43 }
44 }
45 return
46 }
47 if (context.getAncestors().some(isInsidePromise)) {
48 context.report({
49 node,
50 messageId: 'callback'
51 })
52 }
53 }
54 }
55 }
56}