forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskip_dirs.go
174 lines (145 loc) · 4.31 KB
/
skip_dirs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package processors
import (
"fmt"
"path/filepath"
"regexp"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
var _ Processor = (*SkipDirs)(nil)
var StdExcludeDirRegexps = []string{
normalizePathRegex("vendor"),
normalizePathRegex("third_party"),
normalizePathRegex("testdata"),
normalizePathRegex("examples"),
normalizePathRegex("Godeps"),
normalizePathRegex("builtin"),
}
// SkipDirs filters reports based on directory names.
// It uses the shortest relative paths and `path-prefix` option.
type skipStat struct {
pattern string
count int
}
type SkipDirs struct {
patterns []*regexp.Regexp
log logutils.Log
skippedDirs map[string]*skipStat
absArgsDirs []string
skippedDirsCache map[string]bool
pathPrefix string
}
func NewSkipDirs(log logutils.Log, patterns, args []string, pathPrefix string) (*SkipDirs, error) {
var patternsRe []*regexp.Regexp
for _, p := range patterns {
p = fsutils.NormalizePathInRegex(p)
patternRe, err := regexp.Compile(p)
if err != nil {
return nil, fmt.Errorf("can't compile regexp %q: %w", p, err)
}
patternsRe = append(patternsRe, patternRe)
}
absArgsDirs, err := absDirs(args)
if err != nil {
return nil, err
}
return &SkipDirs{
patterns: patternsRe,
log: log,
skippedDirs: map[string]*skipStat{},
absArgsDirs: absArgsDirs,
skippedDirsCache: map[string]bool{},
pathPrefix: pathPrefix,
}, nil
}
func (*SkipDirs) Name() string {
return "skip_dirs"
}
func (p *SkipDirs) Process(issues []result.Issue) ([]result.Issue, error) {
if len(p.patterns) == 0 {
return issues, nil
}
return filterIssues(issues, p.shouldPassIssue), nil
}
func (p *SkipDirs) Finish() {
for dir, stat := range p.skippedDirs {
p.log.Infof("Skipped %d issues from dir %s by pattern %s", stat.count, dir, stat.pattern)
}
}
func (p *SkipDirs) shouldPassIssue(issue *result.Issue) bool {
if filepath.IsAbs(issue.FilePath()) {
if isGoFile(issue.FilePath()) {
p.log.Warnf("Got abs path %s in skip dirs processor, it should be relative", issue.FilePath())
}
return true
}
issueRelDir := filepath.Dir(issue.FilePath())
if toPass, ok := p.skippedDirsCache[issueRelDir]; ok {
if !toPass {
p.skippedDirs[issueRelDir].count++
}
return toPass
}
issueAbsDir, err := filepath.Abs(issueRelDir)
if err != nil {
p.log.Warnf("Can't abs-ify path %q: %s", issueRelDir, err)
return true
}
toPass := p.shouldPassIssueDirs(issueRelDir, issueAbsDir)
p.skippedDirsCache[issueRelDir] = toPass
return toPass
}
func (p *SkipDirs) shouldPassIssueDirs(issueRelDir, issueAbsDir string) bool {
for _, absArgDir := range p.absArgsDirs {
if absArgDir == issueAbsDir {
// we must not skip issues if they are from explicitly set dirs
// even if they match skip patterns
return true
}
}
// We use issueRelDir for matching: it's the relative to the current
// work dir path of directory of source file with the issue. It can lead
// to unexpected behavior if we're analyzing files out of current work dir.
// The alternative solution is to find relative to args path, but it has
// disadvantages (https://github.com/golangci/golangci-lint/pull/313).
path := fsutils.WithPathPrefix(p.pathPrefix, issueRelDir)
for _, pattern := range p.patterns {
if pattern.MatchString(path) {
ps := pattern.String()
if p.skippedDirs[issueRelDir] == nil {
p.skippedDirs[issueRelDir] = &skipStat{
pattern: ps,
}
}
p.skippedDirs[issueRelDir].count++
return false
}
}
return true
}
func absDirs(args []string) ([]string, error) {
if len(args) == 0 {
args = append(args, "./...")
}
var absArgsDirs []string
for _, arg := range args {
base := filepath.Base(arg)
if base == "..." || isGoFile(base) {
arg = filepath.Dir(arg)
}
absArg, err := filepath.Abs(arg)
if err != nil {
return nil, fmt.Errorf("failed to abs-ify arg %q: %w", arg, err)
}
absArgsDirs = append(absArgsDirs, absArg)
}
return absArgsDirs, nil
}
func normalizePathRegex(e string) string {
return createPathRegex(e, filepath.Separator)
}
func createPathRegex(e string, sep rune) string {
escapedSep := regexp.QuoteMeta(string(sep)) // needed for windows sep '\\'
return fmt.Sprintf(`(^|%[1]s)%[2]s($|%[1]s)`, escapedSep, e)
}