Skip to content

Commit

Permalink
merge eslint-plugin-unicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama committed Dec 27, 2024
1 parent 155d9ea commit 0c57506
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 54 deletions.
47 changes: 2 additions & 45 deletions crates/oxc_linter/src/rules/eslint/no_negated_condition.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{context::LintContext, rule::Rule, AstNode};
use oxc_ast::ast::{ConditionalExpression, Expression, IfStatement, Statement};
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};

fn no_negated_condition_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected negated condition.").with_label(span)
Expand Down Expand Up @@ -49,51 +46,11 @@ declare_oxc_lint!(

impl Rule for NoNegatedCondition {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
AstKind::IfStatement(if_stmt) => {
if !has_else_without_condition(if_stmt) {
return;
}

if is_negated_if(if_stmt) {
ctx.diagnostic(no_negated_condition_diagnostic(if_stmt.span));
}
}
AstKind::ConditionalExpression(conditional_expr) => {
if is_negated_if_conditional(conditional_expr) {
ctx.diagnostic(no_negated_condition_diagnostic(conditional_expr.span));
}
}
_ => {}
}
// This rule is exactly the same as the eslint-plugin-unicorn's no-negated-condition rule.
crate::rules::unicorn::no_negated_condition::NoNegatedCondition::default().run(node, ctx);
}
}

fn has_else_without_condition(node: &IfStatement) -> bool {
matches!(node.alternate, Some(Statement::BlockStatement(_)))
}

fn is_negated_unary_expression(test: &Expression) -> bool {
matches!(test, Expression::UnaryExpression(unary) if unary.operator == UnaryOperator::LogicalNot)
}

fn is_negated_binary_expression(test: &Expression) -> bool {
matches!(
test,
Expression::BinaryExpression(binary)
if binary.operator == BinaryOperator::Inequality
|| binary.operator == BinaryOperator::StrictInequality
)
}

fn is_negated_if(node: &IfStatement) -> bool {
is_negated_unary_expression(&node.test) || is_negated_binary_expression(&node.test)
}

fn is_negated_if_conditional(node: &ConditionalExpression) -> bool {
is_negated_unary_expression(&node.test) || is_negated_binary_expression(&node.test)
}

#[test]
fn test() {
use crate::tester::Tester;
Expand Down
21 changes: 21 additions & 0 deletions crates/oxc_linter/src/rules/unicorn/no_negated_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ fn test() {
r"if (a !== b) {}",
r"if (a === b) {} else {}",
r"a ? b : c",
// Test cases from ESLint
"if (a) {}",
"if (a) {} else {}",
"if (!a) {}",
"if (!a) {} else if (b) {}",
"if (!a) {} else if (b) {} else {}",
"if (a == b) {}",
"if (a == b) {} else {}",
"if (a != b) {}",
"if (a != b) {} else if (b) {}",
"if (a != b) {} else if (b) {} else {}",
"if (a !== b) {}",
"if (a === b) {} else {}",
"a ? b : c",
];

let fail = vec![
Expand All @@ -140,6 +154,13 @@ fn test() {
r"if(!a) {b()} else {c()}",
r"if(!!a) b(); else c();",
r"(!!a) ? b() : c();",
// Test cases from ESLint
"if (!a) {;} else {;}",
"if (a != b) {;} else {;}",
"if (a !== b) {;} else {;}",
"!a ? b : c",
"a != b ? c : d",
"a !== b ? c : d",
];

Tester::new(NoNegatedCondition::NAME, NoNegatedCondition::CATEGORY, pass, fail)
Expand Down
24 changes: 15 additions & 9 deletions crates/oxc_linter/src/snapshots/eslint_no_negated_condition.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,43 @@ source: crates/oxc_linter/src/tester.rs
snapshot_kind: text
---
eslint(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:1]
╭─[no_negated_condition.tsx:1:5]
1if (!a) {;} else {;}
· ────────────────────
· ──
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:1]
╭─[no_negated_condition.tsx:1:5]
1if (a != b) {;} else {;}
· ────────────────────────
· ──────
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:1]
╭─[no_negated_condition.tsx:1:5]
1if (a !== b) {;} else {;}
· ─────────────────────────
· ───────
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:1]
1!a ? b : c
· ──────────
· ──
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:1]
1a != b ? c : d
· ──────────────
· ──────
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:1]
1a !== b ? c : d
· ───────────────
· ───────
╰────
help: Remove the negation operator and switch the consequent and alternate branches.
42 changes: 42 additions & 0 deletions crates/oxc_linter/src/snapshots/unicorn_no_negated_condition.snap
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,45 @@ snapshot_kind: text
· ───
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint-plugin-unicorn(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:5]
1if (!a) {;} else {;}
· ──
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint-plugin-unicorn(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:5]
1if (a != b) {;} else {;}
· ──────
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint-plugin-unicorn(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:5]
1if (a !== b) {;} else {;}
· ───────
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint-plugin-unicorn(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:1]
1!a ? b : c
· ──
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint-plugin-unicorn(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:1]
1a != b ? c : d
· ──────
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

eslint-plugin-unicorn(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:1]
1a !== b ? c : d
· ───────
╰────
help: Remove the negation operator and switch the consequent and alternate branches.

0 comments on commit 0c57506

Please sign in to comment.