Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions v2/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func ExtractFunctionStyleCommentTags(marker string, tagNames []string, lines []s
if err != nil {
return nil, err
}
tag, err := toStringArgs(typedTag)
tag, err := toStringArgs(typedTag, opts.forbidArguments)
if err != nil {
return nil, err
}
Expand All @@ -141,13 +141,26 @@ func ParseValues(enabled bool) TagOption {
}
}

// ForbidArguments bans tag arguments. When enabled, tag arguments
// must be empty. Otherwise, a parse error will be returned.
// Default: disabled
func ForbidArguments(enabled bool) TagOption {
return func(opts *tagOpts) {
opts.forbidArguments = enabled
}
}

type tagOpts struct {
parseValues bool
parseValues bool
forbidArguments bool
}

func toStringArgs(tag codetags.Tag) (Tag, error) {
func toStringArgs(tag codetags.Tag, forbidArguments bool) (Tag, error) {
var stringArgs []string
if len(tag.Args) > 1 {
if forbidArguments && len(tag.Args) > 0 {
return Tag{}, fmt.Errorf("expected no arguments, got: %v", tag.Args)
}
if !forbidArguments && len(tag.Args) > 1 {
return Tag{}, fmt.Errorf("expected one argument, got: %v", tag.Args)
}
for _, arg := range tag.Args {
Expand Down
45 changes: 38 additions & 7 deletions v2/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ func TestExtractFunctionStyleCommentTags(t *testing.T) {
mkstrs := func(s ...string) []string { return s }

cases := []struct {
name string
comments []string
tagNames []string
parseValues bool
expectError bool
expect map[string][]Tag
name string
comments []string
tagNames []string
parseValues bool
forbidArguments bool
expectError bool
expect map[string][]Tag
}{{
name: "no args",
comments: []string{
Expand Down Expand Up @@ -284,6 +285,36 @@ func TestExtractFunctionStyleCommentTags(t *testing.T) {
},
parseValues: true,
expectError: true,
}, {
name: "ForbidArguments enabled - no args",
comments: []string{
"Human comment that is ignored",
"+simpleNoVal",
"+simpleWithVal=val",
"+duplicateNoVal",
"+duplicateNoVal",
"+duplicateWithVal=val1",
"+duplicateWithVal=val2",
},
forbidArguments: true,
expect: map[string][]Tag{
"simpleNoVal": mktags(Tag{"simpleNoVal", nil, ""}),
"simpleWithVal": mktags(Tag{"simpleWithVal", nil, "val"}),
"duplicateNoVal": mktags(
Tag{"duplicateNoVal", nil, ""},
Tag{"duplicateNoVal", nil, ""}),
"duplicateWithVal": mktags(
Tag{"duplicateWithVal", nil, "val1"},
Tag{"duplicateWithVal", nil, "val2"}),
},
}, {
name: "ForbidArguments enabled - with args",
comments: []string{
"Human comment that is ignored",
"+simpleNoVal(arg)",
},
forbidArguments: true,
expectError: true,
}, {
name: "raw values with comments",
comments: []string{
Expand All @@ -302,7 +333,7 @@ func TestExtractFunctionStyleCommentTags(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result, err := ExtractFunctionStyleCommentTags("+", tc.tagNames, tc.comments, ParseValues(tc.parseValues))
result, err := ExtractFunctionStyleCommentTags("+", tc.tagNames, tc.comments, ParseValues(tc.parseValues), ForbidArguments(tc.forbidArguments))

if tc.expectError {
if err == nil {
Expand Down