• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @fileoverview Prohibit the use of assert operators ( ===, !==, ==, != )
3 */
4
5'use strict';
6
7const astSelector = 'ExpressionStatement[expression.type="CallExpression"]' +
8                    '[expression.callee.name="assert"]' +
9                    '[expression.arguments.0.type="BinaryExpression"]';
10
11function parseError(method, op) {
12  return `'assert.${method}' should be used instead of '${op}'`;
13}
14
15const preferedAssertMethod = {
16  '===': 'strictEqual',
17  '!==': 'notStrictEqual',
18  '==': 'equal',
19  '!=': 'notEqual'
20};
21
22module.exports = function(context) {
23  return {
24    [astSelector]: function(node) {
25      const arg = node.expression.arguments[0];
26      const assertMethod = preferedAssertMethod[arg.operator];
27      if (assertMethod) {
28        context.report({
29          node,
30          message: parseError(assertMethod, arg.operator),
31          fix: (fixer) => {
32            const sourceCode = context.getSourceCode();
33            const left = sourceCode.getText(arg.left);
34            const right = sourceCode.getText(arg.right);
35            return fixer.replaceText(
36              node,
37              `assert.${assertMethod}(${left}, ${right});`
38            );
39          }
40        });
41      }
42    }
43  };
44};
45
46module.exports.meta = {
47  fixable: 'code'
48};
49