fix: handle multiple snippet parameters with one or more being optional (#10833)

fixes #10825

Co-authored-by: Dominic Gannaway <dg@domgan.com>
pull/10869/head
Simon H 1 year ago committed by GitHub
parent 3eef1cb8cf
commit b468978e4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: handle multiple snippet parameters with one or more being optional

@ -121,7 +121,10 @@ function read_type_annotation(parser, optional_allowed = false) {
const template =
parser.template.slice(0, a).replace(/[^\n]/g, ' ') +
insert +
parser.template.slice(parser.index);
// If this is a type annotation for a function parameter, Acorn-TS will treat subsequent
// parameters as part of a sequence expression instead, and will then error on optional
// parameters (`?:`). Therefore replace that sequence with something that will not error.
parser.template.slice(parser.index).replace(/\?\s*:/g, ':');
let expression = parse_expression_at(template, parser.ts, a);
// `foo: bar = baz` gets mangled — fix it

@ -1,5 +1,5 @@
import { test } from '../../test';
export default test({
html: '1 2 3 4 5'
html: '1 2 3 4 5 6a'
});

@ -16,9 +16,13 @@
{#snippet counter5(c?: number = 5)}
{c}
{/snippet}
{#snippet counter6(c?: number, d?: string)}
{c}{d}
{/snippet}
{@render counter1(1)}
{@render counter2({ c: 2 })}
{@render counter3(3)}
{@render counter4()}
{@render counter5()}
{@render counter6(6, 'a')}

Loading…
Cancel
Save