0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/tools/eslint-rules/prefer-assert-methods.js
Dany Shaanan d469321946 tools: add eslint rule prefer-assert-methods
PR-URL: https://github.com/nodejs/node/pull/8622
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Jackson Tian <shvyo1987@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
2016-09-20 16:35:39 -07:00

40 lines
944 B
JavaScript

'use strict';
function isAssert(node) {
return node.expression &&
node.expression.type === 'CallExpression' &&
node.expression.callee &&
node.expression.callee.name === 'assert';
}
function getFirstArg(expression) {
return expression.arguments && expression.arguments[0];
}
function parseError(method, op) {
return `'assert.${method}' should be used instead of '${op}'`;
}
const preferedAssertMethod = {
'===': 'strictEqual',
'!==': 'notStrictEqual',
'==': 'equal',
'!=': 'notEqual'
};
module.exports = function(context) {
return {
ExpressionStatement(node) {
if (isAssert(node)) {
const arg = getFirstArg(node.expression);
if (arg && arg.type === 'BinaryExpression') {
const assertMethod = preferedAssertMethod[arg.operator];
if (assertMethod) {
context.report(node, parseError(assertMethod, arg.operator));
}
}
}
}
};
};