-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathissue.go
100 lines (76 loc) · 1.97 KB
/
issue.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package result
import (
"crypto/md5" //nolint:gosec
"fmt"
"go/token"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"
)
type Range struct {
From, To int
}
type Replacement struct {
NeedOnlyDelete bool // need to delete all lines of the issue without replacement with new lines
NewLines []string // if NeedDelete is false it's the replacement lines
Inline *InlineFix
}
type InlineFix struct {
StartCol int // zero-based
Length int // length of chunk to be replaced
NewString string
}
type Issue struct {
FromLinter string
Text string
SuggestedFixes []analysis.SuggestedFix
Severity string
// Source lines of a code with the issue to show
SourceLines []string
// If we know how to fix the issue we can provide replacement lines
Replacement *Replacement
// Pkg is needed for proper caching of linting results
Pkg *packages.Package `json:"-"`
LineRange *Range `json:",omitempty"`
Pos token.Position
// HunkPos is used only when golangci-lint is run over a diff
HunkPos int `json:",omitempty"`
// If we are expecting a nolint (because this is from nolintlint), record the expected linter
ExpectNoLint bool
ExpectedNoLintLinter string
}
func (i *Issue) FilePath() string {
return i.Pos.Filename
}
func (i *Issue) Line() int {
return i.Pos.Line
}
func (i *Issue) Column() int {
return i.Pos.Column
}
func (i *Issue) GetLineRange() Range {
if i.LineRange == nil {
return Range{
From: i.Line(),
To: i.Line(),
}
}
if i.LineRange.From == 0 {
return Range{
From: i.Line(),
To: i.Line(),
}
}
return *i.LineRange
}
func (i *Issue) Description() string {
return fmt.Sprintf("%s: %s", i.FromLinter, i.Text)
}
func (i *Issue) Fingerprint() string {
firstLine := ""
if len(i.SourceLines) > 0 {
firstLine = i.SourceLines[0]
}
hash := md5.New() //nolint:gosec
_, _ = hash.Write([]byte(fmt.Sprintf("%s%s%s", i.Pos.Filename, i.Text, firstLine)))
return fmt.Sprintf("%X", hash.Sum(nil))
}