mirror of https://github.com/sveltejs/svelte
fix: more robust handling of var declarations (#12949)
* fix: more robust handling of var declarations - 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) * simplify --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/12960/head
parent
70ed8cd666
commit
01d32e53a9
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: more robust handling of var declarations
|
@ -0,0 +1,7 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
test({ assert, logs }) {
|
||||
assert.deepEqual(logs, [undefined, undefined, 10, 20, 0, 1]);
|
||||
}
|
||||
});
|
@ -0,0 +1,24 @@
|
||||
<script>
|
||||
console.log(foo, double);
|
||||
var foo = $state(10);
|
||||
var double = $derived(foo * 2);
|
||||
console.log(foo, double);
|
||||
|
||||
function wrap(initial) {
|
||||
var _value = $state(initial);
|
||||
|
||||
return {
|
||||
get() {
|
||||
return _value;
|
||||
},
|
||||
set(state) {
|
||||
_value = state;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var wrapped = wrap(0);
|
||||
console.log(wrapped.get());
|
||||
wrapped.set(1);
|
||||
console.log(wrapped.get());
|
||||
</script>
|
Loading…
Reference in new issue