Skip to content

Commit df71654

Browse files
authored
dev: improve revive construction (#4817)
1 parent bff93a1 commit df71654

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

pkg/golinters/revive/revive.go

+32-12
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
4949
[]*analysis.Analyzer{analyzer},
5050
nil,
5151
).WithContextSetter(func(lintCtx *linter.Context) {
52+
w, err := newWrapper(settings)
53+
if err != nil {
54+
lintCtx.Log.Errorf("setup revive: %v", err)
55+
return
56+
}
57+
5258
analyzer.Run = func(pass *analysis.Pass) (any, error) {
53-
issues, err := runRevive(lintCtx, pass, settings)
59+
issues, err := w.run(lintCtx, pass)
5460
if err != nil {
5561
return nil, err
5662
}
@@ -70,10 +76,15 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
7076
}).WithLoadMode(goanalysis.LoadModeSyntax)
7177
}
7278

73-
func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.ReviveSettings) ([]goanalysis.Issue, error) {
74-
packages := [][]string{internal.GetFileNames(pass)}
79+
type wrapper struct {
80+
revive lint.Linter
81+
formatter lint.Formatter
82+
lintingRules []lint.Rule
83+
conf *lint.Config
84+
}
7585

76-
conf, err := getReviveConfig(settings)
86+
func newWrapper(settings *config.ReviveSettings) (*wrapper, error) {
87+
conf, err := getConfig(settings)
7788
if err != nil {
7889
return nil, err
7990
}
@@ -83,14 +94,23 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
8394
return nil, err
8495
}
8596

86-
revive := lint.New(os.ReadFile, settings.MaxOpenFiles)
87-
8897
lintingRules, err := reviveConfig.GetLintingRules(conf, []lint.Rule{})
8998
if err != nil {
9099
return nil, err
91100
}
92101

93-
failures, err := revive.Lint(packages, lintingRules, *conf)
102+
return &wrapper{
103+
revive: lint.New(os.ReadFile, settings.MaxOpenFiles),
104+
formatter: formatter,
105+
lintingRules: lintingRules,
106+
conf: conf,
107+
}, nil
108+
}
109+
110+
func (w *wrapper) run(lintCtx *linter.Context, pass *analysis.Pass) ([]goanalysis.Issue, error) {
111+
packages := [][]string{internal.GetFileNames(pass)}
112+
113+
failures, err := w.revive.Lint(packages, w.lintingRules, *w.conf)
94114
if err != nil {
95115
return nil, err
96116
}
@@ -100,15 +120,15 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
100120

101121
var output string
102122
go func() {
103-
output, err = formatter.Format(formatChan, *conf)
123+
output, err = w.formatter.Format(formatChan, *w.conf)
104124
if err != nil {
105125
lintCtx.Log.Errorf("Format error: %v", err)
106126
}
107127
exitChan <- true
108128
}()
109129

110130
for f := range failures {
111-
if f.Confidence < conf.Confidence {
131+
if f.Confidence < w.conf.Confidence {
112132
continue
113133
}
114134

@@ -126,13 +146,13 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
126146

127147
var issues []goanalysis.Issue
128148
for i := range results {
129-
issues = append(issues, reviveToIssue(pass, &results[i]))
149+
issues = append(issues, toIssue(pass, &results[i]))
130150
}
131151

132152
return issues, nil
133153
}
134154

135-
func reviveToIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
155+
func toIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
136156
lineRangeTo := object.Position.End.Line
137157
if object.RuleName == (&rule.ExportedRule{}).Name() {
138158
lineRangeTo = object.Position.Start.Line
@@ -160,7 +180,7 @@ func reviveToIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
160180
// https://github.com/golangci/golangci-lint/issues/1745
161181
// https://github.com/mgechev/revive/blob/v1.3.7/config/config.go#L217
162182
// https://github.com/mgechev/revive/blob/v1.3.7/config/config.go#L169-L174
163-
func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
183+
func getConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
164184
conf := defaultConfig()
165185

166186
if !reflect.DeepEqual(cfg, &config.ReviveSettings{}) {

0 commit comments

Comments
 (0)