Skip to content

Commit 5bd804b

Browse files
committed
Improve directive parser
1 parent 2522041 commit 5bd804b

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

internal/directive/directive.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ func ParseIgnore(doc *ast.CommentGroup) *Ignore {
3030
}
3131

3232
for _, comment := range doc.List {
33-
if !strings.HasPrefix(comment.Text, "//iface:ignore") {
34-
continue
33+
text := strings.TrimSpace(comment.Text)
34+
if text == "//iface:ignore" {
35+
return &Ignore{}
3536
}
3637

3738
// parse the Names if exists
38-
if strings.Contains(comment.Text, "=") {
39-
parts := strings.Split(comment.Text, "=")
40-
if len(parts) != 2 {
41-
continue
39+
if val, found := strings.CutPrefix(text, "//iface:ignore="); found {
40+
val = strings.TrimSpace(val)
41+
if val == "" {
42+
return &Ignore{}
4243
}
4344

44-
names := strings.Split(parts[1], ",")
45+
names := strings.Split(val, ",")
4546
if len(names) == 0 {
4647
continue
4748
}
@@ -53,9 +54,9 @@ func ParseIgnore(doc *ast.CommentGroup) *Ignore {
5354
if len(names) > 0 {
5455
return &Ignore{Names: names}
5556
}
56-
}
5757

58-
return &Ignore{}
58+
return &Ignore{}
59+
}
5960
}
6061

6162
return nil

internal/directive/directive_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestParseIgnore(t *testing.T) {
3232
},
3333
},
3434
},
35+
dir: nil,
3536
},
3637
"directive with one name": {
3738
doc: &ast.CommentGroup{
@@ -57,6 +58,26 @@ func TestParseIgnore(t *testing.T) {
5758
Names: []string{"unused", "identical"},
5859
},
5960
},
61+
"directive with weird assignment": {
62+
doc: &ast.CommentGroup{
63+
List: []*ast.Comment{
64+
{
65+
Text: "//iface:ignore-asd=unused",
66+
},
67+
},
68+
},
69+
dir: nil,
70+
},
71+
"directive empty val": {
72+
doc: &ast.CommentGroup{
73+
List: []*ast.Comment{
74+
{
75+
Text: "//iface:ignore=",
76+
},
77+
},
78+
},
79+
dir: &directive.Ignore{},
80+
},
6081
}
6182

6283
for name, tc := range testCases {

0 commit comments

Comments
 (0)