0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-24 16:29:46 +01:00

fix: throw compilation error for malformed snippets (#12144)

* fix: throw compilation error for malformed snippets

* fix: support multiline

* tweak

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
This commit is contained in:
Paolo Ricciuti 2024-06-23 18:11:37 +02:00 committed by GitHub
parent ce7abe4471
commit 8e696b0817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 35 additions and 11 deletions

View File

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: throw compilation error for malformed snippets

View File

@ -269,28 +269,27 @@ function open(parser) {
e.expected_identifier(parser.index);
}
let slice_end = parser.index;
const params_start = parser.index;
parser.eat('(', true);
let parentheses = 1;
let params = '';
while (!parser.match(')') || parentheses !== 1) {
while (parser.index < parser.template.length && (!parser.match(')') || parentheses !== 1)) {
if (parser.match('(')) parentheses++;
if (parser.match(')')) parentheses--;
params += parser.read(/^./);
parser.index += 1;
}
parser.eat(')', true);
const prelude = parser.template.slice(0, params_start).replace(/\S/g, ' ');
const params = parser.template.slice(params_start, parser.index);
let function_expression = /** @type {import('estree').ArrowFunctionExpression} */ (
parse_expression_at(
parser.template.slice(0, slice_end).replace(/\S/g, ' ') + `(${params}) => {}`,
parser.ts,
0
)
parse_expression_at(prelude + `${params} => {}`, parser.ts, params_start)
);
parser.index += 2;
parser.eat('}', true);
/** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>>} */
const block = parser.append({

View File

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
error: {
code: 'expected_token',
message: 'Expected token )',
position: [31, 31]
}
});

View File

@ -0,0 +1 @@
{#snippet children(hi{/snippet}

View File

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
error: {
code: 'expected_token',
message: 'Expected token }',
position: [20, 20]
}
});

View File

@ -0,0 +1 @@
{#snippet children()hi{/snippet}