diff --git a/parser.go b/parser.go index 261ba6c55..452682226 100644 --- a/parser.go +++ b/parser.go @@ -843,10 +843,12 @@ func matchExtension(extensionToMatch string, comments []*ast.Comment) (match boo for _, comment := range comments { commentLine := strings.TrimSpace(strings.TrimLeft(comment.Text, "/")) fields := FieldsByAnySpace(commentLine, 2) - lowerAttribute := strings.ToLower(fields[0]) + if len(fields) > 0 { + lowerAttribute := strings.ToLower(fields[0]) - if lowerAttribute == fmt.Sprintf("@x-%s", strings.ToLower(extensionToMatch)) { - return true + if lowerAttribute == fmt.Sprintf("@x-%s", strings.ToLower(extensionToMatch)) { + return true + } } } return false diff --git a/parser_test.go b/parser_test.go index 0876b94e9..f696b53a3 100644 --- a/parser_test.go +++ b/parser_test.go @@ -3895,17 +3895,17 @@ func TestParser_parseExtension(t *testing.T) { { name: "when no flag is set, everything is exported", parser: New(), - expectedPaths: map[string]bool{"/without-extension": true, "/with-another-extension": true, "/with-correct-extension": true}, + expectedPaths: map[string]bool{"/without-extension": true, "/with-another-extension": true, "/with-correct-extension": true, "/with-empty-comment-line": true}, }, { name: "when nonexistent flag is set, nothing is exported", parser: New(SetParseExtension("nonexistent-extension-filter")), - expectedPaths: map[string]bool{"/without-extension": false, "/with-another-extension": false, "/with-correct-extension": false}, + expectedPaths: map[string]bool{"/without-extension": false, "/with-another-extension": false, "/with-correct-extension": false, "/with-empty-comment-line": false}, }, { name: "when correct flag is set, only that Path is exported", parser: New(SetParseExtension("google-backend")), - expectedPaths: map[string]bool{"/without-extension": false, "/with-another-extension": false, "/with-correct-extension": true}, + expectedPaths: map[string]bool{"/without-extension": false, "/with-another-extension": false, "/with-correct-extension": true, "/with-empty-comment-line": false}, }, } diff --git a/testdata/parseExtension/parseExtension.go b/testdata/parseExtension/parseExtension.go index 9948c7a25..c666ebde1 100644 --- a/testdata/parseExtension/parseExtension.go +++ b/testdata/parseExtension/parseExtension.go @@ -10,3 +10,7 @@ func Fun2() {} // @Router /with-correct-extension [get] // @x-google-backend {"address": "http://backend"} func Fun3() {} + +// @Router /with-empty-comment-line [get] +// +func FunEmptyCommentLine() {}