fix: declare `let:` directives before `{@const}` on slotted elements (#18271)

Fixes #18119 (missed a spot in #16985)
pull/18273/head
wheelman 1 month ago committed by GitHub
parent 8b961be0b1
commit bb69f9c44c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: declare `let:` directives before `{@const}` declarations on slotted elements

@ -201,8 +201,8 @@ export function RegularElement(node, context) {
}
}
// Let bindings first, they can be used on attributes
context.state.init.push(...lets);
// Let bindings first, they can be used on attributes and `{@const}` declarations
context.state.let_directives.push(...lets);
const node_id = context.state.node;

@ -0,0 +1,7 @@
<script>
export let things;
</script>
{#each things as thing}
<slot name="foo" {thing} />
{/each}

@ -0,0 +1,15 @@
import { test } from '../../test';
// `let:` directives on a slotted element must be declared before sibling `{@const}`
// declarations that capture them. In dev mode the `{@const}` derived is read eagerly,
// so a wrong declaration order throws "Cannot access '...' before initialization".
export default test({
compileOptions: {
dev: true
},
html: `
<div slot="foo"><span>1</span></div>
<div slot="foo"><span>2</span></div>
`
});

@ -0,0 +1,10 @@
<script>
import Nested from './Nested.svelte';
</script>
<Nested things={[1, 2]}>
<div slot="foo" let:thing>
{@const props = { thing }}
<span>{props.thing}</span>
</div>
</Nested>
Loading…
Cancel
Save