-
-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): implement
eslint/no-nested-ternary
(#8150)
implement: https://eslint.org/docs/latest/rules/no-nested-ternary --------- Co-authored-by: Cameron Clark <[email protected]>
- Loading branch information
1 parent
1c5db72
commit 5234d96
Showing
3 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
crates/oxc_linter/src/rules/eslint/no_nested_ternary.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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.consequent.get_inner_expression(), | ||
Expression::ConditionalExpression(_) | ||
) || matches!( | ||
node.alternate.get_inner_expression(), | ||
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;", | ||
"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(); | ||
} |
63 changes: 63 additions & 0 deletions
63
crates/oxc_linter/src/snapshots/eslint_no_nested_ternary.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
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; | ||
· ─────────────────────────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-nested-ternary): Do not nest ternary expressions. | ||
╭─[no_nested_ternary.tsx:1:14] | ||
1 │ var result = foo ? (bar ? baz : qux) : quux; | ||
· ────────────────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-nested-ternary): Do not nest ternary expressions. | ||
╭─[no_nested_ternary.tsx:1:14] | ||
1 │ var result = foo ? (bar === baz ? qux : quux) : foobar; | ||
· ───────────────────────────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-nested-ternary): Do not nest ternary expressions. | ||
╭─[no_nested_ternary.tsx:1:13] | ||
1 │ doSomething(foo ? bar : baz ? qux : quux); | ||
· ──────────────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-nested-ternary): Do not nest ternary expressions. | ||
╭─[no_nested_ternary.tsx:1:14] | ||
1 │ var result = foo /* comment */ ? bar : baz ? qux : quux; | ||
· ────────────────────────────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-nested-ternary): Do not nest ternary expressions. | ||
╭─[no_nested_ternary.tsx:1:14] | ||
1 │ var result = foo! ? bar : baz! ? qux : quux; | ||
· ────────────────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-nested-ternary): Do not nest ternary expressions. | ||
╭─[no_nested_ternary.tsx:1:14] | ||
1 │ var result = foo ? bar! : (baz! ? qux : quux); | ||
· ──────────────────────────────── | ||
╰──── | ||
|
||
⚠ eslint(no-nested-ternary): Do not nest ternary expressions. | ||
╭─[no_nested_ternary.tsx:1:14] | ||
1 │ var 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] | ||
1 │ var result = foo ? (bar as string) : (baz as number ? qux : quux); | ||
· ──────────────────────────────────────────────────── | ||
╰──── |