Skip to content

Declaration order of svelte 4 slot variables broken in Svelte 5.38+ when using @const directive #16585

@psyirius

Description

@psyirius

Describe the bug

Svelte 4 slots using inline @const declaration resulting in error at runtime
Cannot access 'index' before initialization

<div slot="item" let:index let:style {style}>
	<!-- This results in error: ❌ -->
	<!-- `Cannot access 'index' before initialization` -->
	{@const item = list[index]}
	XOXO: {item}
	
	<!-- This works ✅ -->
	XOXO: {list[index]}
</div>

Reproduction

<div slot="item" let:index let:style {style}>
	<!-- This works ✅ -->
	XOXO: {list[index]}
</div>

Emits:
Image

<div slot="item" let:index let:style {style}>
	<!-- This results in error: ❌ -->
	<!-- `Cannot access 'index' before initialization` -->
	{@const item = list[index]}
	XOXO: {item}
</div>

Emits:
Image
Here, const is emitted before any slot variable declaration

Here is an example project:
https://svelte.dev/playground/69d51ef1910347c4823c3e1c201d90f6?version=5.38.0

Logs

Cannot access 'index' before initialization in <unknown> in VList.svelte in App.svelte in

System Info

irrelevant

Severity

blocking an upgrade

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions