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:
parent
ce7abe4471
commit
8e696b0817
5
.changeset/polite-ways-serve.md
Normal file
5
.changeset/polite-ways-serve.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
fix: throw compilation error for malformed snippets
|
@ -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({
|
||||
|
@ -0,0 +1,9 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
error: {
|
||||
code: 'expected_token',
|
||||
message: 'Expected token )',
|
||||
position: [31, 31]
|
||||
}
|
||||
});
|
@ -0,0 +1 @@
|
||||
{#snippet children(hi{/snippet}
|
@ -0,0 +1,9 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
error: {
|
||||
code: 'expected_token',
|
||||
message: 'Expected token }',
|
||||
position: [20, 20]
|
||||
}
|
||||
});
|
@ -0,0 +1 @@
|
||||
{#snippet children()hi{/snippet}
|
Loading…
Reference in New Issue
Block a user