Back to Biomejs

useQwikValidLexicalScope

src/content/docs/linter/rules/use-qwik-valid-lexical-scope.mdx

latest4.2 KB
Original Source

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

<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v2.2.6` - Diagnostic Category: [`lint/correctness/useQwikValidLexicalScope`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - This rule belongs to the following domains: - [`qwik`](/linter/domains#qwik) - Sources: - Same as [`qwik/valid-lexical-scope`](https://qwik.dev/docs/advanced/eslint/#valid-lexical-scope)

How to configure

json
{
	"linter": {
		"rules": {
			"correctness": {
				"useQwikValidLexicalScope": "error"
			}
		}
	}
}

Description

Disallow unserializable expressions in Qwik dollar ($) scopes.

Ensures all captured values in Qwik components can be properly serialized for resumability. See Qwik Optimizer: Lexical Scope for proper usage patterns.

Examples

Invalid

js
// Arrow function assigned without wrapping it in $(...)
const handleClick = () => {
  console.log("clicked");
};
<pre class="language-text"><code class="language-text">code-block.js:2:21 <a href="https://biomejs.dev/linter/rules/use-qwik-valid-lexical-scope">lint/correctness/useQwikValidLexicalScope</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Non-serializable expression must be wrapped with </span><span style="color: Tomato;"><strong>$(...)</strong></span> <strong>1 │ </strong>// Arrow function assigned without wrapping it in $(...) <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong>const handleClick = () =&gt; &#123; <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;">&gt;</span></strong> <strong>3 │ </strong> console.log(&quot;clicked&quot;); <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong>&#125;; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Qwik requires serializable closures for: </span> <span style="color: lightgreen;">- Resumability (pausing/resuming execution) </span> <span style="color: lightgreen;">- Code splitting (lazy loading components) </span> <span style="color: lightgreen;">- Optimized rehydration (client-side continuation)</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Wrap the expression with </span><span style="color: lightgreen;"><strong>$(...)</strong></span><span style="color: lightgreen;"> to make it serializable. Learn more: </span><span style="color: lightgreen;"><a href="https://qwik.dev/docs/components/state/#use-methods">Qwik documentation</a></span><span style="color: lightgreen;">.</span> </code></pre>

Valid

js
const handleClick = $(() => {
  // Valid: only using serializable variables or props
  console.log("clicked");
});
</TabItem> </Tabs>