0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 03:07:54 +01:00
nodejs/tools/eslint-rules/prefer-assert-methods.js
Michaël Zasso 3c1069bb06
tools: prepare custom rules for ESLint v9
Refs: https://eslint.org/docs/latest/use/migrate-to-9.0.0
PR-URL: https://github.com/nodejs/node/pull/52889
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
2024-05-09 15:27:39 +00:00

48 lines
1.3 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 = {
meta: { fixable: 'code' },
create(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.sourceCode;
const left = sourceCode.getText(arg.left);
const right = sourceCode.getText(arg.right);
return fixer.replaceText(
node,
`assert.${assertMethod}(${left}, ${right});`,
);
},
});
}
},
};
},
};