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/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()}