Skip to content

Commit 9716c31

Browse files
committed
fix: store from props hoist wrong param
1 parent 68071f7 commit 9716c31

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

.changeset/tiny-moose-kiss.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: store from props hoist wrong param

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,20 @@ function get_hoistable_params(node, context) {
473473

474474
if (binding !== null && !scope.declarations.has(reference) && binding.initial !== node) {
475475
if (binding.kind === 'store_sub') {
476-
// We need both the subscription for getting the value and the store for updating
477-
safe_push(b.id(binding.node.name.slice(1)));
476+
const is_from_prop =
477+
binding.declaration_kind === 'synthetic' &&
478+
[...binding.scope.declarations.values()].find(
479+
(declaration) => declaration.kind === 'prop' || declaration.kind === 'bindable_prop'
480+
);
481+
if (is_from_prop && !added_props) {
482+
// if the store come from props we want $$props to be pushed rather than the name
483+
// of the store since that variable doesn't exist
484+
added_props = true;
485+
safe_push(b.id('$$props'));
486+
} else {
487+
// We need both the subscription for getting the value and the store for updating
488+
safe_push(b.id(binding.node.name.slice(1)));
489+
}
478490
safe_push(b.id(binding.node.name));
479491
} else if (
480492
// If it's a destructured derived binding, then we can extract the derived signal reference and use that.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
compileOptions: {
5+
dev: true
6+
},
7+
recover: true,
8+
mode: ['client']
9+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
const { attrs } = $props();
3+
function increment() {
4+
$attrs.count++;
5+
}
6+
</script>
7+
8+
<button onclick={increment}>
9+
+
10+
</button>

0 commit comments

Comments
 (0)