Skip to content

Commit 69a4275

Browse files
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 <[email protected]>
1 parent 3d6da41 commit 69a4275

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

.changeset/angry-mayflies-matter.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: correctly validate `undefined` snippet params with default value

packages/svelte/src/compiler/phases/3-transform/client/visitors/SnippetBlock.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export function SnippetBlock(node, context) {
2121
/** @type {Statement[]} */
2222
const declarations = [];
2323

24+
if (dev) {
25+
declarations.push(b.stmt(b.call('$.validate_snippet_args', b.spread(b.id('arguments')))));
26+
}
27+
2428
const transform = { ...context.state.transform };
2529
const child_state = { ...context.state, transform };
2630

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

3236
if (argument.type === 'Identifier') {
33-
args.push({
34-
type: 'AssignmentPattern',
35-
left: argument,
36-
right: b.id('$.noop')
37-
});
38-
37+
args.push(b.assignment_pattern(argument, b.id('$.noop')));
3938
transform[argument.name] = { read: b.call };
4039

4140
continue;
@@ -66,29 +65,16 @@ export function SnippetBlock(node, context) {
6665
}
6766
}
6867
}
69-
if (dev) {
70-
declarations.unshift(
71-
b.stmt(
72-
b.call(
73-
'$.validate_snippet_args',
74-
.../** @type {Identifier[]} */ (
75-
args.map((arg) => (arg?.type === 'Identifier' ? arg : arg?.left))
76-
)
77-
)
78-
)
79-
);
80-
}
68+
8169
body = b.block([
8270
...declarations,
8371
.../** @type {BlockStatement} */ (context.visit(node.body, child_state)).body
8472
]);
8573

86-
/** @type {Expression} */
87-
let snippet = b.arrow(args, body);
88-
89-
if (dev) {
90-
snippet = b.call('$.wrap_snippet', b.id(context.state.analysis.name), snippet);
91-
}
74+
// in dev we use a FunctionExpression (not arrow function) so we can use `arguments`
75+
let snippet = dev
76+
? b.call('$.wrap_snippet', b.id(context.state.analysis.name), b.function(null, args, body))
77+
: b.arrow(args, body);
9278

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `<p>default</p>`
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{#snippet test(param = "default")}
2+
<p>{param}</p>
3+
{/snippet}
4+
5+
{@render test()}

0 commit comments

Comments
 (0)