Skip to content

Svelte 5: Snippets incorrectly sharing scope inside component slots #9756

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

Closed
Not-Jayden opened this issue Dec 4, 2023 · 2 comments · Fixed by #9759
Closed

Svelte 5: Snippets incorrectly sharing scope inside component slots #9756

Not-Jayden opened this issue Dec 4, 2023 · 2 comments · Fixed by #9759
Assignees
Milestone

Comments

@Not-Jayden
Copy link
Contributor

Not-Jayden commented Dec 4, 2023

Describe the bug

When declaring snippets with the same name inside separate component slots, you get a compiler error for '<name>' has already been declared, even though they shouldn't be sharing the same scope.

e.g.

<script>
	import Box from './Box.svelte';
</script>

<Box>
	{#snippet children()}
		Hello
	{/snippet}
</Box>

<Box>
	{#snippet children()}
		World
	{/snippet}
</Box>

The second children snippet gives an error of 'children' has already been declared, even though each snippet should have their own respective scope.

Workaround for now is creating the snippet with a different name, and passing it as a prop to the component.

e.g.

<Box>
	{#snippet children()}
		Hello
	{/snippet}
</Box>

<Box children={children2}>
	{#snippet children2()}
		World
	{/snippet}
</Box>

Reproduction

Reproduction

Logs

index-a870eda6.js:83 CompileError: 'children' has already been declared
    at ar ([email protected]:1:228250)
    at Ys.declare ([email protected]:1:301114)
    at SnippetBlock ([email protected]:1:306484)
    at e ([email protected]:1:112918)
    at visit ([email protected]:1:112780)
    at Component ([email protected]:1:303836)
    at e ([email protected]:1:112918)
    at eval ([email protected]:1:112560)
    at Array.forEach (<anonymous>)
    at Object.next ([email protected]:1:112509)

System Info

REPL Chrome

Severity

annoyance

@mimbrown
Copy link
Contributor

mimbrown commented Dec 4, 2023

The compiled output should just be able to change from:

function children(...)

to

const children = function(...)

and the scoping will work properly.

@mimbrown
Copy link
Contributor

mimbrown commented Dec 4, 2023

Another entry in the books for "legacy JS hoisting rules are annoying"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants