diff --git a/.changeset/polite-ways-serve.md b/.changeset/polite-ways-serve.md new file mode 100644 index 0000000000..fe25761069 --- /dev/null +++ b/.changeset/polite-ways-serve.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: throw compilation error for malformed snippets diff --git a/packages/svelte/src/compiler/phases/1-parse/state/tag.js b/packages/svelte/src/compiler/phases/1-parse/state/tag.js index 85fde1d486..762dab69bb 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/tag.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/tag.js @@ -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>} */ const block = parser.append({ diff --git a/packages/svelte/tests/compiler-errors/samples/malformed-snippet-2/_config.js b/packages/svelte/tests/compiler-errors/samples/malformed-snippet-2/_config.js new file mode 100644 index 0000000000..73154e8971 --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/malformed-snippet-2/_config.js @@ -0,0 +1,9 @@ +import { test } from '../../test'; + +export default test({ + error: { + code: 'expected_token', + message: 'Expected token )', + position: [31, 31] + } +}); diff --git a/packages/svelte/tests/compiler-errors/samples/malformed-snippet-2/main.svelte b/packages/svelte/tests/compiler-errors/samples/malformed-snippet-2/main.svelte new file mode 100644 index 0000000000..2cde495cb4 --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/malformed-snippet-2/main.svelte @@ -0,0 +1 @@ +{#snippet children(hi{/snippet} diff --git a/packages/svelte/tests/compiler-errors/samples/malformed-snippet/_config.js b/packages/svelte/tests/compiler-errors/samples/malformed-snippet/_config.js new file mode 100644 index 0000000000..393d1542d4 --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/malformed-snippet/_config.js @@ -0,0 +1,9 @@ +import { test } from '../../test'; + +export default test({ + error: { + code: 'expected_token', + message: 'Expected token }', + position: [20, 20] + } +}); diff --git a/packages/svelte/tests/compiler-errors/samples/malformed-snippet/main.svelte b/packages/svelte/tests/compiler-errors/samples/malformed-snippet/main.svelte new file mode 100644 index 0000000000..3ac8f31f14 --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/malformed-snippet/main.svelte @@ -0,0 +1 @@ +{#snippet children()hi{/snippet}