Skip to content

Commit 27d5942

Browse files
committed
hoist non_hoistable, rename
1 parent fad193a commit 27d5942

File tree

1 file changed

+16
-15
lines changed
  • packages/svelte/src/compiler/phases/2-analyze/visitors

1 file changed

+16
-15
lines changed

packages/svelte/src/compiler/phases/2-analyze/visitors/Attribute.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export function Attribute(node, context) {
5050
}
5151
}
5252

53+
/** @type {DelegatedEvent} */
54+
const unhoisted = { hoisted: false };
55+
5356
/**
5457
* Checks if given event attribute can be delegated/hoisted and returns the corresponding info if so
5558
* @param {string} event_name
@@ -69,15 +72,13 @@ function get_delegated_event(event_name, handler, context) {
6972
return null;
7073
}
7174

72-
/** @type {DelegatedEvent} */
73-
const non_hoistable = { hoisted: false };
7475
/** @type {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression | null} */
7576
let target_function = null;
7677
let binding = null;
7778

7879
if (element.metadata.has_spread) {
7980
// event attribute becomes part of the dynamic spread array
80-
return non_hoistable;
81+
return unhoisted;
8182
}
8283

8384
if (handler.type === 'ArrowFunctionExpression' || handler.type === 'FunctionExpression') {
@@ -87,13 +88,13 @@ function get_delegated_event(event_name, handler, context) {
8788

8889
if (context.state.analysis.module.scope.references.has(handler.name)) {
8990
// 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;
9192
}
9293

9394
if (binding != null) {
9495
for (const { path } of binding.references) {
9596
const parent = path.at(-1);
96-
if (parent == null) return non_hoistable;
97+
if (parent == null) return unhoisted;
9798

9899
const grandparent = path.at(-2);
99100

@@ -120,17 +121,17 @@ function get_delegated_event(event_name, handler, context) {
120121
element.metadata.has_spread ||
121122
!is_delegated(event_name)
122123
) {
123-
return non_hoistable;
124+
return unhoisted;
124125
}
125126
} else if (parent.type !== 'FunctionDeclaration' && parent.type !== 'VariableDeclarator') {
126-
return non_hoistable;
127+
return unhoisted;
127128
}
128129
}
129130
}
130131

131132
// If the binding is exported, bail out
132133
if (context.state.analysis.exports.find((node) => node.name === handler.name)) {
133-
return non_hoistable;
134+
return unhoisted;
134135
}
135136

136137
if (binding !== null && binding.initial !== null && !binding.mutated && !binding.is_called) {
@@ -148,23 +149,23 @@ function get_delegated_event(event_name, handler, context) {
148149

149150
// If we can't find a function, or the function has multiple parameters, bail out
150151
if (target_function == null || target_function.params.length > 1) {
151-
return non_hoistable;
152+
return unhoisted;
152153
}
153154

154155
const visited_references = new Set();
155156
const scope = target_function.metadata.scope;
156157
for (const [reference] of scope.references) {
157158
// Bail-out if the arguments keyword is used
158-
if (reference === 'arguments') return non_hoistable;
159+
if (reference === 'arguments') return unhoisted;
159160
// 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;
161162

162163
const binding = scope.get(reference);
163164
const local_binding = context.state.scope.get(reference);
164165

165166
// If we are referencing a binding that is shadowed in another scope then bail out.
166167
if (local_binding !== null && binding !== null && local_binding.node !== binding.node) {
167-
return non_hoistable;
168+
return unhoisted;
168169
}
169170

170171
// 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) {
173174
binding.kind === 'store_sub' &&
174175
visited_references.has(reference.slice(1))
175176
) {
176-
return non_hoistable;
177+
return unhoisted;
177178
}
178179

179180
// 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;
181182

182183
if (
183184
binding !== null &&
@@ -191,7 +192,7 @@ function get_delegated_event(event_name, handler, context) {
191192
binding.kind === 'legacy_reactive_import') &&
192193
binding.mutated))
193194
) {
194-
return non_hoistable;
195+
return unhoisted;
195196
}
196197
visited_references.add(reference);
197198
}

0 commit comments

Comments
 (0)