-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Svelte 5: using var
with $state
is brittle
#12807
Comments
Can't we statically detect it's referenced before and just statically compile the read to |
Not trivially: var a = get_b(), b = $state();
function get_b() {
return b; // we certainly can't compile this to `undefined`
} With sufficiently sophisticated static analysis we could determine that |
Adjacent: #12900. |
- fixes #12807: state declared with var is now retrieved using a new `safe_get` function, which works like `get` but checks for undefined - fixes #12900: ensure we're not creating another new scope for block statements of function declarations, which resulted in var declarations getting "swallowed" (because they were hoisted to the function declaration scope and never seen by our logic)
I opted for option 4 in #12949: Add a new |
Describe the bug
State declarations are transformed thusly:
If we tried to
console.log(count)
before the declaration, we would get a TDZ error:But if it's a
var
instead, we don't — instead we try toget(undefined)
.This errors with
which is cryptic and unhelpful.
Possible remedies:
if (signal === undefined) return signal
to the top ofget
svelte/packages/svelte/src/internal/client/runtime.js
Lines 722 to 732 in 5094cb9
var
Option 1 means doing a tiny bit of extra work on every
get
for something that's a real edge case. Option 2 feels like a bit of a cop-out. So I think we should probably do option 3.Reproduction
link
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: