-
Notifications
You must be signed in to change notification settings - Fork 113
/
run.go
229 lines (200 loc) · 5.11 KB
/
run.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package analyzer
import (
"fmt"
"log"
"path/filepath"
"strings"
"sync"
"golang.org/x/tools/go/analysis"
_ "github.com/go-critic/go-critic/checkers" // Register go-critic checkers
"github.com/go-critic/go-critic/framework/linter"
)
type gocritic struct {
infoList []*linter.CheckerInfo
goVersion linter.GoVersion
}
var (
globalGocriticMu sync.Mutex
globalGocritic *gocritic
globalInitErrorReported bool
)
func runAnalyzer(pass *analysis.Pass) (interface{}, error) {
critic, err := prepareGocritic()
if err != nil {
return nil, fmt.Errorf("init error: %w", err)
}
ctx := linter.NewContext(pass.Fset, pass.TypesSizes)
ctx.GoVersion = critic.goVersion
ctx.SetPackageInfo(pass.TypesInfo, pass.Pkg)
checkers, err := critic.createCheckers(ctx)
if err != nil {
return nil, err
}
for _, f := range pass.Files {
filename := filepath.Base(pass.Fset.Position(f.Pos()).Filename)
ctx.SetFileInfo(filename, f)
for _, c := range checkers {
warnings := c.Check(f)
for _, warning := range warnings {
diag := analysis.Diagnostic{
Pos: warning.Node.Pos(),
Message: fmt.Sprintf("%s: %s", c.Info.Name, warning.Text),
}
if warning.HasQuickFix() {
diag.SuggestedFixes = []analysis.SuggestedFix{
{
Message: "suggested replacement",
TextEdits: []analysis.TextEdit{
{
Pos: warning.Suggestion.From,
End: warning.Suggestion.To,
NewText: warning.Suggestion.Replacement,
},
},
},
}
}
pass.Report(diag)
}
}
}
return nil, nil
}
// prepareGocritic initializes a new gocririt object,
// but unlike newGocritic() it could use a cached version.
func prepareGocritic() (*gocritic, error) {
if DisableCache {
return newGocritic()
}
globalGocriticMu.Lock()
defer globalGocriticMu.Unlock()
// Don't report init error ever again if it was already reported.
if globalInitErrorReported {
return nil, nil
}
if globalGocritic != nil {
return globalGocritic, nil
}
critic, err := newGocritic()
if err != nil {
globalInitErrorReported = true
return nil, err
}
globalGocritic = critic
return critic, nil
}
func newGocritic() (*gocritic, error) {
critic := &gocritic{
infoList: filterCheckersList(registeredCheckers),
}
ver, err := linter.ParseGoVersion(flagGoVersion)
if err != nil {
return nil, err
}
critic.goVersion = ver
for _, info := range critic.infoList {
for pname, param := range info.Params {
key := checkerParamName(info, pname)
switch param.Value.(type) {
case int:
info.Params[pname].Value = *intParams[key]
case bool:
info.Params[pname].Value = *boolParams[key]
case string:
info.Params[pname].Value = *stringParams[key]
default:
panic("unreachable") // Checked in AddChecker
}
}
}
return critic, nil
}
func filterCheckersList(infoList []*linter.CheckerInfo) []*linter.CheckerInfo {
parseKeys := func(keys []string, byName, byTag map[string]bool) {
for _, key := range keys {
if strings.HasPrefix(key, "#") {
byTag[key[len("#"):]] = true
} else {
byName[key] = true
}
}
}
splitValues := func(s string) []string {
parts := strings.Split(s, ",")
for i := range parts {
parts[i] = strings.TrimSpace(parts[i])
}
return parts
}
disableArg := flagDisable
if disableArg == "<default>" {
if flagEnableAll {
disableArg = ""
} else {
disableArg = "#experimental,#opinionated,#performance"
}
}
enabledByName := make(map[string]bool)
enabledTags := make(map[string]bool)
parseKeys(splitValues(flagEnable), enabledByName, enabledTags)
disabledByName := make(map[string]bool)
disabledTags := make(map[string]bool)
parseKeys(splitValues(disableArg), disabledByName, disabledTags)
enabledByTag := func(info *linter.CheckerInfo) bool {
for _, tag := range info.Tags {
if enabledTags[tag] {
return true
}
}
return false
}
disabledByTag := func(info *linter.CheckerInfo) string {
for _, tag := range info.Tags {
if disabledTags[tag] {
return tag
}
}
return ""
}
var filtered []*linter.CheckerInfo
for _, info := range infoList {
enabled := flagEnableAll || enabledByName[info.Name] || enabledByTag(info)
notice := ""
switch {
case !enabled:
notice = "not enabled by name or tag (-enable)"
case disabledByName[info.Name]:
enabled = false
notice = "disabled by name (-disable)"
default:
if tag := disabledByTag(info); tag != "" {
enabled = false
notice = fmt.Sprintf("disabled by %q tag (-disable)", tag)
}
}
if flagDebugInit && !enabled {
log.Printf("\tdebug: %s: %s", info.Name, notice)
}
if !enabled {
continue
}
filtered = append(filtered, info)
}
if flagDebugInit {
for _, info := range filtered {
log.Printf("\tdebug: %s is enabled", info.Name)
}
}
return filtered
}
func (critic *gocritic) createCheckers(ctx *linter.Context) ([]*linter.Checker, error) {
checkers := make([]*linter.Checker, len(critic.infoList))
for i, info := range critic.infoList {
c, err := linter.NewChecker(ctx, info)
if err != nil {
return nil, fmt.Errorf("init %s: %w", info.Name, err)
}
checkers[i] = c
}
return checkers, nil
}