@@ -67,6 +67,18 @@ function basename(filename: string | undefined): string | undefined {
67
67
68
68
const DefaultExportSymbol = Symbol ( "DefaultExportSymbol" ) ;
69
69
70
+ function getObjectPropertyKey (
71
+ node : BabelTypes . ObjectProperty | BabelTypes . ObjectMethod
72
+ ) : string | null {
73
+ if ( node . key . type === "Identifier" ) {
74
+ return node . key . name ;
75
+ } else if ( node . key . type === "StringLiteral" ) {
76
+ return node . key . value ;
77
+ }
78
+
79
+ return null ;
80
+ }
81
+
70
82
/**
71
83
* If the function node has a name (i.e. is a function declaration with a
72
84
* name), return that. Else return null.
@@ -75,11 +87,7 @@ function getFunctionNodeName(path: NodePath<FunctionLike>): string | null {
75
87
if ( path . node . type === "FunctionDeclaration" && path . node . id ) {
76
88
return path . node . id . name ;
77
89
} else if ( path . node . type === "ObjectMethod" ) {
78
- if ( path . node . key . type === "Identifier" ) {
79
- return path . node . key . name ;
80
- } else if ( path . node . key . type === "StringLiteral" ) {
81
- return path . node . key . value ;
82
- }
90
+ return getObjectPropertyKey ( path . node ) ;
83
91
}
84
92
85
93
return null ;
@@ -122,6 +130,8 @@ function getFunctionNameFromParent(
122
130
} else {
123
131
return null ;
124
132
}
133
+ } else if ( parentPath . node . type === "ObjectProperty" ) {
134
+ return getObjectPropertyKey ( parentPath . node ) ;
125
135
} else if ( parentPath . node . type === "ExportDefaultDeclaration" ) {
126
136
return DefaultExportSymbol ;
127
137
} else if (
@@ -150,10 +160,10 @@ function getFunctionName(
150
160
return getFunctionNameFromParent ( path . parentPath ) ;
151
161
}
152
162
153
- function fnNameStartsWithCapital ( name : string | null ) : boolean {
163
+ function isComponentName ( name : string | null ) : boolean {
154
164
return name ?. match ( / ^ [ A - Z ] / ) != null ?? false ;
155
165
}
156
- function fnNameStartsWithUse ( name : string | null ) : boolean {
166
+ function isCustomHookName ( name : string | null ) : boolean {
157
167
return name ?. match ( / ^ u s e [ A - Z ] / ) != null ?? null ;
158
168
}
159
169
@@ -230,14 +240,10 @@ function isComponentFunction(
230
240
) : boolean {
231
241
return (
232
242
getData ( path . scope , containsJSX ) === true && // Function contains JSX
233
- fnNameStartsWithCapital ( functionName ) // Function name indicates it's a component
243
+ isComponentName ( functionName ) // Function name indicates it's a component
234
244
) ;
235
245
}
236
246
237
- function isCustomHook ( functionName : string | null ) : boolean {
238
- return fnNameStartsWithUse ( functionName ) ; // Function name indicates it's a hook
239
- }
240
-
241
247
function shouldTransform (
242
248
path : NodePath < FunctionLike > ,
243
249
functionName : string | null ,
@@ -255,7 +261,8 @@ function shouldTransform(
255
261
if ( options . mode == null || options . mode === "auto" ) {
256
262
return (
257
263
getData ( path . scope , maybeUsesSignal ) === true && // Function appears to use signals;
258
- ( isComponentFunction ( path , functionName ) || isCustomHook ( functionName ) )
264
+ ( isComponentFunction ( path , functionName ) ||
265
+ isCustomHookName ( functionName ) )
259
266
) ;
260
267
}
261
268
@@ -330,7 +337,7 @@ function transformFunction(
330
337
state : PluginPass
331
338
) {
332
339
let newFunction : FunctionLike ;
333
- if ( isCustomHook ( functionName ) || options . experimental ?. noTryFinally ) {
340
+ if ( isCustomHookName ( functionName ) || options . experimental ?. noTryFinally ) {
334
341
// For custom hooks, we don't need to wrap the function body in a
335
342
// try/finally block because later code in the function's render body could
336
343
// read signals and we want to track and associate those signals with this
@@ -452,9 +459,7 @@ function isComponentLike(
452
459
path : NodePath < FunctionLike > ,
453
460
functionName : string | null
454
461
) : boolean {
455
- return (
456
- ! getData ( path , alreadyTransformed ) && fnNameStartsWithCapital ( functionName )
457
- ) ;
462
+ return ! getData ( path , alreadyTransformed ) && isComponentName ( functionName ) ;
458
463
}
459
464
460
465
export default function signalsTransform (
0 commit comments