fix: disallow sequence expressions in `@const` tags (#11357)

* fix: disallow sequence expressions in `@const` tags

closes #11349

* allow parenthesized sequence expression
pull/11385/head
Simon H 1 year ago committed by GitHub
parent 0cf10a361e
commit 5e0845fe3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: disallow sequence expressions in `@const` tags

@ -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

@ -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");
}
/**

@ -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);

@ -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}
Loading…
Cancel
Save