Blame view

node_modules/eslint-plugin-promise/rules/prefer-await-to-callbacks.js 1.9 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  'use strict'
  
  const getDocsUrl = require('./lib/get-docs-url')
  
  const errorMessage = 'Avoid callbacks. Prefer Async/Await.'
  
  module.exports = {
    meta: {
      docs: {
        url: getDocsUrl('prefer-await-to-callbacks')
      }
    },
    create(context) {
      function checkLastParamsForCallback(node) {
        const lastParam = node.params[node.params.length - 1] || {}
        if (lastParam.name === 'callback' || lastParam.name === 'cb') {
          context.report({ node: lastParam, message: errorMessage })
        }
      }
      function isInsideYieldOrAwait() {
        return context.getAncestors().some(parent => {
          return (
            parent.type === 'AwaitExpression' || parent.type === 'YieldExpression'
          )
        })
      }
      return {
        CallExpression(node) {
          // Callbacks aren't allowed.
          if (node.callee.name === 'cb' || node.callee.name === 'callback') {
            context.report({ node, message: errorMessage })
            return
          }
  
          // Then-ables aren't allowed either.
          const args = node.arguments
          const lastArgIndex = args.length - 1
          const arg = lastArgIndex > -1 && node.arguments[lastArgIndex]
          if (
            (arg && arg.type === 'FunctionExpression') ||
            arg.type === 'ArrowFunctionExpression'
          ) {
            // Ignore event listener callbacks.
            if (
              node.callee.property &&
              (node.callee.property.name === 'on' ||
                node.callee.property.name === 'once')
            ) {
              return
            }
            if (arg.params && arg.params[0] && arg.params[0].name === 'err') {
              if (!isInsideYieldOrAwait()) {
                context.report({ node: arg, message: errorMessage })
              }
            }
          }
        },
        FunctionDeclaration: checkLastParamsForCallback,
        FunctionExpression: checkLastParamsForCallback,
        ArrowFunctionExpression: checkLastParamsForCallback
      }
    }
  }