2017-10-31 21:42:56 +01:00
|
|
|
/**
|
|
|
|
* @fileoverview Prohibit the use of assert operators ( ===, !==, ==, != )
|
|
|
|
*/
|
|
|
|
|
2016-09-17 12:32:33 +02:00
|
|
|
'use strict';
|
|
|
|
|
2017-12-09 15:54:33 +01:00
|
|
|
const astSelector = 'ExpressionStatement[expression.type="CallExpression"]' +
|
|
|
|
'[expression.callee.name="assert"]' +
|
|
|
|
'[expression.arguments.0.type="BinaryExpression"]';
|
2016-09-17 12:32:33 +02:00
|
|
|
|
|
|
|
function parseError(method, op) {
|
|
|
|
return `'assert.${method}' should be used instead of '${op}'`;
|
|
|
|
}
|
|
|
|
|
2021-02-28 02:37:51 +01:00
|
|
|
const preferredAssertMethod = {
|
2016-09-17 12:32:33 +02:00
|
|
|
'===': 'strictEqual',
|
|
|
|
'!==': 'notStrictEqual',
|
|
|
|
'==': 'equal',
|
2022-12-18 17:39:39 +01:00
|
|
|
'!=': 'notEqual',
|
2016-09-17 12:32:33 +02:00
|
|
|
};
|
|
|
|
|
2023-02-03 10:55:29 +01:00
|
|
|
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) => {
|
2024-05-09 17:27:39 +02:00
|
|
|
const sourceCode = context.sourceCode;
|
2023-02-03 10:55:29 +01:00
|
|
|
const left = sourceCode.getText(arg.left);
|
|
|
|
const right = sourceCode.getText(arg.right);
|
|
|
|
return fixer.replaceText(
|
|
|
|
node,
|
|
|
|
`assert.${assertMethod}(${left}, ${right});`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2020-08-01 20:22:51 +02:00
|
|
|
};
|