Skip to content

Commit

Permalink
fix: disallow sequence expressions in @const tags (#11357)
Browse files Browse the repository at this point in the history
* fix: disallow sequence expressions in `@const` tags

closes #11349

* allow parenthesized sequence expression
  • Loading branch information
dummdidumm committed Apr 29, 2024
1 parent 0cf10a3 commit 5e0845f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-masks-exercise.md
@@ -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
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
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
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
@@ -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]
}
});
@@ -0,0 +1,7 @@
{#if true}
{@const foo = ('bar', 'baz')}
{/if}

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

0 comments on commit 5e0845f

Please sign in to comment.