From db554447aa26408334fe1d92523cfb4b2d6f4db5 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Sat, 31 Aug 2024 20:48:59 +0000 Subject: [PATCH] feat(linter/oxc): add fixer for `double-comparisons` (#5378) --- .../src/rules/oxc/double_comparisons.rs | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/crates/oxc_linter/src/rules/oxc/double_comparisons.rs b/crates/oxc_linter/src/rules/oxc/double_comparisons.rs index 01778536c86d1..33ed27d059089 100644 --- a/crates/oxc_linter/src/rules/oxc/double_comparisons.rs +++ b/crates/oxc_linter/src/rules/oxc/double_comparisons.rs @@ -39,6 +39,7 @@ declare_oxc_lint!( /// ``` DoubleComparisons, correctness, + fix ); #[allow(clippy::similar_names)] @@ -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) + }, + ); } } @@ -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(); }