Skip to content
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

fix(analyze): check if a fragment has only one child element using tokens. #3582

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,28 @@ impl Rule for NoUselessFragments {

let child_list = fragment.children();

let mut self_closing_or_element_count = 0;

for child in child_list.iter() {
match child.syntax().kind() {
JsSyntaxKind::JSX_SELF_CLOSING_ELEMENT
| JsSyntaxKind::JSX_ELEMENT
| JsSyntaxKind::JSX_EXPRESSION_CHILD
| JsSyntaxKind::JSX_TEXT => {
if child.syntax().kind() == JsSyntaxKind::JSX_TEXT
&& child_list.len() > 1
{
continue;
}
self_closing_or_element_count += 1;
}
_ => {}
}
}

if !parents_where_fragments_must_be_preserved {
match child_list.first() {
Some(first) if child_list.len() == 1 => {
Some(first) if self_closing_or_element_count == 1 => {
if JsxText::can_cast(first.syntax().kind()) && in_jsx_attr_expr {
return None;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// https://github.com/biomejs/biome/issues/3545
<>
<Component />
</>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
assertion_line: 84
expression: issue_3545.jsx
---
# Input
```jsx
// https://github.com/biomejs/biome/issues/3545
<>
<Component />
</>;

```

# Diagnostics
```
issue_3545.jsx:2:1 lint/complexity/noUselessFragments FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Avoid using unnecessary Fragment.

1 │ // https://github.com/biomejs/biome/issues/3545
> 2 │ <>
│ ^^
> 3 │ <Component />
> 4 │ </>;
│ ^^^
5 │

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 │ // https://github.com/biomejs/biome/issues/3545
2 │ - <>
3 │ - → <Component·/>
4 │ - </>;
2 │ + "";
5 3 │
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a regression here



```
Loading