UNPKG

987 BJavaScriptView Raw
1/**
2 * Rule: no-promise-in-callback
3 * Discourage using promises inside of callbacks.
4 */
5
6'use strict'
7
8const getDocsUrl = require('./lib/get-docs-url')
9const isPromise = require('./lib/is-promise')
10const isInsideCallback = require('./lib/is-inside-callback')
11
12module.exports = {
13 meta: {
14 docs: {
15 url: getDocsUrl('no-promise-in-callback')
16 }
17 },
18 create(context) {
19 return {
20 CallExpression(node) {
21 if (!isPromise(node)) return
22
23 // if i'm returning the promise, it's probably not really a callback
24 // function, and I should be okay....
25 if (node.parent.type === 'ReturnStatement') return
26
27 // what about if the parent is an ArrowFunctionExpression
28 // would that imply an implicit return?
29
30 if (context.getAncestors().some(isInsideCallback)) {
31 context.report({
32 node: node.callee,
33 message: 'Avoid using promises inside of callbacks.'
34 })
35 }
36 }
37 }
38 }
39}