Skip to content

Commit 696a4b3

Browse files
trueadmRich-Harris
andauthored
fix: improve compiler attribute validation logic (#12081)
prevent misidentification of bindings as delegatable event handlers if used outside event attribute Fixes #12074 --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 69ee547 commit 696a4b3

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

.changeset/hot-rivers-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: prevent misidentification of bindings as delegatable event handlers if used outside event attribute

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,24 @@ function get_delegated_event(event_name, handler, context) {
111111
if (binding != null) {
112112
for (const { path } of binding.references) {
113113
const parent = path.at(-1);
114-
if (parent == null) {
115-
return non_hoistable;
116-
}
114+
if (parent == null) return non_hoistable;
115+
116+
const grandparent = path.at(-2);
117117

118118
/** @type {import('#compiler').RegularElement | null} */
119119
let element = null;
120120
/** @type {string | null} */
121121
let event_name = null;
122122
if (parent.type === 'OnDirective') {
123-
element = /** @type {import('#compiler').RegularElement} */ (path.at(-2));
123+
element = /** @type {import('#compiler').RegularElement} */ (grandparent);
124124
event_name = parent.name;
125125
} else if (
126126
parent.type === 'ExpressionTag' &&
127-
is_event_attribute(/** @type {import('#compiler').Attribute} */ (path.at(-2)))
127+
grandparent?.type === 'Attribute' &&
128+
is_event_attribute(grandparent)
128129
) {
129130
element = /** @type {import('#compiler').RegularElement} */ (path.at(-3));
130-
const attribute = /** @type {import('#compiler').Attribute} */ (path.at(-2));
131+
const attribute = /** @type {import('#compiler').Attribute} */ (grandparent);
131132
event_name = get_attribute_event_name(attribute.name);
132133
}
133134

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
test() {
5+
// Compiler shouldn't error
6+
}
7+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
function f() {}
3+
</script>
4+
5+
<input onchange={f}>{f}

0 commit comments

Comments
 (0)