-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexclude.go
More file actions
184 lines (157 loc) · 4.79 KB
/
exclude.go
File metadata and controls
184 lines (157 loc) · 4.79 KB
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
175
176
177
178
179
180
181
182
183
184
package exclude
import (
"flag"
"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"
)
// AllFuncs is all functions.
var AllFuncs = []Func{GeneratedFile, TestFile, FileWithPattern}
// Func excludes reporting diagnostics or analyzing.
type Func func(a *analysis.Analyzer) *analysis.Analyzer
// By excludes with the functions.
func By(a *analysis.Analyzer, fs ...Func) *analysis.Analyzer {
analyzer := a
for _, f := range fs {
analyzer = f(analyzer)
}
return analyzer
}
// All excludes the analyzers with the functions.
func All(as []*analysis.Analyzer, fs ...Func) []*analysis.Analyzer {
excluded := make([]*analysis.Analyzer, len(as))
for i := range as {
excluded[i] = By(as[i], fs...)
}
return excluded
}
// Flags sets flags which name has "all-exclude" prefix to each analyzers.
// Flags returns remain arguments including flags which did not set to the analyzers.
func Flags(args []string, as ...*analysis.Analyzer) (remains []string, _ error) {
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.SetOutput(ioutil.Discard)
a := By(new(analysis.Analyzer), AllFuncs...)
a.Flags.VisitAll(func(f *flag.Flag) {
flags.Var(f.Value, "all-"+f.Name, f.Usage)
})
if err := flags.Parse(args); err != nil {
return nil, err
}
var rerr error
flags.Visit(func(f *flag.Flag) {
if !strings.HasPrefix(f.Name, "all-exclude") {
remains = append(remains, f.Name+"="+f.Value.String())
return
}
for _, a := range as {
// remove "all-" prefix and set each analyzer
name := f.Name[4:]
if a.Flags.Lookup(name) != nil {
err := a.Flags.Set(f.Name[4:], f.Value.String())
if err != nil {
rerr = err
}
}
}
})
if rerr != nil {
return nil, rerr
}
remains = append(remains, flags.Args()...)
if remains != nil {
return remains, nil
}
return []string{}, nil
}
// GeneratedFile excludes auto generated files.
// Because it excludes only reporting diagnostics, analyzing would be excuted.
func GeneratedFile(a *analysis.Analyzer) *analysis.Analyzer {
const flag = "exclude-generated"
if a.Flags.Lookup(flag) != nil {
panic("flag -" + flag + " has already been set")
}
var onoff bool
a.Flags.BoolVar(&onoff, flag, true, "whether excludes generated files or not")
if !onoff {
// skip
return a
}
orgRun := a.Run
a.Run = func(pass *analysis.Pass) (interface{}, error) {
pass.Report = ReportWithFilter(pass, func(d analysis.Diagnostic) bool {
return !analysisutil.IsGeneratedFile(analysisutil.File(pass, d.Pos))
})
return orgRun(pass)
}
return a
}
// TestFile excludes test files.
// Because it excludes only reporting diagnostics, analyzing would be excuted.
func TestFile(a *analysis.Analyzer) *analysis.Analyzer {
const flag = "exclude-testfile"
if a.Flags.Lookup(flag) != nil {
panic("flag -" + flag + " has already been set")
}
var onoff bool
a.Flags.BoolVar(&onoff, flag, true, "whether excludes test files or not")
if !onoff {
// skip
return a
}
orgRun := a.Run
a.Run = func(pass *analysis.Pass) (interface{}, error) {
pass.Report = ReportWithFilter(pass, func(d analysis.Diagnostic) bool {
file := pass.Fset.File(d.Pos)
return file != nil && !strings.HasSuffix(file.Name(), "_test.go")
})
return orgRun(pass)
}
return a
}
// FileWithPattern excludes files which matches the pattern given by exclude flag.
// Because it excludes only reporting diagnostics, analyzing would be excuted.
func FileWithPattern(a *analysis.Analyzer) *analysis.Analyzer {
const flag = "exclude-file"
if a.Flags.Lookup(flag) != nil {
panic("flag -" + flag + " has already been set")
}
var pattern string
a.Flags.StringVar(&pattern, flag, "", "a pattern of excluding file path")
orgRun := a.Run
a.Run = func(pass *analysis.Pass) (interface{}, error) {
if pattern == "" {
return orgRun(pass)
}
excludeRegexp, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf("exclude path: %w", err)
}
pass.Report = ReportWithFilter(pass, func(d analysis.Diagnostic) bool {
file := pass.Fset.File(d.Pos)
return file != nil && !excludeRegexp.MatchString(file.Name())
})
return orgRun(pass)
}
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
}