feat(linter/oxc): add fixer for erasing-op (#5377)

adds a fixer for `erasing-op` (replace the binary expression with `0`)
This commit is contained in:
camc314 2024-09-03 00:46:54 +00:00
parent 2ccbd9361b
commit b8464325e7

View file

@ -45,7 +45,8 @@ declare_oxc_lint!(
/// let y = 0; /// let y = 0;
/// ``` /// ```
ErasingOp, ErasingOp,
correctness correctness,
suggestion
); );
impl Rule for ErasingOp { impl Rule for ErasingOp {
@ -84,7 +85,9 @@ fn check_op<'a, 'b>(
ctx: &LintContext<'a>, ctx: &LintContext<'a>,
) { ) {
if is_number_value(op, 0.0) { if is_number_value(op, 0.0) {
ctx.diagnostic(erasing_op_diagnostic(binary_expression.span)); ctx.diagnostic_with_suggestion(erasing_op_diagnostic(binary_expression.span), |fixer| {
fixer.replace(binary_expression.span, "0")
});
} }
} }
@ -96,5 +99,7 @@ fn test() {
let fail = vec!["x * 0;", "0 * x;", "0 & x;", "0 / x;"]; let fail = vec!["x * 0;", "0 * x;", "0 & x;", "0 / x;"];
Tester::new(ErasingOp::NAME, pass, fail).test_and_snapshot(); let fix = vec![("x * 0;", "0;"), ("0 * x;", "0;"), ("0 & x;", "0;"), ("0 / x;", "0;")];
Tester::new(ErasingOp::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
} }