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: throw compilation error for malformed snippets #12144

Merged
merged 3 commits into from
Jun 23, 2024
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/polite-ways-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: throw compilation error for malformed snippets
21 changes: 10 additions & 11 deletions packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../test';

export default test({
error: {
code: 'expected_token',
message: 'Expected token )',
position: [31, 31]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{#snippet children(hi{/snippet}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../test';

export default test({
error: {
code: 'expected_token',
message: 'Expected token }',
position: [20, 20]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{#snippet children()hi{/snippet}
Loading