UNPKG

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