Back to Biomejs

useConsistentCurlyBraces

src/content/docs/linter/rules/use-consistent-curly-braces.mdx

latest8.1 KB
Original Source

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

<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v1.8.2` - Diagnostic Category: [`lint/style/useConsistentCurlyBraces`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Inspired from [`react/jsx-curly-brace-presence`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-curly-brace-presence.md)

How to configure

json
{
	"linter": {
		"rules": {
			"style": {
				"useConsistentCurlyBraces": "error"
			}
		}
	}
}

Description

This rule enforces consistent use of curly braces inside JSX attributes and JSX children.

For situations where JSX expressions are unnecessary, please refer to the React doc and this page about JSX gotchas.

This rule will check for and warn about unnecessary curly braces in both JSX props and children.

Examples

Invalid

jsx
<Foo>{'Hello world'}</Foo>
<pre class="language-text"><code class="language-text">code-block.jsx:1:6 <a href="https://biomejs.dev/linter/rules/use-consistent-curly-braces">lint/style/useConsistentCurlyBraces</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Should not have curly braces around expression.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&lt;Foo&gt;&#123;'Hello world'&#125;&lt;/Foo&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><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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">JSX child does not need to be wrapped in curly braces.</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 curly braces around the expression.</span> <strong> 1 │ </strong>&lt;Foo&gt;<span style="color: Tomato;">&#123;</span><span style="color: Tomato;">'</span>Hello<span style="opacity: 0.8;">·</span>world<span style="color: Tomato;">'</span><span style="color: Tomato;">&#125;</span>&lt;/Foo&gt; <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> </code></pre>
jsx
<Foo foo={'bar'} />
<pre class="language-text"><code class="language-text">code-block.jsx:1:10 <a href="https://biomejs.dev/linter/rules/use-consistent-curly-braces">lint/style/useConsistentCurlyBraces</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Should not have curly braces around expression.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&lt;Foo foo=&#123;'bar'&#125; /&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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">JSX attribute value does not need to be wrapped in curly braces.</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 curly braces around the expression.</span> <strong> 1 │ </strong>&lt;Foo<span style="opacity: 0.8;">·</span>foo=<span style="color: Tomato;">&#123;</span>'bar'<span style="color: Tomato;">&#125;</span><span style="opacity: 0.8;">·</span>/&gt; <strong> │ </strong> <span style="color: Tomato;">-</span> <span style="color: Tomato;">-</span> </code></pre>
jsx
<Foo foo=<Bar /> />
<pre class="language-text"><code class="language-text">code-block.jsx:1:10 <a href="https://biomejs.dev/linter/rules/use-consistent-curly-braces">lint/style/useConsistentCurlyBraces</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Should have curly braces around expression.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&lt;Foo foo=&lt;Bar /&gt; /&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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">JSX attribute value should be wrapped in curly braces. This will make the JSX attribute value more readable.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Add curly braces around the expression.</span> <strong> 1 │ </strong>&lt;Foo<span style="opacity: 0.8;">·</span>foo=<span style="color: MediumSeaGreen;">&#123;</span>&lt;Bar<span style="opacity: 0.8;">·</span>/&gt;<span style="color: MediumSeaGreen;">&#125;</span><span style="opacity: 0.8;">·</span>/&gt; <strong> │ </strong> <span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">+</span> </code></pre>

Valid

jsx
<>
    <Foo>Hello world</Foo>
    <Foo foo="bar" />
    <Foo foo={5} />
    <Foo foo={<Bar />} />
</>
</TabItem> </Tabs>