Skip to content

Commit

Permalink
fix: ensure event handlers referencing $host are not hoisted (#12775)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Aug 10, 2024
1 parent 9ff33f1 commit c32a918
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-windows-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure event handlers referencing $host are not hoisted
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ function get_delegated_event(event_name, handler, context) {
const visited_references = new Set();
const scope = target_function.metadata.scope;
for (const [reference] of scope.references) {
// Bail out if the arguments keyword is used
if (reference === 'arguments') return unhoisted;
// Bail out if the arguments keyword is used or $host is referenced
if (reference === 'arguments' || reference === '$host') return unhoisted;
// Bail out if references a store subscription
if (scope.get(`$${reference}`)?.kind === 'store_sub') return unhoisted;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export default test({

await tick();

el.shadowRoot.querySelector('button').click();
assert.deepEqual(events, ['greeting', 'hello']);
el.shadowRoot.querySelectorAll('button')[0].click();
el.shadowRoot.querySelectorAll('button')[1].click();
assert.deepEqual(events, ['greeting', 'hello', 'greeting', 'welcome']);

el.removeEventListener('greeting', handle_evt);
el.shadowRoot.querySelector('button').click();
assert.deepEqual(events, ['greeting', 'hello']);
el.shadowRoot.querySelectorAll('button')[0].click();
el.shadowRoot.querySelectorAll('button')[1].click();
assert.deepEqual(events, ['greeting', 'hello', 'greeting', 'welcome']);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
function greet(greeting) {
$host().dispatchEvent(new CustomEvent('greeting', { detail: greeting }))
}
function welcome() {
$host().dispatchEvent(new CustomEvent('greeting', { detail: 'welcome' }))
}
</script>

<button onclick={() => greet('hello')}>say hello</button>
<button onclick={welcome}>say welcome</button>

0 comments on commit c32a918

Please sign in to comment.