From f0ddacaddf60bcaac2e9a3f58e008106607374ff Mon Sep 17 00:00:00 2001 From: heygsc <1596920983@qq.com> Date: Thu, 8 Aug 2024 19:06:08 +0800 Subject: [PATCH 1/2] feat(linter): add fixer for eslint/no-eq-null --- .../oxc_linter/src/rules/eslint/no_eq_null.rs | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_eq_null.rs b/crates/oxc_linter/src/rules/eslint/no_eq_null.rs index 30ed9fb71ac21..b0ea700609cc7 100644 --- a/crates/oxc_linter/src/rules/eslint/no_eq_null.rs +++ b/crates/oxc_linter/src/rules/eslint/no_eq_null.rs @@ -3,7 +3,7 @@ use std::fmt::Debug; use oxc_ast::AstKind; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; -use oxc_span::Span; +use oxc_span::{GetSpan, Span}; use oxc_syntax::operator::BinaryOperator; use crate::{context::LintContext, rule::Rule, AstNode}; @@ -31,7 +31,8 @@ declare_oxc_lint!( /// } /// ``` NoEqNull, - restriction + restriction, + fix ); impl Rule for NoEqNull { @@ -49,10 +50,24 @@ impl Rule for NoEqNull { & binary_expression.left.is_null() & bad_operator { - ctx.diagnostic(no_eq_null_diagnostic(Span::new( - binary_expression.span.start, - binary_expression.span.end, - ))); + ctx.diagnostic_with_fix( + no_eq_null_diagnostic(Span::new( + binary_expression.span.start, + binary_expression.span.end, + )), + |fixer| { + let start = binary_expression.left.span().end; + let end = binary_expression.right.span().start; + let span = Span::new(start, end); + let new_operator_str = + if binary_expression.operator == BinaryOperator::Equality { + " === " + } else { + " !== " + }; + fixer.replace(span, new_operator_str) + }, + ); } } } @@ -66,5 +81,11 @@ fn test() { let fail = vec!["if (x == null) { }", "if (x != null) { }", "do {} while (null == x)"]; - Tester::new(NoEqNull::NAME, pass, fail).test_and_snapshot(); + let fix = vec![ + ("if (x == null) { }", "if (x === null) { }"), + ("if (x != null) { }", "if (x !== null) { }"), + ("do {} while (null == x)", "do {} while (null === x)"), + ]; + + Tester::new(NoEqNull::NAME, pass, fail).expect_fix(fix).test_and_snapshot(); } From cfb0088b9f7bbbb7ea7a4563ff03c96c7e784248 Mon Sep 17 00:00:00 2001 From: heygsc <1596920983@qq.com> Date: Thu, 8 Aug 2024 20:17:34 +0800 Subject: [PATCH 2/2] chore: update to dangerous --- crates/oxc_linter/src/rules/eslint/no_eq_null.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_eq_null.rs b/crates/oxc_linter/src/rules/eslint/no_eq_null.rs index b0ea700609cc7..c78e712605bc7 100644 --- a/crates/oxc_linter/src/rules/eslint/no_eq_null.rs +++ b/crates/oxc_linter/src/rules/eslint/no_eq_null.rs @@ -32,7 +32,7 @@ declare_oxc_lint!( /// ``` NoEqNull, restriction, - fix + fix_dangerous ); impl Rule for NoEqNull { @@ -50,7 +50,7 @@ impl Rule for NoEqNull { & binary_expression.left.is_null() & bad_operator { - ctx.diagnostic_with_fix( + ctx.diagnostic_with_dangerous_fix( no_eq_null_diagnostic(Span::new( binary_expression.span.start, binary_expression.span.end,