fix(analyze): check if a fragment has only one child element using tokens.
Summary
This bug occurs when a warning is supposed to be issued if a fragment has only one child element, but if line breaks or tabs are included, the warning does not appear. To check the number of child elements, child_list.len() is used, but this includes line breaks and tabs, causing the count of child elements to be more than one. https://github.com/biomejs/biome/blob/main/crates/biome_js_analyze/src/lint/complexity/no_useless_fragments.rs#L165
Therefore, I modified the code to scan the tokens of the fragment's children and check if there is only one JsSyntaxKind::JSX_SELF_CLOSING_ELEMENT or JsSyntaxKind::JSX_ELEMENT.
Test Plan
Change the path on line 17 of crates/biome_js_analyze/tests/quick_test.rs to tests/specs/complexity/noUselessFragments/issue_3545.jsx and tests/specs/complexity/noUselessFragments/withJsxElementInvalid.jsx. Run the tests and confirm that both tests pass successfully.
closed #3545
CodSpeed Performance Report
Merging #3582 will degrade performances by 6.98%
Comparing satojin219:fix/no-useless-fragments-one-element (fcd861c) with main (10ef9d4)
Summary
❌ 1 regressions
✅ 103 untouched benchmarks
:warning: Please fix the performance issues or acknowledge them on CodSpeed.
Benchmarks breakdown
| Benchmark | main |
satojin219:fix/no-useless-fragments-one-element |
Change | |
|---|---|---|---|---|
| ❌ | router_14177007973872119684.ts[cached] |
2.1 ms | 2.2 ms | -6.98% |
A test i s failing. According to it the following code shoudl also be rreporetd as useless:
<>
foo
</>
@Conaclos @ematipico I have fixed the bug, so please review again.
@satojin219 Can you update the changelog to describe your fix? https://github.com/biomejs/biome/blob/main/CONTRIBUTING.md#writing-a-changelog-line
@ematipico https://github.com/biomejs/biome/pull/3582#issuecomment-2286150152
chore: updaste CHANGELOG.md I've added it to the changelog!