summaryrefslogtreecommitdiff
path: root/tools/eslint-rules/assert-throws-arguments.js
blob: 3b51188c0c89bc8964fac00c2595c5ef54f52436 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * @fileoverview Check that assert.throws is never called with a string as
 * second argument.
 * @author Michaël Zasso
 */
'use strict';

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

function checkThrowsArguments(context, node) {
  if (node.callee.type === 'MemberExpression' &&
      node.callee.object.name === 'assert' &&
      node.callee.property.name === 'throws') {
    const args = node.arguments;
    if (args.length > 3) {
      context.report({
        message: 'Too many arguments',
        node: node
      });
    } else if (args.length > 1) {
      const error = args[1];
      if (error.type === 'Literal' && typeof error.value === 'string' ||
          error.type === 'TemplateLiteral') {
        context.report({
          message: 'Unexpected string as second argument',
          node: error
        });
      }
    } else {
      if (context.options[0].requireTwo) {
        context.report({
          message: 'Expected at least two arguments',
          node: node
        });
      }
    }
  }
}

module.exports = {
  meta: {
    schema: [
      {
        type: 'object',
        properties: {
          requireTwo: {
            type: 'boolean'
          }
        }
      }
    ]
  },
  create: function(context) {
    return {
      CallExpression: (node) => checkThrowsArguments(context, node)
    };
  }
};