Skip to content

Commit

Permalink
fix: throw on invalid attribute expressions (#11736)
Browse files Browse the repository at this point in the history
In runes mode only to prevent a breaking change solely from upgrading to Svelte 5
Closes #11734
  • Loading branch information
paoloricciuti committed May 23, 2024
1 parent ba429fd commit 5765752
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/funny-trees-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: throw on invalid attribute expressions
4 changes: 4 additions & 0 deletions packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@

> Event attribute must be a JavaScript expression, not a string
## attribute_invalid_expression

> Invalid attribute expression
## attribute_invalid_multiple

> 'multiple' attribute must be static if select uses two-way binding
Expand Down
9 changes: 9 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,15 @@ export function attribute_invalid_event_handler(node) {
e(node, "attribute_invalid_event_handler", "Event attribute must be a JavaScript expression, not a string");
}

/**
* Invalid attribute expression
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function attribute_invalid_expression(node) {
e(node, "attribute_invalid_expression", "Invalid attribute expression");
}

/**
* 'multiple' attribute must be static if select uses two-way binding
* @param {null | number | NodeLike} node
Expand Down
20 changes: 19 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import * as e from '../../errors.js';
import {
extract_identifiers,
get_parent,
is_event_attribute,
is_expression_attribute,
is_quoted_attribute,
is_text_attribute,
object,
unwrap_optional
Expand Down Expand Up @@ -58,6 +58,15 @@ function validate_component(node, context) {
}

if (attribute.type === 'Attribute') {
if (
context.state.analysis.runes &&
!is_quoted_attribute(attribute) &&
Array.isArray(attribute.value) &&
attribute.value.length > 1
) {
e.attribute_invalid_expression(attribute);
}

if (context.state.analysis.runes && is_expression_attribute(attribute)) {
const expression = attribute.value[0].expression;
if (expression.type === 'SequenceExpression') {
Expand Down Expand Up @@ -107,6 +116,15 @@ function validate_element(node, context) {
if (attribute.type === 'Attribute') {
const is_expression = is_expression_attribute(attribute);

if (
context.state.analysis.runes &&
!is_quoted_attribute(attribute) &&
Array.isArray(attribute.value) &&
attribute.value.length > 1
) {
e.attribute_invalid_expression(attribute);
}

if (context.state.analysis.runes && is_expression) {
const expression = attribute.value[0].expression;
if (expression.type === 'SequenceExpression') {
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte/src/compiler/utils/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ export function is_expression_attribute(attribute) {
);
}

/**
* Returns true if the attribute is quoted.
* @param {import('#compiler').Attribute} attribute
* @returns {attribute is import('#compiler').Attribute & { value: [import('#compiler').ExpressionTag] }}
*/
export function is_quoted_attribute(attribute) {
if (attribute.value === true) return false;
return attribute.value.at(-1)?.end !== attribute.end;
}

/**
* Returns true if the attribute starts with `on` and contains a single expression node.
* @param {import('#compiler').Attribute} attribute
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: 'attribute_invalid_expression',
message: 'Invalid attribute expression',
position: [101, 116]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<svelte:options runes />
<script>
import Component from "./Component.svelte";
</script>

<Component onclick={true}} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../test';

export default test({
error: {
code: 'attribute_invalid_expression',
message: 'Invalid attribute expression',
position: [34, 71]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<svelte:options runes />
<button
onclick={() => console.log('hello')}}
>
click
</button>

0 comments on commit 5765752

Please sign in to comment.