Skip to content

Commit 2d37826

Browse files
authored
fix: ensure $host rune correctly compiles in variable declarations (#13127)
Fixes #13121
1 parent 71c1227 commit 2d37826

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

.changeset/heavy-cars-ring.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure $host rune correctly compiles in variable declarations

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export function VariableDeclaration(node, context) {
2828
rune === '$effect.tracking' ||
2929
rune === '$effect.root' ||
3030
rune === '$inspect' ||
31-
rune === '$state.snapshot'
31+
rune === '$state.snapshot' ||
32+
rune === '$host'
3233
) {
3334
if (init != null && is_hoisted_function(init)) {
3435
context.state.hoisted.push(

packages/svelte/tests/runtime-browser/custom-elements-samples/host-rune/_config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ export default test({
1616

1717
el.shadowRoot.querySelectorAll('button')[0].click();
1818
el.shadowRoot.querySelectorAll('button')[1].click();
19-
assert.deepEqual(events, ['greeting', 'hello', 'greeting', 'welcome']);
19+
el.shadowRoot.querySelectorAll('button')[2].click();
20+
assert.deepEqual(events, ['greeting', 'hello', 'greeting', 'welcome', 'greeting', 'bonjour']);
2021

2122
el.removeEventListener('greeting', handle_evt);
2223
el.shadowRoot.querySelectorAll('button')[0].click();
2324
el.shadowRoot.querySelectorAll('button')[1].click();
24-
assert.deepEqual(events, ['greeting', 'hello', 'greeting', 'welcome']);
25+
el.shadowRoot.querySelectorAll('button')[2].click();
26+
assert.deepEqual(events, ['greeting', 'hello', 'greeting', 'welcome', 'greeting', 'bonjour']);
2527
}
2628
});

packages/svelte/tests/runtime-browser/custom-elements-samples/host-rune/main.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
function welcome() {
99
$host().dispatchEvent(new CustomEvent('greeting', { detail: 'welcome' }))
1010
}
11+
function bonjour() {
12+
const element = $host();
13+
element.dispatchEvent(new CustomEvent('greeting', { detail: 'bonjour' }))
14+
}
1115
</script>
1216

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

0 commit comments

Comments
 (0)