Skip to content

Commit

Permalink
fix: don't error on empty comment line (#1415)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkova authored Dec 12, 2022
1 parent 7c20f30 commit 7867c24
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
8 changes: 5 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
}

Expand Down
4 changes: 4 additions & 0 deletions testdata/parseExtension/parseExtension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

0 comments on commit 7867c24

Please sign in to comment.