node/tools/eslint-rules/prefer-assert-methods.js
Tobias Nießen 54bb7e3bff
tools: fix object name in prefer-assert-methods.js
PR-URL: https://github.com/nodejs/node/pull/37544
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2021-03-05 10:17:34 -08:00

49 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 preferredAssertMethod = {
'===': 'strictEqual',
'!==': 'notStrictEqual',
'==': 'equal',
'!=': 'notEqual'
};
module.exports = function(context) {
return {
[astSelector]: function(node) {
const arg = node.expression.arguments[0];
const assertMethod = preferredAssertMethod[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});`
);
}
});
}
}
};
};
module.exports.meta = {
fixable: 'code'
};