Skip to content

Commit 1c0e240

Browse files
authored
chore: reuse is_function helper (#15467)
1 parent e2bbc56 commit 1c0e240

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,8 @@ function get_delegated_event(event_name, handler, context) {
162162
return unhoisted;
163163
}
164164

165-
if (binding !== null && binding.initial !== null && !binding.updated) {
166-
const binding_type = binding.initial.type;
167-
168-
if (
169-
binding_type === 'ArrowFunctionExpression' ||
170-
binding_type === 'FunctionDeclaration' ||
171-
binding_type === 'FunctionExpression'
172-
) {
173-
target_function = binding.initial;
174-
}
165+
if (binding?.is_function()) {
166+
target_function = binding.initial;
175167
}
176168
}
177169

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { ClassDeclaration, Expression, FunctionDeclaration, Identifier, ImportDeclaration, MemberExpression, Node, Pattern, VariableDeclarator } from 'estree' */
1+
/** @import { ArrowFunctionExpression, ClassDeclaration, Expression, FunctionDeclaration, FunctionExpression, Identifier, ImportDeclaration, MemberExpression, Node, Pattern, VariableDeclarator } from 'estree' */
22
/** @import { Context, Visitor } from 'zimmerframe' */
33
/** @import { AST, BindingKind, DeclarationKind } from '#compiler' */
44
import is_reference from 'is-reference';
@@ -80,19 +80,23 @@ export class Binding {
8080
return this.mutated || this.reassigned;
8181
}
8282

83+
/**
84+
* @returns {this is Binding & { initial: ArrowFunctionExpression | FunctionDeclaration | FunctionExpression }}
85+
*/
8386
is_function() {
84-
if (this.reassigned) {
87+
if (this.updated) {
8588
// even if it's reassigned to another function,
8689
// we can't use it directly as e.g. an event handler
8790
return false;
8891
}
8992

90-
if (this.declaration_kind === 'function') {
91-
return true;
92-
}
93-
9493
const type = this.initial?.type;
95-
return type === 'ArrowFunctionExpression' || type === 'FunctionExpression';
94+
95+
return (
96+
type === 'ArrowFunctionExpression' ||
97+
type === 'FunctionExpression' ||
98+
type === 'FunctionDeclaration'
99+
);
96100
}
97101
}
98102

0 commit comments

Comments
 (0)