diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index 8c6fb137bc36c..e7560638456bf 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -70,6 +70,7 @@ mod eslint { pub mod no_empty_character_class; pub mod no_empty_pattern; pub mod no_empty_static_block; + pub mod no_eq_null; pub mod no_eval; pub mod no_ex_assign; pub mod no_extra_boolean_cast; @@ -394,6 +395,7 @@ oxc_macros::declare_all_lint_rules! { eslint::no_eval, eslint::no_ex_assign, eslint::no_extra_boolean_cast, + eslint::no_eq_null, eslint::no_fallthrough, eslint::no_func_assign, eslint::no_global_assign, diff --git a/crates/oxc_linter/src/rules/eslint/no_eq_null.rs b/crates/oxc_linter/src/rules/eslint/no_eq_null.rs new file mode 100644 index 0000000000000..618cae05841d2 --- /dev/null +++ b/crates/oxc_linter/src/rules/eslint/no_eq_null.rs @@ -0,0 +1,74 @@ +use oxc_ast::AstKind; +use oxc_diagnostics::{ + miette::{self, Diagnostic}, + thiserror::Error, +}; +use oxc_macros::declare_oxc_lint; +use oxc_span::Span; +use oxc_syntax::operator::BinaryOperator; +use std::fmt::Debug; + +use crate::{context::LintContext, rule::Rule, AstNode}; + +#[derive(Debug, Error, Diagnostic)] +#[error("eslint(no-eq-null): Use '===' to compare with null")] +#[diagnostic( + severity(warning), + help("Disallow `null` comparisons without type-checking operators.") +)] +struct NoEqNullDiagnostic(#[label] pub Span); + +#[derive(Debug, Default, Clone)] +pub struct NoEqNull; + +declare_oxc_lint!( + /// ### What it does + /// Disallow null comparisons without type-checking operators. + /// + /// ### Why is this bad? + /// Comparing to null without a type-checking operator (== or !=), can have unintended results as the comparison will evaluate to true when comparing to not just a null, but also an undefined value. + /// + /// ### Example + /// ```javascript + /// if (foo == null) { + /// bar(); + /// } + /// ``` + NoEqNull, + correctness +); + +impl Rule for NoEqNull { + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { + if let AstKind::BinaryExpression(binary_expression) = node.kind() { + let bad_operator = matches!( + binary_expression.operator, + BinaryOperator::Equality | BinaryOperator::Inequality + ); + + if binary_expression.right.is_literal() + & binary_expression.right.is_null() + & bad_operator + | binary_expression.left.is_literal() + & binary_expression.left.is_null() + & bad_operator + { + ctx.diagnostic(NoEqNullDiagnostic(Span::new( + binary_expression.span.start, + binary_expression.span.end, + ))); + } + } + } +} + +#[test] +fn test() { + use crate::tester::Tester; + + let pass = vec!["if (x === null) { }", "if (null === f()) { }"]; + + let fail = vec!["if (x == null) { }", "if (x != null) { }", "do {} while (null == x)"]; + + Tester::new(NoEqNull::NAME, pass, fail).test_and_snapshot(); +} diff --git a/crates/oxc_linter/src/snapshots/no_eq_null.snap b/crates/oxc_linter/src/snapshots/no_eq_null.snap new file mode 100644 index 0000000000000..91e0a07927c7f --- /dev/null +++ b/crates/oxc_linter/src/snapshots/no_eq_null.snap @@ -0,0 +1,24 @@ +--- +source: crates/oxc_linter/src/tester.rs +expression: no_eq_null +--- + ⚠ eslint(no-eq-null): Use '===' to compare with null + ╭─[no_eq_null.tsx:1:5] + 1 │ if (x == null) { } + · ───────── + ╰──── + help: Disallow `null` comparisons without type-checking operators. + + ⚠ eslint(no-eq-null): Use '===' to compare with null + ╭─[no_eq_null.tsx:1:5] + 1 │ if (x != null) { } + · ───────── + ╰──── + help: Disallow `null` comparisons without type-checking operators. + + ⚠ eslint(no-eq-null): Use '===' to compare with null + ╭─[no_eq_null.tsx:1:14] + 1 │ do {} while (null == x) + · ───────── + ╰──── + help: Disallow `null` comparisons without type-checking operators.