@@ -144,6 +144,91 @@ func (lnt *Linter) configure() error {
144
144
return nil
145
145
}
146
146
147
+ func parseError (srcErr packages.Error ) (* result.Issue , error ) {
148
+ pos , err := libpackages .ParseErrorPosition (srcErr .Pos )
149
+ if err != nil {
150
+ return nil , err
151
+ }
152
+
153
+ return & result.Issue {
154
+ Pos : * pos ,
155
+ Text : srcErr .Msg ,
156
+ FromLinter : "typecheck" ,
157
+ }, nil
158
+ }
159
+
160
+ func buildIssuesFromErrorsForTypecheckMode (errs []error , lintCtx * linter.Context ) ([]result.Issue , error ) {
161
+ var issues []result.Issue
162
+ uniqReportedIssues := map [string ]bool {}
163
+ for _ , err := range errs {
164
+ itErr , ok := errors .Cause (err ).(* IllTypedError )
165
+ if ! ok {
166
+ return nil , err
167
+ }
168
+ for _ , err := range libpackages .ExtractErrors (itErr .Pkg ) {
169
+ i , perr := parseError (err )
170
+ if perr != nil { // failed to parse
171
+ if uniqReportedIssues [err .Msg ] {
172
+ continue
173
+ }
174
+ uniqReportedIssues [err .Msg ] = true
175
+ lintCtx .Log .Errorf ("typechecking error: %s" , err .Msg )
176
+ } else {
177
+ i .Pkg = itErr .Pkg // to save to cache later
178
+ issues = append (issues , * i )
179
+ }
180
+ }
181
+ }
182
+ return issues , nil
183
+ }
184
+
185
+ func buildIssues (diags []Diagnostic , linterNameBuilder func (diag * Diagnostic ) string ) []result.Issue {
186
+ var issues []result.Issue
187
+ for i := range diags {
188
+ diag := & diags [i ]
189
+ linterName := linterNameBuilder (diag )
190
+
191
+ var text string
192
+ if diag .Analyzer .Name == linterName {
193
+ text = diag .Message
194
+ } else {
195
+ text = fmt .Sprintf ("%s: %s" , diag .Analyzer .Name , diag .Message )
196
+ }
197
+
198
+ var suggestedFixes string
199
+ if len (diag .SuggestedFixes ) > 0 {
200
+ elems := []string {}
201
+ for _ , fix := range diag .SuggestedFixes {
202
+ elems = append (elems , fix .Message )
203
+ for _ , text := range fix .TextEdits {
204
+ elems = append (elems , string (text .NewText ))
205
+ }
206
+ }
207
+ suggestedFixes = strings .Join (elems , "\n " )
208
+ }
209
+
210
+ issues = append (issues , result.Issue {
211
+ FromLinter : linterName ,
212
+ Text : text ,
213
+ SuggestedFixes : suggestedFixes ,
214
+ Pos : diag .Position ,
215
+ Pkg : diag .Pkg ,
216
+ })
217
+
218
+ if len (diag .Related ) > 0 {
219
+ for _ , info := range diag .Related {
220
+ issues = append (issues , result.Issue {
221
+ FromLinter : linterName ,
222
+ Text : fmt .Sprintf ("%s(related information): %s" , diag .Analyzer .Name , info .Message ),
223
+ Pos : diag .Pkg .Fset .Position (info .Pos ),
224
+ Pkg : diag .Pkg ,
225
+ })
226
+ }
227
+ }
228
+ }
229
+ return issues
230
+ }
231
+
147
232
func (lnt * Linter ) preRun (lintCtx * linter.Context ) error {
148
233
if err := analysis .Validate (lnt .analyzers ); err != nil {
149
234
return errors .Wrap (err , "failed to validate analyzers" )
0 commit comments