@@ -3,7 +3,6 @@ package revive
3
3
import (
4
4
"bytes"
5
5
"cmp"
6
- "encoding/json"
7
6
"fmt"
8
7
"go/token"
9
8
"os"
34
33
isDebug = logutils .HaveDebugTag (logutils .DebugKeyRevive )
35
34
)
36
35
37
- // jsonObject defines a JSON object of a failure
38
- type jsonObject struct {
39
- Severity lint.Severity
40
- lint.Failure `json:",inline"`
41
- }
42
-
43
36
func New (settings * config.ReviveSettings ) * goanalysis.Linter {
44
37
var mu sync.Mutex
45
38
var resIssues []goanalysis.Issue
@@ -63,7 +56,7 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
63
56
}
64
57
65
58
analyzer .Run = func (pass * analysis.Pass ) (any , error ) {
66
- issues , err := w .run (lintCtx , pass )
59
+ issues , err := w .run (pass )
67
60
if err != nil {
68
61
return nil , err
69
62
}
@@ -85,7 +78,6 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
85
78
86
79
type wrapper struct {
87
80
revive lint.Linter
88
- formatter lint.Formatter
89
81
lintingRules []lint.Rule
90
82
conf * lint.Config
91
83
}
@@ -103,101 +95,70 @@ func newWrapper(settings *config.ReviveSettings) (*wrapper, error) {
103
95
return nil , err
104
96
}
105
97
106
- formatter , err := reviveConfig .GetFormatter ("json" )
107
- if err != nil {
108
- return nil , err
109
- }
110
-
111
98
lintingRules , err := reviveConfig .GetLintingRules (conf , []lint.Rule {})
112
99
if err != nil {
113
100
return nil , err
114
101
}
115
102
116
103
return & wrapper {
117
104
revive : lint .New (os .ReadFile , settings .MaxOpenFiles ),
118
- formatter : formatter ,
119
105
lintingRules : lintingRules ,
120
106
conf : conf ,
121
107
}, nil
122
108
}
123
109
124
- func (w * wrapper ) run (lintCtx * linter. Context , pass * analysis.Pass ) ([]goanalysis.Issue , error ) {
110
+ func (w * wrapper ) run (pass * analysis.Pass ) ([]goanalysis.Issue , error ) {
125
111
packages := [][]string {internal .GetGoFileNames (pass )}
126
112
127
113
failures , err := w .revive .Lint (packages , w .lintingRules , * w .conf )
128
114
if err != nil {
129
115
return nil , err
130
116
}
131
117
132
- formatChan := make (chan lint.Failure )
133
- exitChan := make (chan bool )
134
-
135
- var output string
136
- go func () {
137
- output , err = w .formatter .Format (formatChan , * w .conf )
138
- if err != nil {
139
- lintCtx .Log .Errorf ("Format error: %v" , err )
140
- }
141
- exitChan <- true
142
- }()
143
-
144
- for f := range failures {
145
- if f .Confidence < w .conf .Confidence {
118
+ var issues []goanalysis.Issue
119
+ for failure := range failures {
120
+ if failure .Confidence < w .conf .Confidence {
146
121
continue
147
122
}
148
123
149
- formatChan <- f
150
- }
151
-
152
- close (formatChan )
153
- <- exitChan
154
-
155
- var results []jsonObject
156
- err = json .Unmarshal ([]byte (output ), & results )
157
- if err != nil {
158
- return nil , err
159
- }
160
-
161
- var issues []goanalysis.Issue
162
- for i := range results {
163
- issues = append (issues , toIssue (pass , & results [i ]))
124
+ issues = append (issues , w .toIssue (pass , & failure ))
164
125
}
165
126
166
127
return issues , nil
167
128
}
168
129
169
- func toIssue (pass * analysis.Pass , object * jsonObject ) goanalysis.Issue {
170
- lineRangeTo := object .Position .End .Line
171
- if object .RuleName == (& rule.ExportedRule {}).Name () {
172
- lineRangeTo = object .Position .Start .Line
130
+ func ( w * wrapper ) toIssue (pass * analysis.Pass , failure * lint. Failure ) goanalysis.Issue {
131
+ lineRangeTo := failure .Position .End .Line
132
+ if failure .RuleName == (& rule.ExportedRule {}).Name () {
133
+ lineRangeTo = failure .Position .Start .Line
173
134
}
174
135
175
136
issue := & result.Issue {
176
- Severity : string (object . Severity ),
177
- Text : fmt .Sprintf ("%s: %s" , object .RuleName , object . Failure .Failure ),
137
+ Severity : string (severity ( w . conf , failure ) ),
138
+ Text : fmt .Sprintf ("%s: %s" , failure .RuleName , failure .Failure ),
178
139
Pos : token.Position {
179
- Filename : object .Position .Start .Filename ,
180
- Line : object .Position .Start .Line ,
181
- Offset : object .Position .Start .Offset ,
182
- Column : object .Position .Start .Column ,
140
+ Filename : failure .Position .Start .Filename ,
141
+ Line : failure .Position .Start .Line ,
142
+ Offset : failure .Position .Start .Offset ,
143
+ Column : failure .Position .Start .Column ,
183
144
},
184
145
LineRange : & result.Range {
185
- From : object .Position .Start .Line ,
146
+ From : failure .Position .Start .Line ,
186
147
To : lineRangeTo ,
187
148
},
188
149
FromLinter : linterName ,
189
150
}
190
151
191
- if object .ReplacementLine != "" {
192
- f := pass .Fset .File (token .Pos (object .Position .Start .Offset ))
152
+ if failure .ReplacementLine != "" {
153
+ f := pass .Fset .File (token .Pos (failure .Position .Start .Offset ))
193
154
194
155
// Skip cgo files because the positions are wrong.
195
- if object .GetFilename () == f .Name () {
156
+ if failure .GetFilename () == f .Name () {
196
157
issue .SuggestedFixes = []analysis.SuggestedFix {{
197
158
TextEdits : []analysis.TextEdit {{
198
- Pos : f .LineStart (object .Position .Start .Line ),
199
- End : goanalysis .EndOfLinePos (f , object .Position .End .Line ),
200
- NewText : []byte (object .ReplacementLine ),
159
+ Pos : f .LineStart (failure .Position .Start .Line ),
160
+ End : goanalysis .EndOfLinePos (f , failure .Position .End .Line ),
161
+ NewText : []byte (failure .ReplacementLine ),
201
162
}},
202
163
}}
203
164
}
@@ -489,3 +450,15 @@ func extractRulesName(rules []lint.Rule) []string {
489
450
490
451
return names
491
452
}
453
+
454
+ // Extracted from https://github.com/mgechev/revive/blob/v1.7.0/formatter/severity.go
455
+ // Modified to use pointers (related to hugeParam rule).
456
+ func severity (config * lint.Config , failure * lint.Failure ) lint.Severity {
457
+ if cfg , ok := config .Rules [failure .RuleName ]; ok && cfg .Severity == lint .SeverityError {
458
+ return lint .SeverityError
459
+ }
460
+ if cfg , ok := config .Directives [failure .RuleName ]; ok && cfg .Severity == lint .SeverityError {
461
+ return lint .SeverityError
462
+ }
463
+ return lint .SeverityWarning
464
+ }
0 commit comments