Skip to content

Commit

Permalink
feat(linter/oxc): add fixer for double-comparisons (#5378)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Aug 31, 2024
1 parent ed31d67 commit db55444
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions crates/oxc_linter/src/rules/oxc/double_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ declare_oxc_lint!(
/// ```
DoubleComparisons,
correctness,
fix
);

#[allow(clippy::similar_names)]
Expand Down Expand Up @@ -84,7 +85,22 @@ impl Rule for DoubleComparisons {
_ => return,
};

ctx.diagnostic(double_comparisons_diagnostic(logical_expr.span, new_op));
ctx.diagnostic_with_fix(
double_comparisons_diagnostic(logical_expr.span, new_op),
|fixer| {
let modified_code = {
let mut codegen = fixer.codegen();
codegen.print_expression(llhs);
codegen.print_char(b' ');
codegen.print_str(new_op);
codegen.print_char(b' ');
codegen.print_expression(lrhs);
codegen.into_source_text()
};

fixer.replace(logical_expr.span, modified_code)
},
);
}
}

Expand Down Expand Up @@ -127,5 +143,20 @@ fn test() {
"x > y || x === y",
];

Tester::new(DoubleComparisons::NAME, pass, fail).test_and_snapshot();
let fix = vec![
("x == y || x < y", "x <= y"),
("x < y || x == y", "x <= y"),
("x == y || x > y", "x >= y"),
("x > y || x == y", "x >= y"),
("x < y || x > y", "x != y"),
("x > y || x < y", "x != y"),
("x <= y && x >= y", "x == y"),
("x >= y && x <= y", "x == y"),
("x === y || x < y", "x <= y"),
("x < y || x === y", "x <= y"),
("x === y || x > y", "x >= y"),
("x > y || x === y", "x >= y"),
];

Tester::new(DoubleComparisons::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}

0 comments on commit db55444

Please sign in to comment.