Skip to content

Commit f00c89f

Browse files
authored
dev: unifying processors code style (#4592)
1 parent ec97551 commit f00c89f

23 files changed

+418
-369
lines changed

pkg/lint/runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
8686
processors.NewNolint(log.Child(logutils.DebugKeyNolint), dbManager, enabledLinters),
8787

8888
processors.NewUniqByLine(cfg),
89-
processors.NewDiff(cfg.Issues.Diff, cfg.Issues.DiffFromRevision, cfg.Issues.DiffPatchFilePath, cfg.Issues.WholeFiles),
89+
processors.NewDiff(&cfg.Issues),
9090
processors.NewMaxPerFileFromLinter(cfg),
9191
processors.NewMaxSameIssues(cfg.Issues.MaxSameIssues, log.Child(logutils.DebugKeyMaxSameIssues), cfg),
9292
processors.NewMaxFromLinter(cfg.Issues.MaxIssuesPerLinter, log.Child(logutils.DebugKeyMaxFromLinter), cfg),

pkg/result/processors/autogenerated_exclude.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
genAutoFile = "autogenerated file" // easyjson
1818
)
1919

20-
var _ Processor = &AutogeneratedExclude{}
20+
var _ Processor = (*AutogeneratedExclude)(nil)
2121

2222
type fileSummary struct {
2323
generated bool
@@ -41,7 +41,7 @@ func NewAutogeneratedExclude(strict bool) *AutogeneratedExclude {
4141
}
4242
}
4343

44-
func (p *AutogeneratedExclude) Name() string {
44+
func (*AutogeneratedExclude) Name() string {
4545
return "autogenerated_exclude"
4646
}
4747

pkg/result/processors/cgo.go

+29-27
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,51 @@ import (
99
"github.com/golangci/golangci-lint/pkg/result"
1010
)
1111

12+
var _ Processor = (*Cgo)(nil)
13+
1214
type Cgo struct {
1315
goCacheDir string
1416
}
1517

16-
var _ Processor = Cgo{}
17-
1818
func NewCgo(goenv *goutil.Env) *Cgo {
1919
return &Cgo{
2020
goCacheDir: goenv.Get(goutil.EnvGoCache),
2121
}
2222
}
2323

24-
func (p Cgo) Name() string {
24+
func (Cgo) Name() string {
2525
return "cgo"
2626
}
2727

2828
func (p Cgo) Process(issues []result.Issue) ([]result.Issue, error) {
29-
return filterIssuesErr(issues, func(issue *result.Issue) (bool, error) {
30-
// some linters (e.g. gosec, deadcode) return incorrect filepaths for cgo issues,
31-
// also cgo files have strange issues looking like false positives.
32-
33-
// cache dir contains all preprocessed files including cgo files
34-
35-
issueFilePath := issue.FilePath()
36-
if !filepath.IsAbs(issue.FilePath()) {
37-
absPath, err := filepath.Abs(issue.FilePath())
38-
if err != nil {
39-
return false, fmt.Errorf("failed to build abs path for %q: %w", issue.FilePath(), err)
40-
}
41-
issueFilePath = absPath
42-
}
29+
return filterIssuesErr(issues, p.shouldPassIssue)
30+
}
4331

44-
if p.goCacheDir != "" && strings.HasPrefix(issueFilePath, p.goCacheDir) {
45-
return false, nil
46-
}
32+
func (Cgo) Finish() {}
33+
34+
func (p Cgo) shouldPassIssue(issue *result.Issue) (bool, error) {
35+
// some linters (e.g. gosec, deadcode) return incorrect filepaths for cgo issues,
36+
// also cgo files have strange issues looking like false positives.
37+
38+
// cache dir contains all preprocessed files including cgo files
4739

48-
if filepath.Base(issue.FilePath()) == "_cgo_gotypes.go" {
49-
// skip cgo warning for go1.10
50-
return false, nil
40+
issueFilePath := issue.FilePath()
41+
if !filepath.IsAbs(issue.FilePath()) {
42+
absPath, err := filepath.Abs(issue.FilePath())
43+
if err != nil {
44+
return false, fmt.Errorf("failed to build abs path for %q: %w", issue.FilePath(), err)
5145
}
46+
issueFilePath = absPath
47+
}
5248

53-
return true, nil
54-
})
55-
}
49+
if p.goCacheDir != "" && strings.HasPrefix(issueFilePath, p.goCacheDir) {
50+
return false, nil
51+
}
5652

57-
func (Cgo) Finish() {}
53+
if filepath.Base(issue.FilePath()) == "_cgo_gotypes.go" {
54+
// skip cgo warning for go1.10
55+
return false, nil
56+
}
57+
58+
return true, nil
59+
}

pkg/result/processors/diff.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import (
99

1010
"github.com/golangci/revgrep"
1111

12+
"github.com/golangci/golangci-lint/pkg/config"
1213
"github.com/golangci/golangci-lint/pkg/result"
1314
)
1415

1516
const envGolangciDiffProcessorPatch = "GOLANGCI_DIFF_PROCESSOR_PATCH"
1617

18+
var _ Processor = (*Diff)(nil)
19+
1720
type Diff struct {
1821
onlyNew bool
1922
fromRev string
@@ -22,19 +25,17 @@ type Diff struct {
2225
patch string
2326
}
2427

25-
var _ Processor = Diff{}
26-
27-
func NewDiff(onlyNew bool, fromRev, patchFilePath string, wholeFiles bool) *Diff {
28+
func NewDiff(cfg *config.Issues) *Diff {
2829
return &Diff{
29-
onlyNew: onlyNew,
30-
fromRev: fromRev,
31-
patchFilePath: patchFilePath,
32-
wholeFiles: wholeFiles,
30+
onlyNew: cfg.Diff,
31+
fromRev: cfg.DiffFromRevision,
32+
patchFilePath: cfg.DiffPatchFilePath,
33+
wholeFiles: cfg.WholeFiles,
3334
patch: os.Getenv(envGolangciDiffProcessorPatch),
3435
}
3536
}
3637

37-
func (p Diff) Name() string {
38+
func (Diff) Name() string {
3839
return "diff"
3940
}
4041

pkg/result/processors/exclude.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/golangci/golangci-lint/pkg/result"
77
)
88

9-
var _ Processor = Exclude{}
9+
var _ Processor = (*Exclude)(nil)
1010

1111
type Exclude struct {
1212
name string
@@ -49,4 +49,4 @@ func (p Exclude) Process(issues []result.Issue) ([]result.Issue, error) {
4949
}), nil
5050
}
5151

52-
func (p Exclude) Finish() {}
52+
func (Exclude) Finish() {}

pkg/result/processors/exclude_rules.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/golangci/golangci-lint/pkg/result"
99
)
1010

11-
var _ Processor = ExcludeRules{}
11+
var _ Processor = (*ExcludeRules)(nil)
1212

1313
type excludeRule struct {
1414
baseRule
@@ -50,23 +50,25 @@ func NewExcludeRules(log logutils.Log, files *fsutils.Files, opts ExcludeRulesOp
5050
return p
5151
}
5252

53+
func (p ExcludeRules) Name() string { return p.name }
54+
5355
func (p ExcludeRules) Process(issues []result.Issue) ([]result.Issue, error) {
5456
if len(p.rules) == 0 {
5557
return issues, nil
5658
}
59+
5760
return filterIssues(issues, func(issue *result.Issue) bool {
5861
for _, rule := range p.rules {
5962
rule := rule
6063
if rule.match(issue, p.files, p.log) {
6164
return false
6265
}
6366
}
67+
6468
return true
6569
}), nil
6670
}
6771

68-
func (p ExcludeRules) Name() string { return p.name }
69-
7072
func (ExcludeRules) Finish() {}
7173

7274
func createRules(rules []ExcludeRule, prefix string) []excludeRule {

pkg/result/processors/filename_unadjuster.go

+48-46
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/golangci/golangci-lint/pkg/result"
1515
)
1616

17+
var _ Processor = (*FilenameUnadjuster)(nil)
18+
1719
type posMapper func(pos token.Position) token.Position
1820

1921
type adjustMap struct {
@@ -30,50 +32,6 @@ type FilenameUnadjuster struct {
3032
loggedUnadjustments map[string]bool
3133
}
3234

33-
var _ Processor = &FilenameUnadjuster{}
34-
35-
func processUnadjusterPkg(m *adjustMap, pkg *packages.Package, log logutils.Log) {
36-
fset := token.NewFileSet() // it's more memory efficient to not store all in one fset
37-
38-
for _, filename := range pkg.CompiledGoFiles {
39-
// It's important to call func here to run GC
40-
processUnadjusterFile(filename, m, log, fset)
41-
}
42-
}
43-
44-
func processUnadjusterFile(filename string, m *adjustMap, log logutils.Log, fset *token.FileSet) {
45-
syntax, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
46-
if err != nil {
47-
// Error will be reported by typecheck
48-
return
49-
}
50-
51-
adjustedFilename := fset.PositionFor(syntax.Pos(), true).Filename
52-
if adjustedFilename == "" {
53-
return
54-
}
55-
56-
unadjustedFilename := fset.PositionFor(syntax.Pos(), false).Filename
57-
if unadjustedFilename == "" || unadjustedFilename == adjustedFilename {
58-
return
59-
}
60-
61-
if !strings.HasSuffix(unadjustedFilename, ".go") {
62-
return // file.go -> /caches/cgo-xxx
63-
}
64-
65-
m.Lock()
66-
defer m.Unlock()
67-
m.m[adjustedFilename] = func(adjustedPos token.Position) token.Position {
68-
tokenFile := fset.File(syntax.Pos())
69-
if tokenFile == nil {
70-
log.Warnf("Failed to get token file for %s", adjustedFilename)
71-
return adjustedPos
72-
}
73-
return fset.PositionFor(tokenFile.Pos(adjustedPos.Offset), false)
74-
}
75-
}
76-
7735
func NewFilenameUnadjuster(pkgs []*packages.Package, log logutils.Log) *FilenameUnadjuster {
7836
m := adjustMap{m: map[string]posMapper{}}
7937

@@ -97,7 +55,7 @@ func NewFilenameUnadjuster(pkgs []*packages.Package, log logutils.Log) *Filename
9755
}
9856
}
9957

100-
func (p *FilenameUnadjuster) Name() string {
58+
func (*FilenameUnadjuster) Name() string {
10159
return "filename_unadjuster"
10260
}
10361

@@ -128,4 +86,48 @@ func (p *FilenameUnadjuster) Process(issues []result.Issue) ([]result.Issue, err
12886
}), nil
12987
}
13088

131-
func (p *FilenameUnadjuster) Finish() {}
89+
func (*FilenameUnadjuster) Finish() {}
90+
91+
func processUnadjusterPkg(m *adjustMap, pkg *packages.Package, log logutils.Log) {
92+
fset := token.NewFileSet() // it's more memory efficient to not store all in one fset
93+
94+
for _, filename := range pkg.CompiledGoFiles {
95+
// It's important to call func here to run GC
96+
processUnadjusterFile(filename, m, log, fset)
97+
}
98+
}
99+
100+
func processUnadjusterFile(filename string, m *adjustMap, log logutils.Log, fset *token.FileSet) {
101+
syntax, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
102+
if err != nil {
103+
// Error will be reported by typecheck
104+
return
105+
}
106+
107+
adjustedFilename := fset.PositionFor(syntax.Pos(), true).Filename
108+
if adjustedFilename == "" {
109+
return
110+
}
111+
112+
unadjustedFilename := fset.PositionFor(syntax.Pos(), false).Filename
113+
if unadjustedFilename == "" || unadjustedFilename == adjustedFilename {
114+
return
115+
}
116+
117+
if !strings.HasSuffix(unadjustedFilename, ".go") {
118+
return // file.go -> /caches/cgo-xxx
119+
}
120+
121+
m.Lock()
122+
defer m.Unlock()
123+
124+
m.m[adjustedFilename] = func(adjustedPos token.Position) token.Position {
125+
tokenFile := fset.File(syntax.Pos())
126+
if tokenFile == nil {
127+
log.Warnf("Failed to get token file for %s", adjustedFilename)
128+
return adjustedPos
129+
}
130+
131+
return fset.PositionFor(tokenFile.Pos(adjustedPos.Offset), false)
132+
}
133+
}

0 commit comments

Comments
 (0)