@@ -50,6 +50,9 @@ export function Attribute(node, context) {
50
50
}
51
51
}
52
52
53
+ /** @type {DelegatedEvent } */
54
+ const unhoisted = { hoisted : false } ;
55
+
53
56
/**
54
57
* Checks if given event attribute can be delegated/hoisted and returns the corresponding info if so
55
58
* @param {string } event_name
@@ -69,15 +72,13 @@ function get_delegated_event(event_name, handler, context) {
69
72
return null ;
70
73
}
71
74
72
- /** @type {DelegatedEvent } */
73
- const non_hoistable = { hoisted : false } ;
74
75
/** @type {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression | null } */
75
76
let target_function = null ;
76
77
let binding = null ;
77
78
78
79
if ( element . metadata . has_spread ) {
79
80
// event attribute becomes part of the dynamic spread array
80
- return non_hoistable ;
81
+ return unhoisted ;
81
82
}
82
83
83
84
if ( handler . type === 'ArrowFunctionExpression' || handler . type === 'FunctionExpression' ) {
@@ -87,13 +88,13 @@ function get_delegated_event(event_name, handler, context) {
87
88
88
89
if ( context . state . analysis . module . scope . references . has ( handler . name ) ) {
89
90
// If a binding with the same name is referenced in the module scope (even if not declared there), bail out
90
- return non_hoistable ;
91
+ return unhoisted ;
91
92
}
92
93
93
94
if ( binding != null ) {
94
95
for ( const { path } of binding . references ) {
95
96
const parent = path . at ( - 1 ) ;
96
- if ( parent == null ) return non_hoistable ;
97
+ if ( parent == null ) return unhoisted ;
97
98
98
99
const grandparent = path . at ( - 2 ) ;
99
100
@@ -120,17 +121,17 @@ function get_delegated_event(event_name, handler, context) {
120
121
element . metadata . has_spread ||
121
122
! is_delegated ( event_name )
122
123
) {
123
- return non_hoistable ;
124
+ return unhoisted ;
124
125
}
125
126
} else if ( parent . type !== 'FunctionDeclaration' && parent . type !== 'VariableDeclarator' ) {
126
- return non_hoistable ;
127
+ return unhoisted ;
127
128
}
128
129
}
129
130
}
130
131
131
132
// If the binding is exported, bail out
132
133
if ( context . state . analysis . exports . find ( ( node ) => node . name === handler . name ) ) {
133
- return non_hoistable ;
134
+ return unhoisted ;
134
135
}
135
136
136
137
if ( binding !== null && binding . initial !== null && ! binding . mutated && ! binding . is_called ) {
@@ -148,23 +149,23 @@ function get_delegated_event(event_name, handler, context) {
148
149
149
150
// If we can't find a function, or the function has multiple parameters, bail out
150
151
if ( target_function == null || target_function . params . length > 1 ) {
151
- return non_hoistable ;
152
+ return unhoisted ;
152
153
}
153
154
154
155
const visited_references = new Set ( ) ;
155
156
const scope = target_function . metadata . scope ;
156
157
for ( const [ reference ] of scope . references ) {
157
158
// Bail-out if the arguments keyword is used
158
- if ( reference === 'arguments' ) return non_hoistable ;
159
+ if ( reference === 'arguments' ) return unhoisted ;
159
160
// Bail-out if references a store subscription
160
- if ( scope . get ( `$${ reference } ` ) ?. kind === 'store_sub' ) return non_hoistable ;
161
+ if ( scope . get ( `$${ reference } ` ) ?. kind === 'store_sub' ) return unhoisted ;
161
162
162
163
const binding = scope . get ( reference ) ;
163
164
const local_binding = context . state . scope . get ( reference ) ;
164
165
165
166
// If we are referencing a binding that is shadowed in another scope then bail out.
166
167
if ( local_binding !== null && binding !== null && local_binding . node !== binding . node ) {
167
- return non_hoistable ;
168
+ return unhoisted ;
168
169
}
169
170
170
171
// If we have multiple references to the same store using $ prefix, bail out.
@@ -173,11 +174,11 @@ function get_delegated_event(event_name, handler, context) {
173
174
binding . kind === 'store_sub' &&
174
175
visited_references . has ( reference . slice ( 1 ) )
175
176
) {
176
- return non_hoistable ;
177
+ return unhoisted ;
177
178
}
178
179
179
180
// If we reference the index within an each block, then bail out.
180
- if ( binding !== null && binding . initial ?. type === 'EachBlock' ) return non_hoistable ;
181
+ if ( binding !== null && binding . initial ?. type === 'EachBlock' ) return unhoisted ;
181
182
182
183
if (
183
184
binding !== null &&
@@ -191,7 +192,7 @@ function get_delegated_event(event_name, handler, context) {
191
192
binding . kind === 'legacy_reactive_import' ) &&
192
193
binding . mutated ) )
193
194
) {
194
- return non_hoistable ;
195
+ return unhoisted ;
195
196
}
196
197
visited_references . add ( reference ) ;
197
198
}
0 commit comments