Skip to content

Commit

Permalink
feat(linter/oxc): add fixer for erasing-op (#5377)
Browse files Browse the repository at this point in the history
adds a fixer for `erasing-op` (replace the binary expression with `0`)
  • Loading branch information
camc314 committed Sep 3, 2024
1 parent 2ccbd93 commit b846432
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions crates/oxc_linter/src/rules/oxc/erasing_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ declare_oxc_lint!(
/// let y = 0;
/// ```
ErasingOp,
correctness
correctness,
suggestion
);

impl Rule for ErasingOp {
Expand Down Expand Up @@ -84,7 +85,9 @@ fn check_op<'a, 'b>(
ctx: &LintContext<'a>,
) {
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")
});
}
}

Expand All @@ -96,5 +99,7 @@ fn test() {

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();
}

0 comments on commit b846432

Please sign in to comment.