-
-
Notifications
You must be signed in to change notification settings - Fork 804
fix(noNestedTernary): detect nested ternaries wrapped in parentheses #8086
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
Merged
ematipico
merged 2 commits into
biomejs:main
from
matanshavit:fix/8045-noNestedTernary-parentheses
Nov 12, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fix(noNestedTernary): detect nested ternaries wrapped in parentheses
- Loading branch information
commit 7d2de9f02f81bd930047b1bab60580e44867b7fe
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Fixed [#8045](https://github.com/biomejs/biome/issues/8045): The [`noNestedTernary`](https://biomejs.dev/linter/rules/no-nested-ternary/) rule now correctly detects nested ternary expressions even when they are wrapped in parentheses. | ||
|
|
||
| Previously, the rule would not flag nested ternaries like `foo ? (bar ? 1 : 2) : 3` because the parentheses prevented detection. The rule now looks through parentheses to identify nested conditionals. | ||
|
|
||
| **Previously not detected (now flagged):** | ||
|
|
||
| ```js | ||
| const result = foo ? (bar ? 1 : 2) : 3; | ||
| ``` | ||
|
|
||
| **Still valid (non-nested with parentheses):** | ||
|
|
||
| ```js | ||
| const result = (foo ? bar : baz); | ||
| ``` | ||
This file contains hidden or 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
18 changes: 17 additions & 1 deletion
18
crates/biome_js_analyze/tests/specs/style/noNestedTernary/invalid.js
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,19 @@ | ||
| var thing = foo ? bar : baz === qux ? quxx : foobar; | ||
|
|
||
| foo ? baz === qux ? quxx() : foobar() : bar(); | ||
| foo ? baz === qux ? quxx() : foobar() : bar(); | ||
|
|
||
| var thing1 = foo ? (bar ? 1 : 2) : 3; | ||
|
|
||
| var thing2 = foo ? 1 : (bar ? 2 : 3); | ||
|
|
||
| var thing3 = foo ? ((bar ? 1 : 2)) : 3; | ||
|
|
||
| var thing4 = foo ? 1 : ((bar ? 2 : 3)); | ||
|
|
||
| var thing5 = foo ? (baz === qux ? quxx : foobar) : bar; | ||
|
|
||
| const case1 = val ? (Math.random() ? 1 : 2) : 3; | ||
|
|
||
| const case2 = val ? 1 : Math.random() ? 2 : 3; | ||
|
|
||
| const case3 = (val ? (Math.random() ? 1 : 2) : 3); |
This file contains hidden or 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
18 changes: 12 additions & 6 deletions
18
crates/biome_js_analyze/tests/specs/style/noNestedTernary/valid.js
This file contains hidden or 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 |
|---|---|---|
| @@ -1,12 +1,18 @@ | ||
| /* should not generate diagnostics */ | ||
| const thing = foo ? bar : foobar; | ||
| const thing1 = foo ? bar : foobar; | ||
|
|
||
| let thing; | ||
| let thing2; | ||
|
|
||
| if (foo) { | ||
| thing = bar; | ||
| thing2 = bar; | ||
| } else if (baz === qux) { | ||
| thing = quxx; | ||
| thing2 = quxx; | ||
| } else { | ||
| thing = foobar; | ||
| } | ||
| thing2 = foobar; | ||
| } | ||
|
|
||
| const thing3 = (foo ? bar : baz); | ||
|
|
||
| const thing4 = foo ? (bar) : (baz); | ||
|
|
||
| const thing5 = ((foo ? bar : baz)); |
18 changes: 12 additions & 6 deletions
18
crates/biome_js_analyze/tests/specs/style/noNestedTernary/valid.js.snap
This file contains hidden or 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 |
|---|---|---|
| @@ -1,20 +1,26 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: valid.js | ||
| snapshot_kind: text | ||
| --- | ||
| # Input | ||
| ```js | ||
| /* should not generate diagnostics */ | ||
| const thing = foo ? bar : foobar; | ||
| const thing1 = foo ? bar : foobar; | ||
|
|
||
| let thing; | ||
| let thing2; | ||
|
|
||
| if (foo) { | ||
| thing = bar; | ||
| thing2 = bar; | ||
| } else if (baz === qux) { | ||
| thing = quxx; | ||
| thing2 = quxx; | ||
| } else { | ||
| thing = foobar; | ||
| thing2 = foobar; | ||
| } | ||
|
|
||
| const thing3 = (foo ? bar : baz); | ||
|
|
||
| const thing4 = foo ? (bar) : (baz); | ||
|
|
||
| const thing5 = ((foo ? bar : baz)); | ||
|
|
||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.