Skip to content

Commit 40e6908

Browse files
committed
fix null and warning for local handlers
1 parent 2f685c1 commit 40e6908

File tree

2 files changed

+22
-13
lines changed
  • packages/svelte/src
    • compiler/phases/3-transform/client/visitors/shared
    • internal/client/dom/elements

2 files changed

+22
-13
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/events.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ export function visit_event_attribute(node, context) {
4646

4747
// When we hoist a function we assign an array with the function and all
4848
// hoisted closure params.
49-
const args = [handler, ...hoisted_params];
50-
delegated_assignment = b.array(args);
49+
if (hoisted_params) {
50+
const args = [handler, ...hoisted_params];
51+
delegated_assignment = b.array(args);
52+
} else {
53+
delegated_assignment = handler;
54+
}
5155
} else {
5256
delegated_assignment = handler;
5357
}
@@ -123,11 +127,17 @@ export function build_event_handler(node, metadata, context) {
123127
}
124128

125129
// function declared in the script
126-
if (
127-
handler.type === 'Identifier' &&
128-
context.state.scope.get(handler.name)?.declaration_kind !== 'import'
129-
) {
130-
return handler;
130+
if (handler.type === 'Identifier') {
131+
const kind = context.state.scope.get(handler.name)?.declaration_kind;
132+
if (kind === 'function') {
133+
return handler;
134+
}
135+
// local variable can be assigned directly
136+
// except in dev mode where when need $.apply()
137+
// in order to handle warnings.
138+
if (!dev && kind !== 'import') {
139+
return handler;
140+
}
131141
}
132142

133143
if (metadata.has_call) {

packages/svelte/src/internal/client/dom/elements/events.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export function handle_event_propagation(event) {
238238
var delegated = current_target['__' + event_name];
239239

240240
if (
241-
delegated !== undefined &&
241+
delegated != null &&
242242
(!(/** @type {any} */ (current_target).disabled) ||
243243
// DOM could've been updated already by the time this is reached, so we check this as well
244244
// -> the target could not have been disabled because it emits the event in the first place
@@ -311,13 +311,11 @@ export function apply(
311311
error = e;
312312
}
313313

314-
if (typeof handler === 'function') {
315-
handler.apply(element, args);
316-
} else if (has_side_effects || handler != null || error) {
314+
if (typeof handler !== 'function' && (has_side_effects || handler != null || error)) {
317315
const filename = component?.[FILENAME];
318316
const location = loc ? ` at ${filename}:${loc[0]}:${loc[1]}` : ` in ${filename}`;
319-
320-
const event_name = args[0].type;
317+
const phase = args[0]?.eventPhase < Event.BUBBLING_PHASE ? 'capture' : '';
318+
const event_name = args[0]?.type + phase;
321319
const description = `\`${event_name}\` handler${location}`;
322320
const suggestion = remove_parens ? 'remove the trailing `()`' : 'add a leading `() =>`';
323321

@@ -327,4 +325,5 @@ export function apply(
327325
throw error;
328326
}
329327
}
328+
handler?.apply(element, args);
330329
}

0 commit comments

Comments
 (0)