summaryrefslogtreecommitdiff
path: root/tools/eslint-rules/prefer-common-mustnotcall.js
blob: 6bc428ed59a8b88afbe4c45e05a18c00a36f242f (plain)
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
/**
 * @fileoverview Prefer common.mustNotCall(msg) over common.mustCall(fn, 0)
 * @author James M Snell <jasnell@gmail.com>
 */
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

const msg = 'Please use common.mustNotCall(msg) instead of ' +
            'common.mustCall(fn, 0) or common.mustCall(0).';

function isCommonMustCall(node) {
  return node &&
         node.callee &&
         node.callee.object &&
         node.callee.object.name === 'common' &&
         node.callee.property &&
         node.callee.property.name === 'mustCall';
}

function isArgZero(argument) {
  return argument &&
         typeof argument.value === 'number' &&
         argument.value === 0;
}

module.exports = function(context) {
  return {
    CallExpression(node) {
      if (isCommonMustCall(node) &&
          (isArgZero(node.arguments[0]) ||  //  catch common.mustCall(0)
           isArgZero(node.arguments[1]))) { //  catch common.mustCall(fn, 0)
        context.report(node, msg);
      }
    }
  };
};