• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3function isAssert(node) {
4  return node.expression &&
5    node.expression.type === 'CallExpression' &&
6    node.expression.callee &&
7    node.expression.callee.name === 'assert';
8}
9
10function getFirstArg(expression) {
11  return expression.arguments && expression.arguments[0];
12}
13
14function parseError(method, op) {
15  return `'assert.${method}' should be used instead of '${op}'`;
16}
17
18const preferedAssertMethod = {
19  '===': 'strictEqual',
20  '!==': 'notStrictEqual',
21  '==': 'equal',
22  '!=': 'notEqual'
23};
24
25module.exports = function(context) {
26  return {
27    ExpressionStatement(node) {
28      if (isAssert(node)) {
29        const arg = getFirstArg(node.expression);
30        if (arg && arg.type === 'BinaryExpression') {
31          const assertMethod = preferedAssertMethod[arg.operator];
32          if (assertMethod) {
33            context.report(node, parseError(assertMethod, arg.operator));
34          }
35        }
36      }
37    }
38  };
39};
40