From c49d891b1f4f96b77ae1ae3f6bb2ca32f539ba8b Mon Sep 17 00:00:00 2001 From: camchenry <1514176+camchenry@users.noreply.github.com> Date: Sun, 12 Oct 2025 10:10:28 +0000 Subject: [PATCH] perf(linter): use match for `no_negated_condition` (#14522) --- .../src/generated/rule_runner_impls.rs | 3 +- .../src/rules/eslint/no_negated_condition.rs | 40 +++++++++---------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/crates/oxc_linter/src/generated/rule_runner_impls.rs b/crates/oxc_linter/src/generated/rule_runner_impls.rs index d65aa8099fcb0..f5c04b9c92ab1 100644 --- a/crates/oxc_linter/src/generated/rule_runner_impls.rs +++ b/crates/oxc_linter/src/generated/rule_runner_impls.rs @@ -394,7 +394,8 @@ impl RuleRunner for crate::rules::eslint::no_multi_str::NoMultiStr { } impl RuleRunner for crate::rules::eslint::no_negated_condition::NoNegatedCondition { - const NODE_TYPES: Option<&AstTypesBitset> = None; + const NODE_TYPES: Option<&AstTypesBitset> = + Some(&AstTypesBitset::from_types(&[AstType::ConditionalExpression, AstType::IfStatement])); } impl RuleRunner for crate::rules::eslint::no_nested_ternary::NoNestedTernary { diff --git a/crates/oxc_linter/src/rules/eslint/no_negated_condition.rs b/crates/oxc_linter/src/rules/eslint/no_negated_condition.rs index f0426af99e9a3..b5296ebb5261a 100644 --- a/crates/oxc_linter/src/rules/eslint/no_negated_condition.rs +++ b/crates/oxc_linter/src/rules/eslint/no_negated_condition.rs @@ -58,7 +58,7 @@ declare_oxc_lint!( impl Rule for NoNegatedCondition { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { - let stmt_test = match node.kind() { + match node.kind() { AstKind::IfStatement(if_stmt) => { let Some(if_stmt_alternate) = &if_stmt.alternate else { return; @@ -68,32 +68,30 @@ impl Rule for NoNegatedCondition { return; } - if_stmt.test.without_parentheses() - } - AstKind::ConditionalExpression(conditional_expr) => { - conditional_expr.test.without_parentheses() - } - _ => return, - }; - - match stmt_test { - Expression::UnaryExpression(unary_expr) => { - if unary_expr.operator != UnaryOperator::LogicalNot { - return; + let test = if_stmt.test.without_parentheses(); + if is_negated_expression(test) { + ctx.diagnostic(no_negated_condition_diagnostic(test.span())); } } - Expression::BinaryExpression(binary_expr) => { - if !matches!( - binary_expr.operator, - BinaryOperator::Inequality | BinaryOperator::StrictInequality - ) { - return; + AstKind::ConditionalExpression(conditional_expr) => { + let test = conditional_expr.test.without_parentheses(); + if is_negated_expression(test) { + ctx.diagnostic(no_negated_condition_diagnostic(test.span())); } } - _ => return, + _ => {} } + } +} - ctx.diagnostic(no_negated_condition_diagnostic(stmt_test.span())); +fn is_negated_expression(expr: &Expression) -> bool { + match expr { + Expression::UnaryExpression(unary_expr) => unary_expr.operator == UnaryOperator::LogicalNot, + Expression::BinaryExpression(binary_expr) => matches!( + binary_expr.operator, + BinaryOperator::Inequality | BinaryOperator::StrictInequality + ), + _ => false, } }