Skip to content

state_referenced_locally warning inconsistencies #16343

@timephy

Description

@timephy

Describe the bug

The warning state_referenced_locally does not show up consistently where I believe it should.

Consider this example:

<script lang="ts">
    // state created from props should warn but doesn't
    let { x } = $props()
    let y = $state(x) // ❌ this should warn but doesn't
    let z = $state(y) // ❌ this should warn but doesn't

    // state created from other state only warns "one level deep"
    let a = $state(0)
    let b = $state(a) // ✅ this does warn
    let c = $state(b) // ❌ this should warn but doesn't

    // state created from implicitly undefined state (putting in `undefined`, `null`, `0`, `false` here does warn)
    let h = $state()
    let j = $state(h) // ❌ this should warn but doesn't
</script>

As outlines in the comments above, the warnings are shown very inconsistently.
I believe in many cases (see ❌ above) warnings should be shown.

The main problems are:

  • $state(...) created from $props() does not warn
  • $state(...) created from other $state(...) only warns "one level deep"
  • $state(...) created from implicitly undefined state ($state()) does not warn

Maybe there are more cases that I did not cover that should warn as well.

Reproduction

https://svelte.dev/playground/62605b146a9e4c4f812b9f872ba21288?version=5.35.5

Logs

This reference only captures the initial value of a. Did you mean to reference it inside a closure instead?
https://svelte.dev/e/state_referenced_locally (state_referenced_locally)

System Info

playground

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions