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 all 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 in snippets
11 changes: 11 additions & 0 deletions packages/svelte/src/compiler/phases/1-parse/acorn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as acorn from 'acorn';
import { walk } from 'zimmerframe';
import { tsPlugin } from 'acorn-typescript';
import { locator } from '../../state.js';

const ParserWithTS = acorn.Parser.extend(tsPlugin({ allowSatisfies: true }));

Expand Down Expand Up @@ -127,6 +128,16 @@ function amend(source, node) {
// @ts-expect-error
delete node.loc.end.index;

if (typeof node.loc?.end === 'number') {
const loc = locator(node.loc.end);
if (loc) {
node.loc.end = {
line: loc.line,
column: loc.column
};
}
}

if (/** @type {any} */ (node).typeAnnotation && node.end === undefined) {
// i think there might be a bug in acorn-typescript that prevents
// `end` from being assigned when there's a type annotation
Expand Down
47 changes: 18 additions & 29 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 @@ -268,39 +269,28 @@ function open(parser) {
e.expected_identifier(parser.index);
}

parser.eat('(', true);

parser.allow_whitespace();

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

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

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
};
}
parser.eat('(', true);

parameters.push(pattern);
let parentheses = 1;
let params = '';

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);
let function_expression = /** @type {import('estree').ArrowFunctionExpression} */ (
parse_expression_at(
parser.template.slice(0, slice_end).replace(/\S/g, ' ') + `(${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({
Expand All @@ -313,10 +303,9 @@ function open(parser) {
end: name_end,
name
},
parameters,
parameters: function_expression.params,
body: create_fragment()
});

parser.stack.push(block);
parser.fragments.push(block.body);

Expand Down
18 changes: 13 additions & 5 deletions packages/svelte/tests/parser-modern/samples/snippets/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,28 @@
"loc": {
"start": {
"line": 3,
"column": 14,
"character": 43
"column": 14
},
"end": {
"line": 3,
"column": 25,
"character": 54
"column": 25
}
},
"end": 54,
"end": 46,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 46,
"end": 54,
"loc": {
"start": {
"line": 3,
"column": 17
},
"end": {
"line": 3,
"column": 25
}
},
"typeAnnotation": {
"type": "TSStringKeyword",
"start": 48,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
"line": 6,
"column": 12
},
"end": 87
"end": {
"line": 6,
"column": 25
}
},
"name": "e",
"typeAnnotation": {
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/023'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{#snippet one(a, b = 1, c = (2, 3))}
{a}{b}{c}
{/snippet}

{#snippet two(a, b = (1, 2), c = 3)}
{a}{b}{c}
{/snippet}

{@render one(0)}/{@render two(0)}
Loading