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: apply event attribute validation to elements only #9772

Merged
merged 1 commit into from
Dec 5, 2023
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/spicy-plums-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: apply event attribute validation to elements only
25 changes: 15 additions & 10 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,24 @@ function validate_element(node, context) {
error(attribute, 'invalid-attribute-name', attribute.name);
}

if (attribute.name === 'is' && context.state.options.namespace !== 'foreign') {
warn(context.state.analysis.warnings, attribute, context.path, 'avoid-is');
} else if (attribute.name === 'slot') {
if (attribute.name.startsWith('on') && attribute.name.length > 2) {
if (
attribute.value === true ||
is_text_attribute(attribute) ||
attribute.value.length > 1
) {
error(attribute, 'invalid-event-attribute-value');
}
}

if (attribute.name === 'slot') {
/** @type {import('#compiler').RegularElement | import('#compiler').SvelteElement | import('#compiler').Component | import('#compiler').SvelteComponent | import('#compiler').SvelteSelf | undefined} */
validate_slot_attribute(context, attribute);
}

if (attribute.name === 'is' && context.state.options.namespace !== 'foreign') {
warn(context.state.analysis.warnings, attribute, context.path, 'avoid-is');
}
} else if (attribute.type === 'AnimateDirective') {
const parent = context.path.at(-2);
if (parent?.type !== 'EachBlock') {
Expand Down Expand Up @@ -316,13 +328,6 @@ function is_tag_valid_with_parent(tag, parent_tag) {
* @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, import('./types.js').AnalysisState>}
*/
export const validation = {
Attribute(node) {
if (node.name.startsWith('on') && node.name.length > 2) {
if (node.value === true || is_text_attribute(node) || node.value.length > 1) {
error(node, 'invalid-event-attribute-value');
}
}
},
BindDirective(node, context) {
validate_no_const_assignment(node, node.expression, context.state.scope, true);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"message": "Event attribute must be a JavaScript expression, not a string",
"code": "invalid-event-attribute-value",
"start": {
"line": 4,
"column": 8
},
"end": {
"line": 4,
"column": 21
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<button onclick={() => {}}>ok</button>
<Button onclick onbar="bar">ok</Button>

<button onclick="foo">not ok</button>