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-nested-ternary rule #8150

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ mod eslint {
pub mod no_loss_of_precision;
pub mod no_magic_numbers;
pub mod no_multi_str;
pub mod no_nested_ternary;
pub mod no_new;
pub mod no_new_func;
pub mod no_new_native_nonconstructor;
Expand Down Expand Up @@ -535,6 +536,7 @@ oxc_macros::declare_all_lint_rules! {
eslint::max_classes_per_file,
eslint::max_lines,
eslint::max_params,
eslint::no_nested_ternary,
eslint::no_labels,
eslint::no_restricted_imports,
eslint::no_object_constructor,
Expand Down
69 changes: 69 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_nested_ternary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use oxc_ast::ast::Expression;
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule, AstNode};

fn no_nested_ternary_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not nest ternary expressions.").with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoNestedTernary;

declare_oxc_lint!(
/// ### What it does
///
/// Disallows nested ternary expressions to improve code readability and maintainability.
///
/// ### Why is this bad?
///
/// Nested ternary expressions make code harder to read and understand. They can lead to complex, difficult-to-debug logic.
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```js
/// const result = condition1 ? (condition2 ? "a" : "b") : "c";
/// ```
///
/// Examples of **correct** code for this rule:
/// ```js
/// let result;
/// if (condition1) {
/// result = condition2 ? "a" : "b";
/// } else {
/// result = "c";
/// }
/// ```
NoNestedTernary,
style,
);

impl Rule for NoNestedTernary {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::ConditionalExpression(node) = node.kind() {
if matches!(node.alternate, Expression::ConditionalExpression(_))
camc314 marked this conversation as resolved.
Show resolved Hide resolved
|| matches!(node.consequent, Expression::ConditionalExpression(_))
{
ctx.diagnostic(no_nested_ternary_diagnostic(node.span));
}
}
}
}

#[test]
fn test() {
use crate::tester::Tester;

let pass = vec!["foo ? doBar() : doBaz();", "var foo = bar === baz ? qux : quxx;"];

let fail = vec![
"foo ? bar : baz === qux ? quxx : foobar;",
"foo ? baz === qux ? quxx : foobar : bar;",
];

Tester::new(NoNestedTernary::NAME, NoNestedTernary::CATEGORY, pass, fail).test_and_snapshot();
}
15 changes: 15 additions & 0 deletions crates/oxc_linter/src/snapshots/eslint_no_nested_ternary.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/oxc_linter/src/tester.rs
snapshot_kind: text
---
⚠ eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:1]
1 │ foo ? bar : baz === qux ? quxx : foobar;
· ───────────────────────────────────────
╰────

⚠ eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:1]
1 │ foo ? baz === qux ? quxx : foobar : bar;
· ───────────────────────────────────────
╰────
Loading