Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow multiple optional parameters with defaults #12070

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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/ten-geese-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: allow multiple optional parameters with defaults
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
75 changes: 31 additions & 44 deletions packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import read_expression from '../read/expression.js';
import * as e from '../../../errors.js';
import { create_fragment } from '../utils/create.js';
import { walk } from 'zimmerframe';
import { parse_expression_at } from '../acorn.js';

const regex_whitespace_with_closing_curly_brace = /^\s*}/;

Expand Down Expand Up @@ -270,55 +271,41 @@ function open(parser) {

parser.eat('(', true);

parser.allow_whitespace();

/** @type {import('estree').Pattern[]} */
const parameters = [];

while (!parser.match(')')) {
let pattern = read_pattern(parser, true);
let parentheses = 1;
let params = '';

parser.allow_whitespace();
if (parser.eat('=')) {
parser.allow_whitespace();
const right = read_expression(parser);
pattern = {
type: 'AssignmentPattern',
left: pattern,
right: right,
start: pattern.start,
end: right.end
};
}

parameters.push(pattern);

if (!parser.eat(',')) break;
parser.allow_whitespace();
while (!parser.match(')') || parentheses !== 1) {
if (parser.match('(')) parentheses++;
if (parser.match(')')) parentheses--;
params += parser.read(/^./);
Comment on lines +279 to +282
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a new bug, but this logic probably won't be sufficient because these characters could appear inside strings etc. we should probably fix it both here and inside read_pattern...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh right...would prevent the plus and minus between strings be sufficient? It should right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings, comments and regular expressions

}

parser.eat(')', true);
const function_expression = parse_expression_at(`function ${name}(${params}){}`, parser.ts, 0);

parser.allow_whitespace();
parser.eat('}', true);
parser.index += 2;

/** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>>} */
const block = parser.append({
type: 'SnippetBlock',
start,
end: -1,
expression: {
type: 'Identifier',
start: name_start,
end: name_end,
name
},
parameters,
body: create_fragment()
});

parser.stack.push(block);
parser.fragments.push(block.body);
if (function_expression.type === 'FunctionExpression') {
/** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>>} */
const block = parser.append({
type: 'SnippetBlock',
start,
end: -1,
expression: {
type: 'Identifier',
start: name_start,
end: name_end,
name
},
parameters: function_expression.params.map((param) => {
param.start += start + 1;
param.end += start + 1;
return param;
}),
body: create_fragment()
});
parser.stack.push(block);
parser.fragments.push(block.body);
}

return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: '013'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{#snippet snip(a, b = 1, c = (2, 3))}
{a}{b}{c}
{/snippet}

{@render snip(0)}
Loading