-
-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(noUselessFragments): check Fragment when it under JSX Element or …
…Condition Expr (#4882)
- Loading branch information
1 parent
94bf15e
commit 7f0e969
Showing
5 changed files
with
149 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
biome_analyze: patch | ||
--- | ||
|
||
# Fix Fragment when it under JSXElement or JS Condition expr | ||
|
||
Fix [#4751](https://github.com/biomejs/biome/issues/4751) by checking fragments inside `JSXElement` and conditional expressions. For example: | ||
|
||
The Case: | ||
|
||
```jsx | ||
<section> | ||
<> | ||
<div /> | ||
<div /> | ||
</> | ||
</section>; | ||
``` | ||
|
||
And: | ||
|
||
```jsx | ||
showFullName ? <>{fullName}</> : <>{firstName}</>; | ||
``` | ||
|
||
It will report. |
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
8 changes: 8 additions & 0 deletions
8
crates/biome_js_analyze/tests/specs/complexity/noUselessFragments/issue_4751.jsx
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,8 @@ | ||
<section> | ||
<> | ||
<div /> | ||
<div /> | ||
</> | ||
</section>; | ||
|
||
showFullName ? <>{fullName}</> : <>{firstName}</>; |
87 changes: 87 additions & 0 deletions
87
crates/biome_js_analyze/tests/specs/complexity/noUselessFragments/issue_4751.jsx.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,87 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: issue_4751.jsx | ||
snapshot_kind: text | ||
--- | ||
# Input | ||
```jsx | ||
<section> | ||
<> | ||
<div /> | ||
<div /> | ||
</> | ||
</section>; | ||
|
||
showFullName ? <>{fullName}</> : <>{firstName}</>; | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
issue_4751.jsx:2:3 lint/complexity/noUselessFragments FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid using unnecessary Fragment. | ||
1 │ <section> | ||
> 2 │ <> | ||
│ ^^ | ||
> 3 │ <div /> | ||
> 4 │ <div /> | ||
> 5 │ </> | ||
│ ^^^ | ||
6 │ </section>; | ||
7 │ | ||
i A fragment is redundant if it contains only one child, or if it is the child of a html element, and is not a keyed fragment. | ||
i Unsafe fix: Remove the Fragment | ||
1 1 │ <section> | ||
2 │ - ··<> | ||
3 2 │ <div /> | ||
4 3 │ <div /> | ||
5 │ - ··</> | ||
6 │ - </section>; | ||
4 │ + ··</section>; | ||
7 5 │ | ||
8 6 │ showFullName ? <>{fullName}</> : <>{firstName}</>; | ||
``` | ||
|
||
``` | ||
issue_4751.jsx:8:16 lint/complexity/noUselessFragments FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid using unnecessary Fragment. | ||
6 │ </section>; | ||
7 │ | ||
> 8 │ showFullName ? <>{fullName}</> : <>{firstName}</>; | ||
│ ^^^^^^^^^^^^^^^ | ||
i A fragment is redundant if it contains only one child, or if it is the child of a html element, and is not a keyed fragment. | ||
i Unsafe fix: Remove the Fragment | ||
8 │ showFullName·?·<>{fullName}</>·:·<>{firstName}</>; | ||
│ --- ---- | ||
``` | ||
|
||
``` | ||
issue_4751.jsx:8:34 lint/complexity/noUselessFragments FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid using unnecessary Fragment. | ||
6 │ </section>; | ||
7 │ | ||
> 8 │ showFullName ? <>{fullName}</> : <>{firstName}</>; | ||
│ ^^^^^^^^^^^^^^^^ | ||
i A fragment is redundant if it contains only one child, or if it is the child of a html element, and is not a keyed fragment. | ||
i Unsafe fix: Remove the Fragment | ||
8 │ showFullName·?·<>{fullName}</>·:·<>{firstName}</>; | ||
│ --- ---- | ||
``` |