From 7867c24647431213f9ce96645be11c382080822d Mon Sep 17 00:00:00 2001 From: Igor K Date: Mon, 12 Dec 2022 13:07:45 +0100 Subject: [PATCH] fix: don't error on empty comment line (#1415) --- 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() {}