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
Shobhit Chittora 2d6912a46b
tools: auto fix custom eslint rule
1. Extends tests
2. Refactors code
3. Adds fixer

Refs: #16636

PR-URL: https://github.com/nodejs/node/pull/16652
Refs: https://github.com/nodejs/node/issues/16636
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2018-02-01 11:40:53 +01:00

45 lines
1.2 KiB
JavaScript

/**
* @fileoverview Prohibit the use of assert operators ( ===, !==, ==, != )
*/
'use strict';
const astSelector = 'ExpressionStatement[expression.type="CallExpression"]' +
'[expression.callee.name="assert"]' +
'[expression.arguments.0.type="BinaryExpression"]';
function parseError(method, op) {
return `'assert.${method}' should be used instead of '${op}'`;
}
const preferedAssertMethod = {
'===': 'strictEqual',
'!==': 'notStrictEqual',
'==': 'equal',
'!=': 'notEqual'
};
module.exports = function(context) {
return {
[astSelector]: function(node) {
const arg = node.expression.arguments[0];
const assertMethod = preferedAssertMethod[arg.operator];
if (assertMethod) {
context.report({
node,
message: parseError(assertMethod, arg.operator),
fix: (fixer) => {
const sourceCode = context.getSourceCode();
const left = sourceCode.getText(arg.left);
const right = sourceCode.getText(arg.right);
return fixer.replaceText(
node,
`assert.${assertMethod}(${left}, ${right});`
);
}
});
}
}
};
};