@@ -115,33 +115,7 @@ func (i *javascriptIndexer) IndexDir(ctx context.Context, dir string) (*schema.I
115
115
}
116
116
}
117
117
118
- funcDefQuery := `
119
- (
120
- [
121
- (
122
- (comment)* @func_docs
123
- .
124
- (
125
- function_declaration
126
- name: (identifier) @func_name
127
- parameters: (formal_parameters) @func_params
128
- )
129
- )
130
- (
131
- (comment)* @func_docs
132
- .
133
- (
134
- export_statement
135
- value: (
136
- function
137
- name: (identifier) @func_name
138
- parameters: (formal_parameters) @func_params
139
- )
140
- )
141
- )
142
- ]
143
- )
144
- `
118
+ funcDefQuery := functionDefinitionQuery ()
145
119
146
120
// Function definitions
147
121
{
@@ -210,7 +184,6 @@ func (i *javascriptIndexer) IndexDir(ctx context.Context, dir string) (*schema.I
210
184
classes := classesByMod [modName ]
211
185
212
186
// Extract class methods:
213
-
214
187
classFuncQuery := `
215
188
(_
216
189
(comment)* @func_docs
@@ -322,9 +295,17 @@ func getFunctions(node *sitter.Node, content []byte, q string, searchKeyPrefix [
322
295
funcDocs = extractFunctionDocs (funcDocs )
323
296
funcName := firstCaptureContentOr (content , captures ["func_name" ], "" )
324
297
funcParams := firstCaptureContentOr (content , captures ["func_params" ], "" )
325
-
298
+ funcIdentifier := firstCaptureContentOr (content , captures ["var_identifier" ], "" )
299
+ isArrowFunction := firstCaptureContentOr (content , captures ["arrow_function" ], "" )
326
300
funcLabel := schema .Markdown ("function " + funcName + funcParams )
327
-
301
+ if funcIdentifier != "" {
302
+ funcName = funcIdentifier
303
+ if isArrowFunction != "" {
304
+ funcLabel = schema .Markdown (funcName + " = " + funcParams )
305
+ } else {
306
+ funcLabel = schema .Markdown (funcName + " = function " + funcParams )
307
+ }
308
+ }
328
309
functions = append (functions , schema.Section {
329
310
ID : funcName ,
330
311
ShortLabel : funcName ,
@@ -433,13 +414,111 @@ func extractFunctionDocs(s string) string {
433
414
434
415
func sanitizeDocs (s string ) string {
435
416
if strings .HasPrefix (s , "//" ) {
417
+ s = strings .ReplaceAll (s , "\n //" , "\n " )
436
418
return strings .TrimPrefix (s , "//" )
437
419
} else if strings .HasPrefix (s , "/*" ) {
438
420
return strings .TrimSuffix (strings .TrimPrefix (s , "/*" ), "*/" )
439
421
}
440
422
return s
441
423
}
442
424
425
+ func functionDefinitionQuery () string {
426
+
427
+ functionDefinition := `(
428
+ function
429
+ name: (identifier)? @func_name
430
+ parameters: (formal_parameters) @func_params
431
+ ) `
432
+
433
+ arrowFunctionDefinition := `(
434
+ arrow_function
435
+ parameters: (formal_parameters) @func_params
436
+ ) @arrow_function`
437
+
438
+ // function myfunc(){}
439
+ funcDeclaration := `
440
+ (
441
+ (comment)* @func_docs
442
+ .
443
+ (
444
+ function_declaration
445
+ name: (identifier) @func_name
446
+ parameters: (formal_parameters) @func_params
447
+ )
448
+ )
449
+ `
450
+ // var myfunction = function(a,b){}
451
+ // var myfunction = (a,b) => {}
452
+ funcAssignmentExpression := fmt .Sprintf (`(
453
+ (comment)* @func_docs
454
+ .
455
+ (lexical_declaration
456
+ (_
457
+ name: (identifier) @var_identifier
458
+ value: [
459
+ %s
460
+ %s
461
+ ]
462
+
463
+
464
+ )
465
+ )
466
+ )` , functionDefinition , arrowFunctionDefinition )
467
+
468
+ // export default myfunc = function(){}
469
+ // export default myfunc = () => {}
470
+ funcExportExpression := fmt .Sprintf (`(
471
+ (comment)* @func_docs
472
+ .
473
+ (export_statement
474
+ (lexical_declaration
475
+ (_
476
+ name: (identifier) @var_identifier
477
+ value: [
478
+ %s
479
+ %s
480
+ ]
481
+
482
+ )
483
+ )?
484
+ value:([
485
+ %s
486
+ %s
487
+ ])?
488
+ )
489
+
490
+ )` , functionDefinition , arrowFunctionDefinition , functionDefinition , arrowFunctionDefinition )
491
+
492
+ // module.exports = function(){}
493
+ funcExpressionStatementAssignment := fmt .Sprintf (`
494
+ (
495
+ (comment)* @func_docs
496
+ .
497
+ (expression_statement
498
+ (assignment_expression
499
+ left: (_) @var_identifier
500
+ right:
501
+ [
502
+ %s
503
+ %s
504
+ ]
505
+ )
506
+ )
507
+ )` , functionDefinition , arrowFunctionDefinition )
508
+
509
+ query := fmt .Sprintf (`
510
+ (
511
+ [
512
+ %s
513
+ %s
514
+ %s
515
+ %s
516
+ ]
517
+ )
518
+ ` , funcDeclaration , funcAssignmentExpression , funcExportExpression , funcExpressionStatementAssignment )
519
+ return query
520
+ }
521
+
443
522
type moduleInfo struct {
444
523
path string
445
524
docs string
0 commit comments