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: disallow sequence expressions in @const tags #11357

Merged
merged 2 commits into from
Apr 29, 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/orange-masks-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: disallow sequence expressions in `@const` tags
2 changes: 1 addition & 1 deletion packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

## const_tag_invalid_expression

> {@const ...} must be an assignment
> {@const ...} must consist of a single variable declaration

## const_tag_invalid_placement

Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,12 +729,12 @@ export function component_invalid_directive(node) {
}

/**
* {@const ...} must be an assignment
* {@const ...} must consist of a single variable declaration
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function const_tag_invalid_expression(node) {
e(node, "const_tag_invalid_expression", "{@const ...} must be an assignment");
e(node, "const_tag_invalid_expression", "{@const ...} must consist of a single variable declaration");
}

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,15 @@ function special(parser) {
parser.eat('=', true);
parser.allow_whitespace();

const expression_start = parser.index;
const init = read_expression(parser);
if (
init.type === 'SequenceExpression' &&
!parser.template.substring(expression_start, init.start).includes('(')
) {
// const a = (b, c) is allowed but a = b, c = d is not;
e.const_tag_invalid_expression(init);
}
parser.allow_whitespace();

parser.eat('}', true);
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: 'const_tag_invalid_expression',
message: '{@const ...} must consist of a single variable declaration',
position: [75, 93]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{#if true}
{@const foo = ('bar', 'baz')}
{/if}

{#if true}
{@const foo = 'foo', bar = 'bar'}
{/if}
Loading