Skip to content

Commit

Permalink
enhance rule
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama committed Dec 27, 2024
1 parent b113e8a commit 106875a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
42 changes: 38 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_nested_ternary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ declare_oxc_lint!(
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(_))
|| matches!(node.consequent, Expression::ConditionalExpression(_))
{
let check_nested_ternary = |expr: &Expression| {
if matches!(expr, Expression::ConditionalExpression(_)) {
true
} else {
let inner_expr = expr.get_inner_expression();
matches!(inner_expr, Expression::ConditionalExpression(_))
}
};

if check_nested_ternary(&node.consequent) || check_nested_ternary(&node.alternate) {
ctx.diagnostic(no_nested_ternary_diagnostic(node.span));
}
}
Expand All @@ -58,11 +65,38 @@ impl Rule for NoNestedTernary {
fn test() {
use crate::tester::Tester;

let pass = vec!["foo ? doBar() : doBaz();", "var foo = bar === baz ? qux : quxx;"];
let pass = vec![
"foo ? doBar() : doBaz();",
"var foo = bar === baz ? qux : quxx;",
"var result = foo && bar ? baz : qux || quux;",
"var result = foo ? bar : baz === qux;",
"foo ? doSomething(a, b) : doSomethingElse(c, d);",
// Parenthesized Expressions
"var result = (foo ? bar : baz) || qux;",
"var result = (foo ? bar : baz) && qux;",
"var result = foo === bar ? (baz || qux) : quux;",
"var result = (foo ? bar : baz) ? qux : quux;",
// TypeScript
"var result = foo! ? bar : baz;",
"var result = foo ? bar! : baz;",
"var result = (foo as boolean) ? bar : baz;",
"var result = foo ? (bar as string) : baz;",
];

let fail = vec![
"foo ? bar : baz === qux ? quxx : foobar;",
"foo ? baz === qux ? quxx : foobar : bar;",
// Parenthesized Expressions
"var result = foo ? (bar ? baz : qux) : quux;",
"var result = foo ? (bar === baz ? qux : quux) : foobar;",
"doSomething(foo ? bar : baz ? qux : quux);",
// Comment
"var result = foo /* comment */ ? bar : baz ? qux : quux;",
// TypeScript
"var result = foo! ? bar : baz! ? qux : quux;",
"var result = foo ? bar! : (baz! ? qux : quux);",
"var result = (foo as boolean) ? bar : (baz as string) ? qux : quux;",
"var result = foo ? (bar as string) : (baz as number ? qux : quux);",
];

Tester::new(NoNestedTernary::NAME, NoNestedTernary::CATEGORY, pass, fail).test_and_snapshot();
Expand Down
48 changes: 48 additions & 0 deletions crates/oxc_linter/src/snapshots/eslint_no_nested_ternary.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,51 @@ snapshot_kind: text
1foo ? baz === qux ? quxx : foobar : bar;
· ───────────────────────────────────────
╰────

eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:14]
1var result = foo ? (bar ? baz : qux) : quux;
· ──────────────────────────────
╰────

eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:14]
1var result = foo ? (bar === baz ? qux : quux) : foobar;
· ─────────────────────────────────────────
╰────

eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:13]
1doSomething(foo ? bar : baz ? qux : quux);
· ────────────────────────────
╰────

eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:14]
1var result = foo /* comment */ ? bar : baz ? qux : quux;
· ──────────────────────────────────────────
╰────

eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:14]
1var result = foo! ? bar : baz! ? qux : quux;
· ──────────────────────────────
╰────

eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:14]
1var result = foo ? bar! : (baz! ? qux : quux);
· ────────────────────────────────
╰────

eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:14]
1var result = (foo as boolean) ? bar : (baz as string) ? qux : quux;
· ─────────────────────────────────────────────────────
╰────

eslint(no-nested-ternary): Do not nest ternary expressions.
╭─[no_nested_ternary.tsx:1:14]
1var result = foo ? (bar as string) : (baz as number ? qux : quux);
· ────────────────────────────────────────────────────
╰────

0 comments on commit 106875a

Please sign in to comment.