-
-
Notifications
You must be signed in to change notification settings - Fork 457
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 no-nested-ternary (#4067)
- Loading branch information
Showing
12 changed files
with
264 additions
and
50 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
119 changes: 69 additions & 50 deletions
119
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
91 changes: 91 additions & 0 deletions
91
crates/biome_js_analyze/src/lint/nursery/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,91 @@ | ||
use biome_analyze::{ | ||
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{AnyJsExpression, JsConditionalExpression}; | ||
use biome_rowan::{AstNode, TextRange}; | ||
|
||
declare_lint_rule! { | ||
/// Disallow nested ternary expressions. | ||
/// | ||
/// Nesting ternary expressions can make code more difficult to understand. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// const thing = foo ? bar : baz === qux ? quxx : foobar; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// foo ? baz === qux ? quxx() : foobar() : bar(); | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// const thing = foo ? bar : foobar; | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// let thing; | ||
/// | ||
/// if (foo) { | ||
/// thing = bar; | ||
/// } else if (baz === qux) { | ||
/// thing = quxx; | ||
/// } else { | ||
/// thing = foobar; | ||
/// } | ||
/// ``` | ||
/// | ||
pub NoNestedTernary { | ||
version: "next", | ||
name: "noNestedTernary", | ||
language: "js", | ||
recommended: false, | ||
sources: &[RuleSource::Eslint("no-nested-ternary")], | ||
} | ||
} | ||
|
||
impl Rule for NoNestedTernary { | ||
type Query = Ast<JsConditionalExpression>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let alternate = node.alternate().ok()?; | ||
let consequent = node.consequent().ok()?; | ||
|
||
if let AnyJsExpression::JsConditionalExpression(expr) = consequent { | ||
return Some(expr.range()); | ||
} | ||
|
||
if let AnyJsExpression::JsConditionalExpression(expr) = alternate { | ||
return Some(expr.range()); | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
state, | ||
markup! { | ||
"Do not nest ternary expressions." | ||
}, | ||
) | ||
.note(markup! { | ||
"Nesting ternary expressions can make code more difficult to understand." | ||
}) | ||
.note(markup! { | ||
"Convert nested ternary expression into if-else statements or separate the conditions to make the logic easier to understand." | ||
}), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
crates/biome_js_analyze/tests/specs/nursery/noNestedTernary/invalid.js
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,3 @@ | ||
var thing = foo ? bar : baz === qux ? quxx : foobar; | ||
|
||
foo ? baz === qux ? quxx() : foobar() : bar(); |
45 changes: 45 additions & 0 deletions
45
crates/biome_js_analyze/tests/specs/nursery/noNestedTernary/invalid.js.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,45 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
var thing = foo ? bar : baz === qux ? quxx : foobar; | ||
|
||
foo ? baz === qux ? quxx() : foobar() : bar(); | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:25 lint/nursery/noNestedTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Do not nest ternary expressions. | ||
> 1 │ var thing = foo ? bar : baz === qux ? quxx : foobar; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
2 │ | ||
3 │ foo ? baz === qux ? quxx() : foobar() : bar(); | ||
i Nesting ternary expressions can make code more difficult to understand. | ||
i Convert nested ternary expression into if-else statements or separate the conditions to make the logic easier to understand. | ||
``` | ||
|
||
``` | ||
invalid.js:3:7 lint/nursery/noNestedTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Do not nest ternary expressions. | ||
1 │ var thing = foo ? bar : baz === qux ? quxx : foobar; | ||
2 │ | ||
> 3 │ foo ? baz === qux ? quxx() : foobar() : bar(); | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
i Nesting ternary expressions can make code more difficult to understand. | ||
i Convert nested ternary expression into if-else statements or separate the conditions to make the logic easier to understand. | ||
``` |
12 changes: 12 additions & 0 deletions
12
crates/biome_js_analyze/tests/specs/nursery/noNestedTernary/valid.js
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,12 @@ | ||
/* should not generate diagnostics */ | ||
const thing = foo ? bar : foobar; | ||
|
||
let thing; | ||
|
||
if (foo) { | ||
thing = bar; | ||
} else if (baz === qux) { | ||
thing = quxx; | ||
} else { | ||
thing = foobar; | ||
} |
19 changes: 19 additions & 0 deletions
19
crates/biome_js_analyze/tests/specs/nursery/noNestedTernary/valid.js.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,19 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```jsx | ||
/* should not generate diagnostics */ | ||
const thing = foo ? bar : foobar; | ||
|
||
let thing; | ||
|
||
if (foo) { | ||
thing = bar; | ||
} else if (baz === qux) { | ||
thing = quxx; | ||
} else { | ||
thing = foobar; | ||
} | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.