Skip to content

Commit

Permalink
passes/commentignore: Support multiple line comments (#249)
Browse files Browse the repository at this point in the history
Reference: #248

Previously, multiple line comments including the lintignore directive would not take effect unless it was on the first line. Now any comment line can implement the lintignore directive.
  • Loading branch information
bflad committed Jan 26, 2022
1 parent 5c97942 commit afaa086
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
13 changes: 13 additions & 0 deletions passes/AT001/testdata/src/a/comment_ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ func fcommentignore() {

//lintignore:AT001,AT002 // extra comment
_ = r.TestCase{}

// extra comment
//lintignore:AT001
_ = r.TestCase{}

// extra comment
//lintignore:AT001
// extra comment
_ = r.TestCase{}

//lintignore:AT001
// extra comment
_ = r.TestCase{}
}
32 changes: 20 additions & 12 deletions passes/commentignore/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,27 @@ func (ignorer *Ignorer) ShouldIgnore(key string, n ast.Node) bool {
func run(pass *analysis.Pass) (interface{}, error) {
ignores := map[string][]ignore{}
for _, f := range pass.Files {
cmap := ast.NewCommentMap(pass.Fset, f, f.Comments)
for n, cgs := range cmap {
for _, cg := range cgs {
if strings.HasPrefix(cg.Text(), commentIgnorePrefix) {
commentIgnore := strings.TrimPrefix(cg.Text(), commentIgnorePrefix)
// Allow extra // comment after keys
commentIgnoreParts := strings.Split(commentIgnore, "//")
keys := strings.TrimSpace(commentIgnoreParts[0])
for n, commentGroups := range ast.NewCommentMap(pass.Fset, f, f.Comments) {
for _, commentGroup := range commentGroups {
for _, comment := range commentGroup.List {
if comment == nil {
continue
}

// Remove // comment prefix
commentText := strings.TrimPrefix(comment.Text, "//")

if strings.HasPrefix(commentText, commentIgnorePrefix) {
commentIgnore := strings.TrimPrefix(commentText, commentIgnorePrefix)
// Allow extra // comment after keys
commentIgnoreParts := strings.Split(commentIgnore, "//")
keys := strings.TrimSpace(commentIgnoreParts[0])

// Allow multiple comma separated ignores
for _, key := range strings.Split(keys, ",") {
// is it possible for nested pos/end to be outside the largest nodes?
ignores[key] = append(ignores[key], ignore{n.Pos(), n.End()})
// Allow multiple comma separated ignores
for _, key := range strings.Split(keys, ",") {
// is it possible for nested pos/end to be outside the largest nodes?
ignores[key] = append(ignores[key], ignore{n.Pos(), n.End()})
}
}
}
}
Expand Down

0 comments on commit afaa086

Please sign in to comment.