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

* fix: correctly validate `undefined` snippet params with default value

* use arguments

* unused

* drive-by

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/15751/head
Paolo Ricciuti 5 months ago committed by GitHub
parent 3d6da41b1d
commit 69a427518d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: correctly validate `undefined` snippet params with default value

@ -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 };
@ -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;
@ -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);

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: `<p>default</p>`
});

@ -0,0 +1,5 @@
{#snippet test(param = "default")}
<p>{param}</p>
{/snippet}
{@render test()}
Loading…
Cancel
Save