forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource_code.go
57 lines (45 loc) · 1.45 KB
/
source_code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package processors
import (
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
var _ Processor = (*SourceCode)(nil)
// SourceCode modifies displayed information based on [result.Issue.GetLineRange()].
//
// This is used:
// - to display the "UnderLinePointer".
// - in some rare cases to display multiple lines instead of one (ex: `dupl`)
//
// It requires to use [fsutils.LineCache] ([fsutils.FileCache]) to get the file information before the fixes.
type SourceCode struct {
lineCache *fsutils.LineCache
log logutils.Log
}
func NewSourceCode(lc *fsutils.LineCache, log logutils.Log) *SourceCode {
return &SourceCode{
lineCache: lc,
log: log,
}
}
func (SourceCode) Name() string {
return "source_code"
}
func (p SourceCode) Process(issues []result.Issue) ([]result.Issue, error) {
return transformIssues(issues, p.transform), nil
}
func (SourceCode) Finish() {}
func (p SourceCode) transform(issue *result.Issue) *result.Issue {
newIssue := *issue
lineRange := issue.GetLineRange()
for lineNumber := lineRange.From; lineNumber <= lineRange.To; lineNumber++ {
line, err := p.lineCache.GetLine(issue.FilePath(), lineNumber)
if err != nil {
p.log.Warnf("Failed to get line %d for file %s: %s",
lineNumber, issue.FilePath(), err)
return issue
}
newIssue.SourceLines = append(newIssue.SourceLines, line)
}
return &newIssue
}