Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit ca98f9b

Browse files
DaivikDaveStephen Gutekanst
authored and
Stephen Gutekanst
committed
indexer/javascript: improve functions coverage
1 parent 5f1eb23 commit ca98f9b

File tree

1 file changed

+109
-30
lines changed

1 file changed

+109
-30
lines changed

doctree/indexer/javascript/indexer.go

Lines changed: 109 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -115,33 +115,7 @@ func (i *javascriptIndexer) IndexDir(ctx context.Context, dir string) (*schema.I
115115
}
116116
}
117117

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()
145119

146120
// Function definitions
147121
{
@@ -210,7 +184,6 @@ func (i *javascriptIndexer) IndexDir(ctx context.Context, dir string) (*schema.I
210184
classes := classesByMod[modName]
211185

212186
// Extract class methods:
213-
214187
classFuncQuery := `
215188
(_
216189
(comment)* @func_docs
@@ -322,9 +295,17 @@ func getFunctions(node *sitter.Node, content []byte, q string, searchKeyPrefix [
322295
funcDocs = extractFunctionDocs(funcDocs)
323296
funcName := firstCaptureContentOr(content, captures["func_name"], "")
324297
funcParams := firstCaptureContentOr(content, captures["func_params"], "")
325-
298+
funcIdentifier := firstCaptureContentOr(content, captures["var_identifier"], "")
299+
isArrowFunction := firstCaptureContentOr(content, captures["arrow_function"], "")
326300
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+
}
328309
functions = append(functions, schema.Section{
329310
ID: funcName,
330311
ShortLabel: funcName,
@@ -433,13 +414,111 @@ func extractFunctionDocs(s string) string {
433414

434415
func sanitizeDocs(s string) string {
435416
if strings.HasPrefix(s, "//") {
417+
s = strings.ReplaceAll(s, "\n//", "\n")
436418
return strings.TrimPrefix(s, "//")
437419
} else if strings.HasPrefix(s, "/*") {
438420
return strings.TrimSuffix(strings.TrimPrefix(s, "/*"), "*/")
439421
}
440422
return s
441423
}
442424

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+
443522
type moduleInfo struct {
444523
path string
445524
docs string

0 commit comments

Comments
 (0)