Skip to content

Commit 63f82c9

Browse files
committed
fix: handle multiple snippet parameters with one or more being optional
fixes #10825
1 parent f8c85d5 commit 63f82c9

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

.changeset/slow-plums-chew.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: handle multiple snippet parameters with one or more being optional

packages/svelte/src/compiler/phases/1-parse/read/context.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ function read_type_annotation(parser, optional_allowed = false) {
121121
const template =
122122
parser.template.slice(0, a).replace(/[^\n]/g, ' ') +
123123
insert +
124-
parser.template.slice(parser.index);
124+
// If this is a type annotation for a function parameter, Acorn-TS will treat subsequent
125+
// parameters as part of a sequence expression instead, and will then error on optional
126+
// parameters (`?:`). Therefore replace that sequence with something that will not error.
127+
parser.template.slice(parser.index).replace(/\?\s*:/g, ':');
125128
let expression = parse_expression_at(template, parser.ts, a);
126129

127130
// `foo: bar = baz` gets mangled — fix it
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test } from '../../test';
22

33
export default test({
4-
html: '1 2 3 4 5'
4+
html: '1 2 3 4 5 6a'
55
});

packages/svelte/tests/runtime-runes/samples/snippet-typescript/main.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
{#snippet counter5(c?: number = 5)}
1717
{c}
1818
{/snippet}
19+
{#snippet counter6(c?: number, d?: string)}
20+
{c}{d}
21+
{/snippet}
1922

2023
{@render counter1(1)}
2124
{@render counter2({ c: 2 })}
2225
{@render counter3(3)}
2326
{@render counter4()}
2427
{@render counter5()}
28+
{@render counter6(6, 'a')}

0 commit comments

Comments
 (0)