Skip to content

Commit c49d891

Browse files
committed
perf(linter): use match for no_negated_condition (#14522)
1 parent e222fc2 commit c49d891

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

crates/oxc_linter/src/generated/rule_runner_impls.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ impl RuleRunner for crate::rules::eslint::no_multi_str::NoMultiStr {
394394
}
395395

396396
impl RuleRunner for crate::rules::eslint::no_negated_condition::NoNegatedCondition {
397-
const NODE_TYPES: Option<&AstTypesBitset> = None;
397+
const NODE_TYPES: Option<&AstTypesBitset> =
398+
Some(&AstTypesBitset::from_types(&[AstType::ConditionalExpression, AstType::IfStatement]));
398399
}
399400

400401
impl RuleRunner for crate::rules::eslint::no_nested_ternary::NoNestedTernary {

crates/oxc_linter/src/rules/eslint/no_negated_condition.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ declare_oxc_lint!(
5858

5959
impl Rule for NoNegatedCondition {
6060
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
61-
let stmt_test = match node.kind() {
61+
match node.kind() {
6262
AstKind::IfStatement(if_stmt) => {
6363
let Some(if_stmt_alternate) = &if_stmt.alternate else {
6464
return;
@@ -68,32 +68,30 @@ impl Rule for NoNegatedCondition {
6868
return;
6969
}
7070

71-
if_stmt.test.without_parentheses()
72-
}
73-
AstKind::ConditionalExpression(conditional_expr) => {
74-
conditional_expr.test.without_parentheses()
75-
}
76-
_ => return,
77-
};
78-
79-
match stmt_test {
80-
Expression::UnaryExpression(unary_expr) => {
81-
if unary_expr.operator != UnaryOperator::LogicalNot {
82-
return;
71+
let test = if_stmt.test.without_parentheses();
72+
if is_negated_expression(test) {
73+
ctx.diagnostic(no_negated_condition_diagnostic(test.span()));
8374
}
8475
}
85-
Expression::BinaryExpression(binary_expr) => {
86-
if !matches!(
87-
binary_expr.operator,
88-
BinaryOperator::Inequality | BinaryOperator::StrictInequality
89-
) {
90-
return;
76+
AstKind::ConditionalExpression(conditional_expr) => {
77+
let test = conditional_expr.test.without_parentheses();
78+
if is_negated_expression(test) {
79+
ctx.diagnostic(no_negated_condition_diagnostic(test.span()));
9180
}
9281
}
93-
_ => return,
82+
_ => {}
9483
}
84+
}
85+
}
9586

96-
ctx.diagnostic(no_negated_condition_diagnostic(stmt_test.span()));
87+
fn is_negated_expression(expr: &Expression) -> bool {
88+
match expr {
89+
Expression::UnaryExpression(unary_expr) => unary_expr.operator == UnaryOperator::LogicalNot,
90+
Expression::BinaryExpression(binary_expr) => matches!(
91+
binary_expr.operator,
92+
BinaryOperator::Inequality | BinaryOperator::StrictInequality
93+
),
94+
_ => false,
9795
}
9896
}
9997

0 commit comments

Comments
 (0)