Skip to content

Commit 4717d2a

Browse files
authored
fix: handle event hoisting props referencing (#9846)
* fix: handle event hoisting props referencing
1 parent fd78acf commit 4717d2a

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

.changeset/clever-rockets-burn.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: handle event hoisting props referencing

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ function get_hoistable_params(node, context) {
316316

317317
/** @type {import('estree').Pattern[]} */
318318
const params = [];
319+
let added_props = false;
319320

320321
for (const [reference] of scope.references) {
321322
const binding = scope.get(reference);
@@ -325,6 +326,19 @@ function get_hoistable_params(node, context) {
325326
// We need both the subscription for getting the value and the store for updating
326327
params.push(b.id(binding.node.name.slice(1)));
327328
params.push(b.id(binding.node.name));
329+
} else if (
330+
// If we are referencing a simple $$props value, then we need to reference the object property instead
331+
binding.kind === 'prop' &&
332+
!binding.reassigned &&
333+
binding.initial === null &&
334+
!context.state.analysis.accessors &&
335+
context.state.analysis.runes
336+
) {
337+
// Handle $$props.something use-cases
338+
if (!added_props) {
339+
added_props = true;
340+
params.push(b.id('$$props'));
341+
}
328342
} else {
329343
// create a copy to remove start/end tags which would mess up source maps
330344
params.push(b.id(binding.node.name));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { test } from '../../test';
2+
import { log } from './log.js';
3+
4+
export default test({
5+
before_test() {
6+
log.length = 0;
7+
},
8+
9+
get props() {
10+
return { item: { name: 'Dominic' } };
11+
},
12+
13+
async test({ assert, target }) {
14+
const [b1] = target.querySelectorAll('button');
15+
16+
b1?.click();
17+
await Promise.resolve();
18+
19+
assert.deepEqual(log, ['Dominic']);
20+
}
21+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** @type {any[]} */
2+
export const log = [];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import { log } from './log.js';
3+
4+
let { item } = $props();
5+
6+
function onclick() {
7+
log.push(item?.name);
8+
}
9+
</script>
10+
11+
<button {onclick}>
12+
{item?.name}
13+
</button>

0 commit comments

Comments
 (0)