@@ -58,7 +58,7 @@ declare_oxc_lint!(
5858
5959impl 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