diff --git a/.changeset/popular-cups-bathe.md b/.changeset/popular-cups-bathe.md new file mode 100644 index 0000000000..861c48faeb --- /dev/null +++ b/.changeset/popular-cups-bathe.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: don't add imports to hoisted event parameters diff --git a/packages/svelte/src/compiler/phases/3-transform/client/utils.js b/packages/svelte/src/compiler/phases/3-transform/client/utils.js index ea0d3d55e7..5cc0f5c91b 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/utils.js @@ -544,7 +544,10 @@ function get_hoistable_params(node, context) { !is_prop_source(binding, context.state) ) { push_unique(b.id('$$props')); - } else { + } else if ( + // imports don't need to be hoisted + binding.declaration_kind !== 'import' + ) { // create a copy to remove start/end tags which would mess up source maps push_unique(b.id(binding.node.name)); // rest props are often accessed through the $$props object for optimization reasons, diff --git a/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/Component.svelte b/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/Component.svelte new file mode 100644 index 0000000000..11003a4aad --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/Component.svelte @@ -0,0 +1,11 @@ + + + diff --git a/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/_config.js b/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/_config.js new file mode 100644 index 0000000000..e8018d17f1 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/_config.js @@ -0,0 +1,14 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + mode: ['client'], + test({ assert, logs, target }) { + const btn = target.querySelector('button'); + + btn?.click(); + flushSync(); + + assert.deepEqual(logs, [1, 1]); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/main.svelte b/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/main.svelte new file mode 100644 index 0000000000..984585bb32 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/main.svelte @@ -0,0 +1,11 @@ + + + diff --git a/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/state.svelte.js b/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/state.svelte.js new file mode 100644 index 0000000000..2808dc3a18 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/state.svelte.js @@ -0,0 +1,5 @@ +export let num = 0; + +export function increment() { + num += 1; +}