Skip to content

Commit 35ab21d

Browse files
authored
fix: don't add imports to hoisted event parameters (#12493)
fixes #12489
1 parent 4b8674f commit 35ab21d

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

.changeset/popular-cups-bathe.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 add imports to hoisted event parameters

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,10 @@ function get_hoistable_params(node, context) {
544544
!is_prop_source(binding, context.state)
545545
) {
546546
push_unique(b.id('$$props'));
547-
} else {
547+
} else if (
548+
// imports don't need to be hoisted
549+
binding.declaration_kind !== 'import'
550+
) {
548551
// create a copy to remove start/end tags which would mess up source maps
549552
push_unique(b.id(binding.node.name));
550553
// rest props are often accessed through the $$props object for optimization reasons,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { num } from './state.svelte.js';
3+
let { foo } = $props();
4+
5+
function onclick() {
6+
foo();
7+
console.log(num);
8+
}
9+
</script>
10+
11+
<button {onclick}>click</button>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
mode: ['client'],
6+
test({ assert, logs, target }) {
7+
const btn = target.querySelector('button');
8+
9+
btn?.click();
10+
flushSync();
11+
12+
assert.deepEqual(logs, [1, 1]);
13+
}
14+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { num, increment } from './state.svelte.js';
3+
import Component from './Component.svelte';
4+
5+
function foo() {
6+
increment();
7+
console.log(num);
8+
}
9+
</script>
10+
11+
<Component {foo} />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export let num = 0;
2+
3+
export function increment() {
4+
num += 1;
5+
}

0 commit comments

Comments
 (0)