Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure $host rune correctly compiles in variable declarations #13127

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-cars-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure $host rune correctly compiles in variable declarations
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export function VariableDeclaration(node, context) {
rune === '$effect.tracking' ||
rune === '$effect.root' ||
rune === '$inspect' ||
rune === '$state.snapshot'
rune === '$state.snapshot' ||
rune === '$host'
) {
if (init != null && is_hoisted_function(init)) {
context.state.hoisted.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export default test({

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

el.removeEventListener('greeting', handle_evt);
el.shadowRoot.querySelectorAll('button')[0].click();
el.shadowRoot.querySelectorAll('button')[1].click();
assert.deepEqual(events, ['greeting', 'hello', 'greeting', 'welcome']);
el.shadowRoot.querySelectorAll('button')[2].click();
assert.deepEqual(events, ['greeting', 'hello', 'greeting', 'welcome', 'greeting', 'bonjour']);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
function welcome() {
$host().dispatchEvent(new CustomEvent('greeting', { detail: 'welcome' }))
}
function bonjour() {
const element = $host();
element.dispatchEvent(new CustomEvent('greeting', { detail: 'bonjour' }))
}
</script>

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