@@ -66,14 +66,14 @@ function parseFunctions(
66
66
source : string ,
67
67
language : 'flow' | 'typescript' ,
68
68
) : Array <
69
- NodePath <
70
- t . FunctionDeclaration | t . ArrowFunctionExpression | t . FunctionExpression
71
- >
69
+ | NodePath < t . FunctionDeclaration >
70
+ | NodePath < t . ArrowFunctionExpression >
71
+ | NodePath < t . FunctionExpression >
72
72
> {
73
73
const items : Array <
74
- NodePath <
75
- t . FunctionDeclaration | t . ArrowFunctionExpression | t . FunctionExpression
76
- >
74
+ | NodePath < t . FunctionDeclaration >
75
+ | NodePath < t . ArrowFunctionExpression >
76
+ | NodePath < t . FunctionExpression >
77
77
> = [ ] ;
78
78
try {
79
79
const ast = parseInput ( source , language ) ;
@@ -155,22 +155,42 @@ function isHookName(s: string): boolean {
155
155
return / ^ u s e [ A - Z 0 - 9 ] / . test ( s ) ;
156
156
}
157
157
158
- function getReactFunctionType (
159
- id : NodePath < t . Identifier | null | undefined > ,
160
- ) : ReactFunctionType {
161
- if ( id && id . node && id . isIdentifier ( ) ) {
162
- if ( isHookName ( id . node . name ) ) {
158
+ function getReactFunctionType ( id : t . Identifier | null ) : ReactFunctionType {
159
+ if ( id != null ) {
160
+ if ( isHookName ( id . name ) ) {
163
161
return 'Hook' ;
164
162
}
165
163
166
164
const isPascalCaseNameSpace = / ^ [ A - Z ] .* / ;
167
- if ( isPascalCaseNameSpace . test ( id . node . name ) ) {
165
+ if ( isPascalCaseNameSpace . test ( id . name ) ) {
168
166
return 'Component' ;
169
167
}
170
168
}
171
169
return 'Other' ;
172
170
}
173
171
172
+ function getFunctionName (
173
+ fn :
174
+ | NodePath < t . FunctionDeclaration >
175
+ | NodePath < t . ArrowFunctionExpression >
176
+ | NodePath < t . FunctionExpression > ,
177
+ ) : t . Identifier | null {
178
+ if ( fn . isArrowFunctionExpression ( ) ) {
179
+ return null ;
180
+ }
181
+ const id = fn . get ( 'id' ) ;
182
+ return Array . isArray ( id ) === false && id . isIdentifier ( ) ? id . node : null ;
183
+ }
184
+
185
+ let count = 0 ;
186
+ function makeIdentifier ( id : t . Identifier | null ) : t . Identifier {
187
+ if ( id != null && id . name != null ) {
188
+ return id ;
189
+ } else {
190
+ return t . identifier ( `anonymous_${ count ++ } ` ) ;
191
+ }
192
+ }
193
+
174
194
function compile ( source : string ) : [ CompilerOutput , 'flow' | 'typescript' ] {
175
195
const results = new Map < string , PrintedCompilerPipelineValue [ ] > ( ) ;
176
196
const error = new CompilerError ( ) ;
@@ -194,21 +214,7 @@ function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
194
214
const config = parseConfigPragma ( pragma ) ;
195
215
196
216
for ( const fn of parseFunctions ( source , language ) ) {
197
- if ( ! fn . isFunctionDeclaration ( ) ) {
198
- error . pushErrorDetail (
199
- new CompilerErrorDetail ( {
200
- reason : `Unexpected function type ${ fn . node . type } ` ,
201
- description :
202
- 'Playground only supports parsing function declarations' ,
203
- severity : ErrorSeverity . Todo ,
204
- loc : fn . node . loc ?? null ,
205
- suggestions : null ,
206
- } ) ,
207
- ) ;
208
- continue ;
209
- }
210
-
211
- const id = fn . get ( 'id' ) ;
217
+ const id = getFunctionName ( fn ) ;
212
218
for ( const result of run (
213
219
fn ,
214
220
{
@@ -221,7 +227,7 @@ function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
221
227
null ,
222
228
null ,
223
229
) ) {
224
- const fnName = fn . node . id ?. name ?? null ;
230
+ const fnName = id ?. name ?? '(anonymous)' ;
225
231
switch ( result . kind ) {
226
232
case 'ast' : {
227
233
upsert ( {
@@ -230,7 +236,7 @@ function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
230
236
name : result . name ,
231
237
value : {
232
238
type : 'FunctionDeclaration' ,
233
- id : result . value . id ,
239
+ id : makeIdentifier ( result . value . id ) ,
234
240
async : result . value . async ,
235
241
generator : result . value . generator ,
236
242
body : result . value . body ,
0 commit comments