Skip to content
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/ten-peaches-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: better support for top-level snippet declarations
Original file line number Diff line number Diff line change
Expand Up @@ -2477,7 +2477,14 @@ export const template_visitors = {
body = /** @type {import('estree').BlockStatement} */ (context.visit(node.body));
}

context.state.init.push(b.const(node.expression, b.arrow(args, body)));
const path = context.path;
// If we're top-level, then we can create a function for the snippet so that it can be referenced
// in the props declaration (default value pattern).
if (path.length === 1 && path[0].type === 'Fragment') {
context.state.init.push(b.function_declaration(node.expression, args, body));
} else {
context.state.init.push(b.const(node.expression, b.arrow(args, body)));
}
if (context.state.options.dev) {
context.state.init.push(b.stmt(b.call('$.add_snippet_symbol', node.expression)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true // Render in dev mode to check that the validation error is not thrown
},
html: `<p>hello world</p>`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
const {children = snippet} = $props();
</script>
{@render children()}
{#snippet snippet()}
<p>hello world</p>
{/snippet}