Back to Biomejs

noUselessFragments

src/content/docs/linter/rules/no-useless-fragments.mdx

latest5.4 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/complexity/noUselessFragments`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`react/jsx-no-useless-fragment`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-useless-fragment.md) - Same as [`react-x/no-useless-fragment`](https://eslint-react.xyz/docs/rules/no-useless-fragment) - Same as [`@eslint-react/no-useless-fragment`](https://eslint-react.xyz/docs/rules/no-useless-fragment)

How to configure

json
{
	"linter": {
		"rules": {
			"complexity": {
				"noUselessFragments": "error"
			}
		}
	}
}

Description

Disallow unnecessary fragments

Examples

Invalid

jsx
<>
    <>foo</>
    <SomeComponent />
</>
<pre class="language-text"><code class="language-text">code-block.jsx:2:5 <a href="https://biomejs.dev/linter/rules/no-useless-fragments">lint/complexity/noUselessFragments</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This fragment is unnecessary.</span> <strong>1 │ </strong>&lt;&gt; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> &lt;&gt;foo&lt;/&gt; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong> &lt;SomeComponent /&gt; <strong>4 │ </strong>&lt;/&gt; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">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 </span><span style="color: lightgreen;"><a href="https://legacy.reactjs.org/docs/fragments.html#keyed-fragments">fragment</a></span><span style="color: lightgreen;">.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove the Fragment</span> <strong> 2 │ </strong><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="color: Tomato;">&lt;</span><span style="color: Tomato;">&gt;</span>foo<span style="color: Tomato;">&lt;</span><span style="color: Tomato;">/</span><span style="color: Tomato;">&gt;</span> <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> </code></pre>
jsx
<></>
<pre class="language-text"><code class="language-text">code-block.jsx:1:1 <a href="https://biomejs.dev/linter/rules/no-useless-fragments">lint/complexity/noUselessFragments</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This fragment is unnecessary.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&lt;&gt;&lt;/&gt; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">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 </span><span style="color: lightgreen;"><a href="https://legacy.reactjs.org/docs/fragments.html#keyed-fragments">fragment</a></span><span style="color: lightgreen;">.</span> </code></pre>

Valid

jsx
<>
foo
</>
jsx
<React.Fragment>
foo
</React.Fragment>
jsx
<>
    <Foo />
    <Bar />
</>
jsx
<>foo {bar}</>
</TabItem> </Tabs>