Skip to content

Commit

Permalink
Improve directive parser
Browse files Browse the repository at this point in the history
  • Loading branch information
uudashr committed Jul 18, 2024
1 parent 2522041 commit 5bd804b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
19 changes: 10 additions & 9 deletions internal/directive/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ func ParseIgnore(doc *ast.CommentGroup) *Ignore {
}

for _, comment := range doc.List {
if !strings.HasPrefix(comment.Text, "//iface:ignore") {
continue
text := strings.TrimSpace(comment.Text)
if text == "//iface:ignore" {
return &Ignore{}
}

// parse the Names if exists
if strings.Contains(comment.Text, "=") {
parts := strings.Split(comment.Text, "=")
if len(parts) != 2 {
continue
if val, found := strings.CutPrefix(text, "//iface:ignore="); found {
val = strings.TrimSpace(val)
if val == "" {
return &Ignore{}
}

names := strings.Split(parts[1], ",")
names := strings.Split(val, ",")
if len(names) == 0 {
continue
}
Expand All @@ -53,9 +54,9 @@ func ParseIgnore(doc *ast.CommentGroup) *Ignore {
if len(names) > 0 {
return &Ignore{Names: names}
}
}

return &Ignore{}
return &Ignore{}
}
}

return nil
Expand Down
21 changes: 21 additions & 0 deletions internal/directive/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestParseIgnore(t *testing.T) {
},
},
},
dir: nil,
},
"directive with one name": {
doc: &ast.CommentGroup{
Expand All @@ -57,6 +58,26 @@ func TestParseIgnore(t *testing.T) {
Names: []string{"unused", "identical"},
},
},
"directive with weird assignment": {
doc: &ast.CommentGroup{
List: []*ast.Comment{
{
Text: "//iface:ignore-asd=unused",
},
},
},
dir: nil,
},
"directive empty val": {
doc: &ast.CommentGroup{
List: []*ast.Comment{
{
Text: "//iface:ignore=",
},
},
},
dir: &directive.Ignore{},
},
}

for name, tc := range testCases {
Expand Down

0 comments on commit 5bd804b

Please sign in to comment.