From 17965fddc801bf7162c1ceb23c190436e3a04fbe Mon Sep 17 00:00:00 2001 From: Igor Kova Date: Sun, 11 Dec 2022 22:04:12 +0100 Subject: [PATCH] fix: don't error on empty comment line --- parser.go | 8 +++++--- parser_test.go | 6 +++--- testdata/parseExtension/parseExtension.go | 4 ++++ 3 files changed, 12 insertions(+), 6 deletions(-) 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() {}