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

Lines changed: 2 additions & 10 deletions
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

Lines changed: 11 additions & 7 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy