Skip to content

fix: correctly validate undefined snippet params with default value #15750

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Merged
merged 4 commits into from
Apr 12, 2025
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/angry-mayflies-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: correctly validate `undefined` snippet params with default value
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export function SnippetBlock(node, context) {
/** @type {Statement[]} */
const declarations = [];

if (dev) {
declarations.push(b.stmt(b.call('$.validate_snippet_args', b.spread(b.id('arguments')))));
}

const transform = { ...context.state.transform };
const child_state = { ...context.state, transform };

Expand All @@ -30,12 +34,7 @@ export function SnippetBlock(node, context) {
if (!argument) continue;

if (argument.type === 'Identifier') {
args.push({
type: 'AssignmentPattern',
left: argument,
right: b.id('$.noop')
});

args.push(b.assignment_pattern(argument, b.id('$.noop')));
transform[argument.name] = { read: b.call };

continue;
Expand Down Expand Up @@ -66,29 +65,16 @@ export function SnippetBlock(node, context) {
}
}
}
if (dev) {
declarations.unshift(
b.stmt(
b.call(
'$.validate_snippet_args',
.../** @type {Identifier[]} */ (
args.map((arg) => (arg?.type === 'Identifier' ? arg : arg?.left))
)
)
)
);
}

body = b.block([
...declarations,
.../** @type {BlockStatement} */ (context.visit(node.body, child_state)).body
]);

/** @type {Expression} */
let snippet = b.arrow(args, body);

if (dev) {
snippet = b.call('$.wrap_snippet', b.id(context.state.analysis.name), snippet);
}
// in dev we use a FunctionExpression (not arrow function) so we can use `arguments`
let snippet = dev
? b.call('$.wrap_snippet', b.id(context.state.analysis.name), b.function(null, args, body))
: b.arrow(args, body);

const declaration = b.const(node.expression, snippet);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: `<p>default</p>`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{#snippet test(param = "default")}
<p>{param}</p>
{/snippet}

{@render test()}