Skip to content

Commit

Permalink
Add LintIgnoreComment
Browse files Browse the repository at this point in the history
  • Loading branch information
tenntenn committed Nov 15, 2024
1 parent fd39e8d commit babe499
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
21 changes: 21 additions & 0 deletions exclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"fmt"
"io/ioutil"
"regexp"
"slices"
"strings"

"github.com/gostaticanalysis/analysisutil"
"github.com/gostaticanalysis/comment"
"github.com/gostaticanalysis/comment/passes/commentmap"
"golang.org/x/tools/go/analysis"
)

Expand Down Expand Up @@ -161,3 +164,21 @@ func FileWithPattern(a *analysis.Analyzer) *analysis.Analyzer {

return a
}

// LintIgnoreComment excludes diagnostics with staticchek style //lint:ignore comment.
func LintIgnoreComment(a *analysis.Analyzer) *analysis.Analyzer {
orgRun := a.Run

if !slices.Contains(a.Requires, commentmap.Analyzer) {
a.Requires = append(a.Requires, commentmap.Analyzer)
}

a.Run = func(pass *analysis.Pass) (interface{}, error) {
pass.Report = ReportWithFilter(pass, func(d analysis.Diagnostic) bool {
cmaps, _ := pass.ResultOf[commentmap.Analyzer].(comment.Maps)
return !cmaps.IgnorePosLine(pass.Fset, d.Pos, a.Name)
})
return orgRun(pass)
}
return a
}
46 changes: 41 additions & 5 deletions exclude_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package exclude_test

import (
"bytes"
"flag"
"fmt"
"html/template"
"path/filepath"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/gostaticanalysis/exclude"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"

"github.com/gostaticanalysis/exclude"
)

func TestGeneratedFile(t *testing.T) {
Expand All @@ -27,15 +30,14 @@ func TestGeneratedFile(t *testing.T) {
}

for name, tt := range cases {
name, tt := name, tt
t.Run(name, func(t *testing.T) {
t.Parallel()
path := filepath.Join(name, name+".go")
dir := writeFiles(t, map[string]string{
path: fmt.Sprintf("%s\npackage %s", tt.src, name),
})
var rec reportRecoder
analysistest.Run(t, dir, rec.new(exclude.GeneratedFile), name)
analysistest.Run(t, dir, rec.new(exclude.GeneratedFile, -1), name)
if got := rec.isReported(); got != tt.want {
t.Errorf("want %v but got %v", tt.want, got)
}
Expand All @@ -62,7 +64,7 @@ func TestTestFile(t *testing.T) {
path: fmt.Sprintf("package %s", name),
})
var rec reportRecoder
analysistest.Run(t, dir, rec.new(exclude.TestFile), name)
analysistest.Run(t, dir, rec.new(exclude.TestFile, 1), name)
if got := rec.isReported(); got != tt.want {
t.Errorf("want %v but got %v", tt.want, got)
}
Expand Down Expand Up @@ -90,7 +92,7 @@ func TestFileWithPattern(t *testing.T) {
path: fmt.Sprintf("package %s", name),
})
var rec reportRecoder
a := rec.new(exclude.FileWithPattern)
a := rec.new(exclude.FileWithPattern, 1)
a.Flags.Set("exclude-file", tt.pattern)
analysistest.Run(t, dir, a, name)
if got := rec.isReported(); got != tt.want {
Expand Down Expand Up @@ -152,3 +154,37 @@ func TestFlags(t *testing.T) {
})
}
}

func TestLintIgnoreComment(t *testing.T) {
t.Parallel()

cases := map[string]struct {
comment string
want bool // is reported?
}{
"nocomment": {"", true},
"ignore": {"//lint:ignore {{.}} reason", false},
"missname": {"//lint:ignore missname reason", true},
"noreason": {"//lint:ignore {{.}}", true},
}

for name, tt := range cases {
t.Run(name, func(t *testing.T) {
t.Parallel()
path := filepath.Join(name, name+".go")
var rec reportRecoder
a := rec.new(exclude.LintIgnoreComment, 3)
var comment bytes.Buffer
if err := template.Must(template.New(name).Parse(tt.comment)).Execute(&comment, a.Name); err != nil {
t.Fatal("unexpected error")
}
dir := writeFiles(t, map[string]string{
path: fmt.Sprintf("package %s\n%s\nvar _ struct{}", name, &comment),
})
analysistest.Run(t, dir, a, name)
if got := rec.isReported(); got != tt.want {
t.Errorf("want %v but got %v", tt.want, got)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.22.9
require (
github.com/google/go-cmp v0.6.0
github.com/gostaticanalysis/analysisutil v0.7.1
github.com/gostaticanalysis/comment v1.5.0
golang.org/x/tools v0.27.0
)

require (
github.com/gostaticanalysis/comment v1.4.2 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.9.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/gostaticanalysis/analysisutil v0.7.1 h1:ZMCjoue3DtDWQ5WyU16YbjbQEQ3Vu
github.com/gostaticanalysis/analysisutil v0.7.1/go.mod h1:v21E3hY37WKMGSnbsw2S/ojApNWb6C1//mXO48CXbVc=
github.com/gostaticanalysis/comment v1.4.2 h1:hlnx5+S2fY9Zo9ePo4AhgYsYHbM2+eAv8m/s1JiCd6Q=
github.com/gostaticanalysis/comment v1.4.2/go.mod h1:KLUTGDv6HOCotCH8h2erHKmpci2ZoR8VPu34YA2uzdM=
github.com/gostaticanalysis/comment v1.5.0 h1:X82FLl+TswsUMpMh17srGRuKaaXprTaytmEpgnKIDu8=
github.com/gostaticanalysis/comment v1.5.0/go.mod h1:V6eb3gpCv9GNVqb6amXzEUX3jXLVK/AdA+IrAMSqvEc=
github.com/gostaticanalysis/testutil v0.3.1-0.20210208050101-bfb5c8eec0e4 h1:d2/eIbH9XjD1fFwD5SHv8x168fjbQ9PB8hvs8DSEC08=
github.com/gostaticanalysis/testutil v0.3.1-0.20210208050101-bfb5c8eec0e4/go.mod h1:D+FIZ+7OahH3ePw/izIEeH5I06eKs1IKI4Xr64/Am3M=
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
Expand Down
13 changes: 11 additions & 2 deletions helper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exclude_test

import (
"go/token"
"testing"

"golang.org/x/tools/go/analysis"
Expand All @@ -13,7 +14,7 @@ type reportRecoder struct {
reports []analysis.Diagnostic
}

func (r *reportRecoder) new(f exclude.Func) *analysis.Analyzer {
func (r *reportRecoder) new(f exclude.Func, line int) *analysis.Analyzer {
a := exclude.By(&analysis.Analyzer{
Name: "TestAnalyzer",
Doc: "document",
Expand All @@ -23,7 +24,15 @@ func (r *reportRecoder) new(f exclude.Func) *analysis.Analyzer {
return nil, nil
}

pass.Reportf(pass.Files[0].Pos(), "hello")
var pos token.Pos
if line >= 0 {
file := pass.Fset.File(pass.Files[0].Pos())
pos = file.LineStart(line)
} else {
pos = pass.Files[0].Pos()
}

pass.Reportf(pos, "hello")
return nil, nil
},
}, f)
Expand Down

0 comments on commit babe499

Please sign in to comment.