Skip to content

Commit a6a8339

Browse files
committed
treat let handler = () => {...} the same as function handler() {...}
1 parent 2671dc6 commit a6a8339

File tree

2 files changed

+20
-3
lines changed
  • packages/svelte/src/compiler/phases

2 files changed

+20
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,16 @@ export function build_event_handler(node, metadata, context) {
128128

129129
// function declared in the script
130130
if (handler.type === 'Identifier') {
131-
const kind = context.state.scope.get(handler.name)?.declaration_kind;
132-
if (kind === 'function') {
131+
const binding = context.state.scope.get(handler.name);
132+
133+
if (binding?.is_function()) {
133134
return handler;
134135
}
136+
135137
// local variable can be assigned directly
136138
// except in dev mode where when need $.apply()
137139
// in order to handle warnings.
138-
if (!dev && kind !== 'import') {
140+
if (!dev && binding?.declaration_kind !== 'import') {
139141
return handler;
140142
}
141143
}

packages/svelte/src/compiler/phases/scope.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ export class Binding {
7979
get updated() {
8080
return this.mutated || this.reassigned;
8181
}
82+
83+
is_function() {
84+
if (this.reassigned) {
85+
// even if it's reassigned to another function,
86+
// we can't use it directly as e.g. an event handler
87+
return false;
88+
}
89+
90+
if (this.declaration_kind === 'function') {
91+
return true;
92+
}
93+
94+
const type = this.initial?.type;
95+
return type === 'ArrowFunctionExpression' || type === 'FunctionExpression';
96+
}
8297
}
8398

8499
export class Scope {

0 commit comments

Comments
 (0)