1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict'
|
7 |
|
8 | const getDocsUrl = require('./lib/get-docs-url')
|
9 | const hasPromiseCallback = require('./lib/has-promise-callback')
|
10 | const isInsidePromise = require('./lib/is-inside-promise')
|
11 | const isCallback = require('./lib/is-callback')
|
12 |
|
13 | module.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 |
|
29 |
|
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 | }
|