|
| 1 | +package goanalysis |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + "golang.org/x/tools/go/packages" |
| 9 | + |
| 10 | + "github.com/golangci/golangci-lint/pkg/lint/linter" |
| 11 | + libpackages "github.com/golangci/golangci-lint/pkg/packages" |
| 12 | + "github.com/golangci/golangci-lint/pkg/result" |
| 13 | +) |
| 14 | + |
| 15 | +type IllTypedError struct { |
| 16 | + Pkg *packages.Package |
| 17 | +} |
| 18 | + |
| 19 | +func (e *IllTypedError) Error() string { |
| 20 | + return fmt.Sprintf("errors in package: %v", e.Pkg.Errors) |
| 21 | +} |
| 22 | + |
| 23 | +type FailedPrerequisitesError struct { |
| 24 | + errors map[string][]string |
| 25 | +} |
| 26 | + |
| 27 | +func (f FailedPrerequisitesError) NotEmpty() bool { |
| 28 | + return len(f.errors) > 0 |
| 29 | +} |
| 30 | + |
| 31 | +func (f *FailedPrerequisitesError) Consume(name string, err error) { |
| 32 | + if f.errors == nil { |
| 33 | + f.errors = map[string][]string{} |
| 34 | + } |
| 35 | + k := fmt.Sprintf("%v", err) |
| 36 | + f.errors[k] = append(f.errors[k], name) |
| 37 | +} |
| 38 | + |
| 39 | +func (f FailedPrerequisitesError) Error() string { |
| 40 | + var errs []string |
| 41 | + for err := range f.errors { |
| 42 | + errs = append(errs, err) |
| 43 | + } |
| 44 | + var groups []groupedPrerequisiteErr |
| 45 | + for _, err := range errs { |
| 46 | + groups = append(groups, groupedPrerequisiteErr{ |
| 47 | + err: err, |
| 48 | + names: f.errors[err], |
| 49 | + }) |
| 50 | + } |
| 51 | + return fmt.Sprintf("failed prerequisites: %s", groups) |
| 52 | +} |
| 53 | + |
| 54 | +type groupedPrerequisiteErr struct { |
| 55 | + names []string |
| 56 | + err string |
| 57 | +} |
| 58 | + |
| 59 | +func (g groupedPrerequisiteErr) String() string { |
| 60 | + if len(g.names) == 1 { |
| 61 | + return fmt.Sprintf("%s: %s", g.names[0], g.err) |
| 62 | + } |
| 63 | + return fmt.Sprintf("(%s): %s", strings.Join(g.names, ", "), g.err) |
| 64 | +} |
| 65 | + |
| 66 | +func buildIssuesFromErrorsForTypecheckMode(errs []error, lintCtx *linter.Context) ([]result.Issue, error) { |
| 67 | + var issues []result.Issue |
| 68 | + uniqReportedIssues := map[string]bool{} |
| 69 | + for _, err := range errs { |
| 70 | + itErr, ok := errors.Cause(err).(*IllTypedError) |
| 71 | + if !ok { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + for _, err := range libpackages.ExtractErrors(itErr.Pkg) { |
| 75 | + i, perr := parseError(err) |
| 76 | + if perr != nil { // failed to parse |
| 77 | + if uniqReportedIssues[err.Msg] { |
| 78 | + continue |
| 79 | + } |
| 80 | + uniqReportedIssues[err.Msg] = true |
| 81 | + lintCtx.Log.Errorf("typechecking error: %s", err.Msg) |
| 82 | + } else { |
| 83 | + i.Pkg = itErr.Pkg // to save to cache later |
| 84 | + issues = append(issues, *i) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return issues, nil |
| 89 | +} |
| 90 | + |
| 91 | +func parseError(srcErr packages.Error) (*result.Issue, error) { |
| 92 | + pos, err := libpackages.ParseErrorPosition(srcErr.Pos) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + return &result.Issue{ |
| 98 | + Pos: *pos, |
| 99 | + Text: srcErr.Msg, |
| 100 | + FromLinter: "typecheck", |
| 101 | + }, nil |
| 102 | +} |
0 commit comments