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/few-horses-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: use function declaration for snippets in server output to avoid TDZ violation
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @import { ArrowFunctionExpression, BlockStatement, CallExpression } from 'estree' */
/** @import { BlockStatement } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types.js' */
import { dev } from '../../../../state.js';
Expand All @@ -9,27 +9,21 @@ import * as b from '#compiler/builders';
* @param {ComponentContext} context
*/
export function SnippetBlock(node, context) {
const body = /** @type {BlockStatement} */ (context.visit(node.body));
let fn = b.function_declaration(
node.expression,
[b.id('$$payload'), ...node.parameters],
/** @type {BlockStatement} */ (context.visit(node.body))
);

if (dev) {
body.body.unshift(b.stmt(b.call('$.validate_snippet_args', b.id('$$payload'))));
}
// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
fn.___snippet = true;

/** @type {ArrowFunctionExpression | CallExpression} */
let fn = b.arrow([b.id('$$payload'), ...node.parameters], body);
const statements = node.metadata.can_hoist ? context.state.hoisted : context.state.init;

if (dev) {
fn = b.call('$.prevent_snippet_stringification', fn);
fn.body.body.unshift(b.stmt(b.call('$.validate_snippet_args', b.id('$$payload'))));
statements.push(b.stmt(b.call('$.prevent_snippet_stringification', fn.id)));
}

const declaration = b.declaration('const', [b.declarator(node.expression, fn)]);

// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
fn.___snippet = true;

if (node.metadata.can_hoist) {
context.state.hoisted.push(declaration);
} else {
context.state.init.push(declaration);
}
statements.push(fn);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function fn(snippet) {
return snippet;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import { fn } from "./fn.js";
let variable = $state("var");

fn(test);
</script>

{#snippet test()}
{variable}
{/snippet}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},

error: 'invalid_snippet_arguments'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
test();
</script>

{#snippet test()}
<p>hello</p>
{/snippet}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as $ from 'svelte/internal/server';
import TextInput from './Child.svelte';

const snippet = ($$payload) => {
function snippet($$payload) {
$$payload.out += `<!---->Something`;
};
}

export default function Bind_component_snippet($$payload) {
let value = '';
Expand Down