Skip to content

Rule suggestion: $derived should not return a function #1332

@dimfeld

Description

@dimfeld

Motivation

AI Coding agents sometimes get a bit confused writing Svelte 5 and create code like this:

let x = $derived(() => values.filter((item) => item.active));

This, of course, makes x into a function instead of the value, and the coding agents often work around this by just changing the uses of x to x(), instead of fixing it properly to use derived.by or a single-expresssion $derived.

It would be helpful to have a lint rule for this.

Description

A $derived rune should not return a function. Either use a single expression with $derived or use $derived.by with a function.

Examples

<script>
// ✓ GOOD 
let x = $derived(expression);
let x = $derived.by(() => expression);
let x = $derived.by(() => {
  return complexExpression;
});
let x = $derived(calculate(value));
let x = $derived.by(calculate);

// ✗ BAD
let x =$derived(() => y);
let x =$derived(function() { return  y });
</script>

Additional comments

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions