sveltejs / language-tools

The Svelte Language Server, and official extensions which use it
MIT License
1.26k stars 201 forks source link

Improve Visibility of Reactive Variables #2188

Open plasmatech8 opened 1 year ago

plasmatech8 commented 1 year ago

Description

This is most relevant to Svelte 5, but is also applicable to prior versions.

Reactive variables and non-reactive variables are co-located in your code when writing a Svelte app.

This may cause some minor inconvenience when you need to check the implementation to determine whether a variable is reactive or not.

This may also be inconvenient for beginners who might not know what is reactivity is.

For example:

Proposed solution

Reactive variables (state/derived/stores) visual cues:

(Additional possibilities/features)

Show side effects of a state assignment =:

Show dependencies of a reactive variable:

Alternatives

Other ways to visually indicate reactive variables:

Additional Information, eg. Screenshots

A visual distinction between reactive and non-reactive variables could add clarity and improve elegance in the sense of having reactive/non-reactive variables co-located.

Note: reddit post

Below shows an example in Svelte 5 (as-of the current early preview) of what it could look like before and after adding proposed features:

output2

opensas commented 4 months ago

Perhaps, more than visibility, we could use a Reactive<T> type to mark a variable as being reactive

so $state('hello') would return a Reactive<string> instead of a plain string

that way we could also state that a prop (which are not reactive by default) are required to be reactive, just like we did when we needed a Writable<T> with stores

brunnerh commented 4 months ago

Types do not work, they would falsely transfer on assignment to non-reactive variables (like a const without $derived or when passed to a function).

opensas commented 4 months ago

Types do not work, they would falsely transfer on assignment to non-reactive variables (like a const without $derived or when passed to a function).

haven't thought about it, it would be great to find a workaround to let the type system be aware of reactive values

jnilla commented 1 week ago

From the docs: https://react.dev/learn/lifecycle-of-reactive-effects#all-variables-declared-in-the-component-body-are-reactive

Quote: Props and state aren’t the only reactive values. Values that you calculate from them are also reactive. If the props or state change, your component will re-render, and the values calculated from them will also change.