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>
pull/12155/head
Paolo Ricciuti 6 months ago committed by GitHub
parent ce7abe4471
commit 8e696b0817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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); e.expected_identifier(parser.index);
} }
let slice_end = parser.index; const params_start = parser.index;
parser.eat('(', true); parser.eat('(', true);
let parentheses = 1; 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++;
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} */ ( let function_expression = /** @type {import('estree').ArrowFunctionExpression} */ (
parse_expression_at( parse_expression_at(prelude + `${params} => {}`, parser.ts, params_start)
parser.template.slice(0, slice_end).replace(/\S/g, ' ') + `(${params}) => {}`,
parser.ts,
0
)
); );
parser.index += 2; parser.eat('}', true);
/** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>>} */ /** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>>} */
const block = parser.append({ 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,9 @@
import { test } from '../../test';
export default test({
error: {
code: 'expected_token',
message: 'Expected token }',
position: [20, 20]
}
});
Loading…
Cancel
Save