fix: handle event hoisting props referencing (#9846)

* fix: handle event hoisting props referencing
pull/9848/head
Dominic Gannaway 1 year ago committed by GitHub
parent fd78acfec9
commit 4717d2ad23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: handle event hoisting props referencing

@ -316,6 +316,7 @@ function get_hoistable_params(node, context) {
/** @type {import('estree').Pattern[]} */
const params = [];
let added_props = false;
for (const [reference] of scope.references) {
const binding = scope.get(reference);
@ -325,6 +326,19 @@ function get_hoistable_params(node, context) {
// We need both the subscription for getting the value and the store for updating
params.push(b.id(binding.node.name.slice(1)));
params.push(b.id(binding.node.name));
} else if (
// If we are referencing a simple $$props value, then we need to reference the object property instead
binding.kind === 'prop' &&
!binding.reassigned &&
binding.initial === null &&
!context.state.analysis.accessors &&
context.state.analysis.runes
) {
// Handle $$props.something use-cases
if (!added_props) {
added_props = true;
params.push(b.id('$$props'));
}
} else {
// create a copy to remove start/end tags which would mess up source maps
params.push(b.id(binding.node.name));

@ -0,0 +1,21 @@
import { test } from '../../test';
import { log } from './log.js';
export default test({
before_test() {
log.length = 0;
},
get props() {
return { item: { name: 'Dominic' } };
},
async test({ assert, target }) {
const [b1] = target.querySelectorAll('button');
b1?.click();
await Promise.resolve();
assert.deepEqual(log, ['Dominic']);
}
});

@ -0,0 +1,2 @@
/** @type {any[]} */
export const log = [];

@ -0,0 +1,13 @@
<script>
import { log } from './log.js';
let { item } = $props();
function onclick() {
log.push(item?.name);
}
</script>
<button {onclick}>
{item?.name}
</button>
Loading…
Cancel
Save