Skip to content

Commit

Permalink
fix: delete transformers shadowed by unreassigned state
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 19, 2024
1 parent b3842d7 commit 996c20f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-grapes-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: delete transformers shadowed by unreassigned state
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/** @import { Visitors, ComponentClientTransformState, ClientTransformState } from './types' */
import { walk } from 'zimmerframe';
import * as b from '../../../utils/builders.js';
import { build_getter } from './utils.js';
import { build_getter, is_state_source } from './utils.js';
import { render_stylesheet } from '../css/index.js';
import { dev, filename } from '../../../state.js';
import { AnimateDirective } from './visitors/AnimateDirective.js';
Expand Down Expand Up @@ -66,7 +66,12 @@ const visitors = {
const transform = { ...state.transform };

for (const [name, binding] of scope.declarations) {
if (binding.kind === 'normal') {
if (
binding.kind === 'normal' ||
// Reads of `$state(...)` declarations are not
// transformed if they are never reassigned
(binding.kind === 'state' && !is_state_source(binding, state.analysis))
) {
delete transform[name];
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: `<div>0</div>`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
let value = $derived.by(() => {
const value = $state(0);
return value;
});
</script>

<div>{value}</div>

0 comments on commit 996c20f

Please sign in to comment.