fix: ensure rest props access on hoisted event handlers works (#12298)

If an even handler is hoisted and said event handler access rest props, we can't be sure whether or not it will access it via `$$props` (optimized) or via `rest()` - therefore we add both as params in this edge case

fixes #12279
pull/12304/head
Simon H 6 months ago committed by GitHub
parent 80263770f1
commit bc32b7c828
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure rest props access on hoisted event handlers works

@ -547,6 +547,11 @@ function get_hoistable_params(node, context) {
} else {
// 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,
// but we can't know if the delegated event handler will use it, so we need to add both as params
if (binding.kind === 'rest_prop' && context.state.analysis.runes) {
push_unique(b.id('$$props'));
}
}
}
}

@ -0,0 +1,14 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
mode: ['client'],
test({ assert, target, logs }) {
const btn = target.querySelector('button');
btn?.click();
flushSync();
assert.deepEqual(logs, ['worked']);
}
});

@ -0,0 +1,5 @@
<script>
let { label, ...rest } = $props();
</script>
<button onclick={() => rest?.onclick()}>{label}</button>

@ -0,0 +1,5 @@
<script>
import Child from './child.svelte';
</script>
<Child label="click me" onclick={() => console.log('worked')} />
Loading…
Cancel
Save