UNPKG

1.03 kBJavaScriptView 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 type: 'suggestion',
15 docs: {
16 url: getDocsUrl('no-promise-in-callback'),
17 },
18 schema: [],
19 },
20 create(context) {
21 return {
22 CallExpression(node) {
23 if (!isPromise(node)) return
24
25 // if i'm returning the promise, it's probably not really a callback
26 // function, and I should be okay....
27 if (node.parent.type === 'ReturnStatement') return
28
29 // what about if the parent is an ArrowFunctionExpression
30 // would that imply an implicit return?
31
32 if (context.getAncestors().some(isInsideCallback)) {
33 context.report({
34 node: node.callee,
35 message: 'Avoid using promises inside of callbacks.',
36 })
37 }
38 },
39 }
40 },
41}