Skip to content

Commit 3546645

Browse files
committed
fix: don't try to add owners to non-$state class fields
`$state.raw` and `$derived(.by)` will not have a state symbol on them, potentially causing a disastrous amount of traversal to potentially not find any state symbol. So it's better to not traverse them. Potentially someone could create a `$state` while creating `$state.raw` or inside a `$derived.by`, but that feels so much of an edge case that it doesn't warrant a perf hit for the common case. Fixes #14491
1 parent a65e68c commit 3546645

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

.changeset/giant-windows-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: don't try to add owners to non-`$state` class fields

packages/svelte/src/compiler/phases/3-transform/client/visitors/ClassBody.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,22 @@ export function ClassBody(node, context) {
184184
'method',
185185
b.id('$.ADD_OWNER'),
186186
[b.id('owner')],
187-
Array.from(public_state.keys()).map((name) =>
188-
b.stmt(
189-
b.call(
190-
'$.add_owner',
191-
b.call('$.get', b.member(b.this, b.private_id(name))),
192-
b.id('owner'),
193-
b.literal(false),
194-
is_ignored(node, 'ownership_invalid_binding') && b.true
187+
Array.from(public_state)
188+
// Only run ownership addition on $state fields.
189+
// Theoretically someone could create a `$state` while creating `$state.raw` or inside a `$derived.by`,
190+
// but that feels so much of an edge case that it doesn't warrant a perf hit for the common case.
191+
.filter(([_, { kind }]) => kind === 'state')
192+
.map(([name]) =>
193+
b.stmt(
194+
b.call(
195+
'$.add_owner',
196+
b.call('$.get', b.member(b.this, b.private_id(name))),
197+
b.id('owner'),
198+
b.literal(false),
199+
is_ignored(node, 'ownership_invalid_binding') && b.true
200+
)
195201
)
196-
)
197-
),
202+
),
198203
true
199204
)
200205
);

0 commit comments

Comments
 (0)