Skip to content

Commit ef158ff

Browse files
authored
fix: apply event attribute validation to elements only (#9772)
fixes #9755
1 parent ede5dab commit ef158ff

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

.changeset/spicy-plums-admire.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: apply event attribute validation to elements only

packages/svelte/src/compiler/phases/2-analyze/validation.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,24 @@ function validate_element(node, context) {
6565
error(attribute, 'invalid-attribute-name', attribute.name);
6666
}
6767

68-
if (attribute.name === 'is' && context.state.options.namespace !== 'foreign') {
69-
warn(context.state.analysis.warnings, attribute, context.path, 'avoid-is');
70-
} else if (attribute.name === 'slot') {
68+
if (attribute.name.startsWith('on') && attribute.name.length > 2) {
69+
if (
70+
attribute.value === true ||
71+
is_text_attribute(attribute) ||
72+
attribute.value.length > 1
73+
) {
74+
error(attribute, 'invalid-event-attribute-value');
75+
}
76+
}
77+
78+
if (attribute.name === 'slot') {
7179
/** @type {import('#compiler').RegularElement | import('#compiler').SvelteElement | import('#compiler').Component | import('#compiler').SvelteComponent | import('#compiler').SvelteSelf | undefined} */
7280
validate_slot_attribute(context, attribute);
7381
}
82+
83+
if (attribute.name === 'is' && context.state.options.namespace !== 'foreign') {
84+
warn(context.state.analysis.warnings, attribute, context.path, 'avoid-is');
85+
}
7486
} else if (attribute.type === 'AnimateDirective') {
7587
const parent = context.path.at(-2);
7688
if (parent?.type !== 'EachBlock') {
@@ -316,13 +328,6 @@ function is_tag_valid_with_parent(tag, parent_tag) {
316328
* @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, import('./types.js').AnalysisState>}
317329
*/
318330
export const validation = {
319-
Attribute(node) {
320-
if (node.name.startsWith('on') && node.name.length > 2) {
321-
if (node.value === true || is_text_attribute(node) || node.value.length > 1) {
322-
error(node, 'invalid-event-attribute-value');
323-
}
324-
}
325-
},
326331
BindDirective(node, context) {
327332
validate_no_const_assignment(node, node.expression, context.state.scope, true);
328333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"message": "Event attribute must be a JavaScript expression, not a string",
4+
"code": "invalid-event-attribute-value",
5+
"start": {
6+
"line": 4,
7+
"column": 8
8+
},
9+
"end": {
10+
"line": 4,
11+
"column": 21
12+
}
13+
}
14+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<button onclick={() => {}}>ok</button>
2+
<Button onclick onbar="bar">ok</Button>
3+
4+
<button onclick="foo">not ok</button>

0 commit comments

Comments
 (0)