Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter): implement no-negated-condition rule #8149

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/config/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ fn transform_rule_and_plugin_name<'a>(
return (rule_name, "eslint");
}

if plugin_name == "unicorn" && rule_name == "no-negated-condition" {
return ("no-negated-condition", "eslint");
}

(rule_name, plugin_name)
}

Expand Down
21 changes: 21 additions & 0 deletions crates/oxc_linter/src/rules/unicorn/no_negated_condition.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file needs to be moved into rules/eslint

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
45 changes: 45 additions & 0 deletions crates/oxc_linter/src/snapshots/eslint_no_negated_condition.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: crates/oxc_linter/src/tester.rs
snapshot_kind: text
---
⚠ eslint(no-negated-condition): Unexpected negated condition.
╭─[no_negated_condition.tsx:1:5]
1 │ if (!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:5]
1 │ if (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:5]
1 │ if (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]
1 │ a != 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]
1 │ a !== 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one should be deleted

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]
1 │ if (!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]
1 │ if (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]
1 │ if (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]
1 │ a != 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]
1 │ a !== b ? c : d
· ───────
╰────
help: Remove the negation operator and switch the consequent and alternate branches.
Loading