@@ -49,8 +49,14 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
49
49
[]* analysis.Analyzer {analyzer },
50
50
nil ,
51
51
).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
+
52
58
analyzer .Run = func (pass * analysis.Pass ) (any , error ) {
53
- issues , err := runRevive (lintCtx , pass , settings )
59
+ issues , err := w . run (lintCtx , pass )
54
60
if err != nil {
55
61
return nil , err
56
62
}
@@ -70,10 +76,15 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
70
76
}).WithLoadMode (goanalysis .LoadModeSyntax )
71
77
}
72
78
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
+ }
75
85
76
- conf , err := getReviveConfig (settings )
86
+ func newWrapper (settings * config.ReviveSettings ) (* wrapper , error ) {
87
+ conf , err := getConfig (settings )
77
88
if err != nil {
78
89
return nil , err
79
90
}
@@ -83,14 +94,23 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
83
94
return nil , err
84
95
}
85
96
86
- revive := lint .New (os .ReadFile , settings .MaxOpenFiles )
87
-
88
97
lintingRules , err := reviveConfig .GetLintingRules (conf , []lint.Rule {})
89
98
if err != nil {
90
99
return nil , err
91
100
}
92
101
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 )
94
114
if err != nil {
95
115
return nil , err
96
116
}
@@ -100,15 +120,15 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
100
120
101
121
var output string
102
122
go func () {
103
- output , err = formatter .Format (formatChan , * conf )
123
+ output , err = w . formatter .Format (formatChan , * w . conf )
104
124
if err != nil {
105
125
lintCtx .Log .Errorf ("Format error: %v" , err )
106
126
}
107
127
exitChan <- true
108
128
}()
109
129
110
130
for f := range failures {
111
- if f .Confidence < conf .Confidence {
131
+ if f .Confidence < w . conf .Confidence {
112
132
continue
113
133
}
114
134
@@ -126,13 +146,13 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
126
146
127
147
var issues []goanalysis.Issue
128
148
for i := range results {
129
- issues = append (issues , reviveToIssue (pass , & results [i ]))
149
+ issues = append (issues , toIssue (pass , & results [i ]))
130
150
}
131
151
132
152
return issues , nil
133
153
}
134
154
135
- func reviveToIssue (pass * analysis.Pass , object * jsonObject ) goanalysis.Issue {
155
+ func toIssue (pass * analysis.Pass , object * jsonObject ) goanalysis.Issue {
136
156
lineRangeTo := object .Position .End .Line
137
157
if object .RuleName == (& rule.ExportedRule {}).Name () {
138
158
lineRangeTo = object .Position .Start .Line
@@ -160,7 +180,7 @@ func reviveToIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
160
180
// https://github.com/golangci/golangci-lint/issues/1745
161
181
// https://github.com/mgechev/revive/blob/v1.3.7/config/config.go#L217
162
182
// 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 ) {
164
184
conf := defaultConfig ()
165
185
166
186
if ! reflect .DeepEqual (cfg , & config.ReviveSettings {}) {
0 commit comments