Skip to content

Commit 12a2337

Browse files
authored
Merge pull request #959 from filecoin-project/feature/godoc_parsing
VEN-3 | Add function GoDoc parsing
2 parents 1378952 + 420a96a commit 12a2337

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

test-crawler/collector/collector.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,10 @@ func GenerateMethodName(funcName string) string {
145145
return fmt.Sprintf("%s_%s", strings.ToUpper(buf.String()), usecase)
146146
}
147147

148-
// GenerateMethodDescription generates description based on inputParams, returnParams
148+
// GenerateMethodDescription generates description based on comment.
149149
func GenerateMethodDescription(f FunctionAnnotation) string {
150-
var dsc string
151150
if f.Description == "" {
152-
dsc = "Function description not set."
153-
if (f.InputParams != "" && f.InputParams != "()") && f.ReturnValues != "" {
154-
dsc = fmt.Sprintf(`Given a %s, returns %s`, f.InputParams, f.ReturnValues)
155-
} else if (f.InputParams == "" || f.InputParams == "()") && f.ReturnValues != "" {
156-
dsc = fmt.Sprintf(`Returns %s`, f.ReturnValues)
157-
}
151+
return "Function description not set."
158152
}
159-
160-
return dsc
153+
return f.Description
161154
}

test-crawler/crawler_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,34 @@ func TestCrawlSingleFileForFunctions(t *testing.T) {
1717
t.Errorf("got error: %v", err.Error())
1818
}
1919

20-
if len(fnsAnn) != 4 {
20+
if len(fnsAnn) != 5 {
2121
t.Errorf("got %q, expected %q methods", len(fnsAnn), 2)
2222
}
2323

24+
assert.Equal(t, "// HelloEvent simple method that just formats message.", fnsAnn[0].Description)
2425
assert.Equal(t, "HELLO_EVENT_001", fnsAnn[0].Name)
2526
assert.Equal(t, "()", fnsAnn[0].InputParams) // input param
2627
assert.Equal(t, "string", fnsAnn[0].ReturnValues) // return param
2728

29+
assert.Equal(t, "// HelloEventWithParameter accepts one param that got formated in message.", fnsAnn[1].Description)
2830
assert.Equal(t, "HELLO_EVENT_WITH_PARAMETER_001", fnsAnn[1].Name)
2931
assert.Equal(t, "(param string)", fnsAnn[1].InputParams)
3032
assert.Equal(t, "(string, error)", fnsAnn[1].ReturnValues)
3133

34+
assert.Equal(t, "// FunctionWithoutParameters...", fnsAnn[2].Description)
3235
assert.Equal(t, "FUNCTION_WITHOUT_PARAMETERS_001", fnsAnn[2].Name)
3336
assert.Equal(t, "()", fnsAnn[2].InputParams)
3437
assert.Equal(t, "", fnsAnn[2].ReturnValues)
3538

39+
assert.Equal(t, "// FunctionWithPointerReturnValue returns a simple pointer value.", fnsAnn[3].Description)
3640
assert.Equal(t, "FUNCTION_WITH_POINTER_RETURN_VALUE_001", fnsAnn[3].Name)
3741
assert.Equal(t, "()", fnsAnn[3].InputParams)
3842
assert.Equal(t, "*Event", fnsAnn[3].ReturnValues)
43+
44+
assert.Equal(t, "Function description not set.", fnsAnn[4].Description)
45+
assert.Equal(t, "FUNCTION_WITHOUT_COMMENT_001", fnsAnn[4].Name)
46+
assert.Equal(t, "()", fnsAnn[4].InputParams)
47+
assert.Equal(t, "*Event", fnsAnn[4].ReturnValues)
3948
}
4049

4150
func TestMakeYAML(t *testing.T) {
@@ -46,15 +55,15 @@ func TestMakeYAML(t *testing.T) {
4655
Name: "SomeName",
4756
InputParams: "(ctx context.Context, param Parameters)",
4857
ReturnValues: "error",
49-
Description: "",
58+
Description: "SomeComment",
5059
Public: true,
5160
},
5261
{
5362
ID: 2,
5463
Name: "SomeName2",
5564
InputParams: "(ctx context.Context, param2 Parameters2)",
5665
ReturnValues: "error",
57-
Description: "",
66+
Description: "SomeComment2",
5867
Public: true,
5968
},
6069
}

test-crawler/extractor/extractor.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
212212
if child != nil {
213213
if child.Type() == string(FUNCTION_DECLARATION) {
214214
if prevNode.Type() == string(COMMENT) {
215-
216215
value, annotationType, _ := parser.Parse(content[prevNode.StartByte():prevNode.EndByte()])
217216
if value != nil && annotationType == a.Ignore {
218217
isIgnored = value.(bool)
@@ -261,6 +260,14 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
261260
child.Child(4): parameter_list: (<-chan *types2.RequestEvent, error)
262261
*/
263262
if child.Type() == string(METHOD_DECLARATION) {
263+
comment := ""
264+
if prevNode.Type() == string(COMMENT) {
265+
comment = content[prevNode.StartByte():prevNode.EndByte()]
266+
value, annotationType, _ := parser.Parse(comment)
267+
if value != nil && annotationType == a.Ignore {
268+
isIgnored = value.(bool)
269+
}
270+
}
264271
funcName = content[child.Child(2).StartByte():child.Child(2).EndByte()]
265272
params := ""
266273
returnValues := ""
@@ -280,6 +287,7 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
280287
returnValues = content[child.Child(4).StartByte():child.Child(4).EndByte()]
281288
}
282289
funcAnn := c.FunctionAnnotation{
290+
Description: comment,
283291
Name: funcName,
284292
Public: isPublic(funcName),
285293
InputParams: params,

test-crawler/mocks/event.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ func (e *Event) FunctionWithoutParameters() {
3939
func (e *Event) FunctionWithPointerReturnValue() *Event {
4040
return &Event{}
4141
}
42+
43+
func (e *Event) FunctionWithoutComment() *Event {
44+
return &Event{}
45+
}

0 commit comments

Comments
 (0)