noSvelteUselessDerivedBy (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/noSvelteUselessDerivedBy - This rule has a safe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noSvelteUselessDerivedBy": "error" } } }}Description
Section titled “Description”Disallow $derived.by() when $derived() is sufficient.
$derived.by() is only needed when a derivation requires statements, such as loops,
intermediate variables, or side effects. When the function passed to $derived.by() only
returns a single expression, passing that expression to $derived() directly is shorter
and behaves the same way.
The rule reports functions that take no parameters, are neither async nor generators, and
whose body is either a single expression or a single return statement.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<script>let count = $state(0);const doubled = $derived.by(() => count * 2);</script>code-block.svelte:3:17 lint/nursery/noSvelteUselessDerivedBy FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This $derived.by() function only returns a single expression.
1 │ <script>
2 │ let count = $state(0);
> 3 │ const doubled = $derived.by(() => count * 2);
│ ^^^^^^^^^^^
4 │ </script>
5 │
ℹ $derived.by() is only needed when the derivation requires statements, such as loops or intermediate variables. Passing the expression to $derived() directly is shorter and behaves the same way.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Pass the expression to $derived() directly.
1 1 │ <script>
2 2 │ let count = $state(0);
3 │ - const·doubled·=·$derived.by(()·=>·count·*·2);
3 │ + const·doubled·=·$derived(count·*·2);
4 4 │ </script>
5 5 │
<script>let count = $state(0);const doubled = $derived.by(() => { return count * 2;});</script>code-block.svelte:3:17 lint/nursery/noSvelteUselessDerivedBy FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This $derived.by() function only returns a single expression.
1 │ <script>
2 │ let count = $state(0);
> 3 │ const doubled = $derived.by(() => {
│ ^^^^^^^^^^^
4 │ return count * 2;
5 │ });
ℹ $derived.by() is only needed when the derivation requires statements, such as loops or intermediate variables. Passing the expression to $derived() directly is shorter and behaves the same way.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Pass the expression to $derived() directly.
1 1 │ <script>
2 2 │ let count = $state(0);
3 │ - const·doubled·=·$derived.by(()·=>·{
4 │ - ····return·count·*·2;
5 │ - });
3 │ + const·doubled·=·$derived(count·*·2);
6 4 │ </script>
7 5 │
<script>let count = $state(0);const doubled = $derived(count * 2);const doubledPlusOne = $derived.by(() => { const doubled = count * 2; return doubled + 1;});</script>Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.