From 35ab21df93c6978f279bcdc69ef18b01575745f2 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 18 Jul 2024 15:57:32 +0200 Subject: [PATCH] fix: don't add imports to hoisted event parameters (#12493) fixes #12489 --- .changeset/popular-cups-bathe.md | 5 +++++ .../compiler/phases/3-transform/client/utils.js | 5 ++++- .../Component.svelte | 11 +++++++++++ .../event-import-no-param-hoisting/_config.js | 14 ++++++++++++++ .../event-import-no-param-hoisting/main.svelte | 11 +++++++++++ .../event-import-no-param-hoisting/state.svelte.js | 5 +++++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 .changeset/popular-cups-bathe.md create mode 100644 packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/Component.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/main.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/event-import-no-param-hoisting/state.svelte.js 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; +}