|
| 1 | +package processors |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/token" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/golangci/golangci-lint/pkg/logutils" |
| 9 | + |
| 10 | + "github.com/golangci/golangci-lint/pkg/lint/astcache" |
| 11 | + |
| 12 | + "github.com/golangci/golangci-lint/pkg/result" |
| 13 | +) |
| 14 | + |
| 15 | +type posMapper func(pos token.Position) token.Position |
| 16 | + |
| 17 | +// FilenameUnadjuster is needed because a lot of linters use fset.Position(f.Pos()) |
| 18 | +// to get filename. And they return adjusted filename (e.g. *.qtpl) for an issue. We need |
| 19 | +// restore real .go filename to properly output it, parse it, etc. |
| 20 | +type FilenameUnadjuster struct { |
| 21 | + m map[string]posMapper // map from adjusted filename to position mapper: adjusted -> unadjusted position |
| 22 | + log logutils.Log |
| 23 | +} |
| 24 | + |
| 25 | +var _ Processor = FilenameUnadjuster{} |
| 26 | + |
| 27 | +func NewFilenameUnadjuster(cache *astcache.Cache, log logutils.Log) *FilenameUnadjuster { |
| 28 | + m := map[string]posMapper{} |
| 29 | + for _, f := range cache.GetAllValidFiles() { |
| 30 | + adjustedFilename := f.Fset.PositionFor(f.F.Pos(), true).Filename |
| 31 | + if adjustedFilename == "" { |
| 32 | + continue |
| 33 | + } |
| 34 | + unadjustedFilename := f.Fset.PositionFor(f.F.Pos(), false).Filename |
| 35 | + if unadjustedFilename == "" || unadjustedFilename == adjustedFilename { |
| 36 | + continue |
| 37 | + } |
| 38 | + if !strings.HasSuffix(unadjustedFilename, ".go") { |
| 39 | + continue // file.go -> /caches/cgo-xxx |
| 40 | + } |
| 41 | + |
| 42 | + f := f |
| 43 | + m[adjustedFilename] = func(adjustedPos token.Position) token.Position { |
| 44 | + tokenFile := f.Fset.File(f.F.Pos()) |
| 45 | + if tokenFile == nil { |
| 46 | + log.Warnf("Failed to get token file for %s", adjustedFilename) |
| 47 | + return adjustedPos |
| 48 | + } |
| 49 | + return f.Fset.PositionFor(tokenFile.Pos(adjustedPos.Offset), false) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return &FilenameUnadjuster{ |
| 54 | + m: m, |
| 55 | + log: log, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (p FilenameUnadjuster) Name() string { |
| 60 | + return "filename_unadjuster" |
| 61 | +} |
| 62 | + |
| 63 | +func (p FilenameUnadjuster) Process(issues []result.Issue) ([]result.Issue, error) { |
| 64 | + return transformIssues(issues, func(i *result.Issue) *result.Issue { |
| 65 | + issueFilePath := i.FilePath() |
| 66 | + if !filepath.IsAbs(i.FilePath()) { |
| 67 | + absPath, err := filepath.Abs(i.FilePath()) |
| 68 | + if err != nil { |
| 69 | + p.log.Warnf("failed to build abs path for %q: %s", i.FilePath(), err) |
| 70 | + return i |
| 71 | + } |
| 72 | + issueFilePath = absPath |
| 73 | + } |
| 74 | + |
| 75 | + mapper := p.m[issueFilePath] |
| 76 | + if mapper == nil { |
| 77 | + return i |
| 78 | + } |
| 79 | + |
| 80 | + newI := *i |
| 81 | + newI.Pos = mapper(i.Pos) |
| 82 | + p.log.Infof("Unadjusted from %v to %v", i.Pos, newI.Pos) |
| 83 | + return &newI |
| 84 | + }), nil |
| 85 | +} |
| 86 | + |
| 87 | +func (FilenameUnadjuster) Finish() {} |
0 commit comments