@@ -44,18 +44,21 @@ type Checker struct {
44
44
}
45
45
46
46
// Prepare extracts a patch and changed lines.
47
+ // It should only be used with [Checker.IsNewIssue].
47
48
func (c * Checker ) Prepare (ctx context.Context ) error {
48
- err := c .preparePatch (ctx )
49
+ err := c .loadPatch (ctx )
49
50
50
51
c .changes = c .linesChanged ()
51
52
52
53
return err
53
54
}
54
55
55
56
// IsNewIssue checks whether issue found by linter is new: it was found in changed lines.
57
+ // It requires to call [Checker.Prepare] before call this method to load the changes from patch.
56
58
func (c * Checker ) IsNewIssue (i InputIssue ) (hunkPos int , isNew bool ) {
57
- fchanges , ok := c .changes [filepath .ToSlash (i .FilePath ())]
58
- if ! ok { // file wasn't changed
59
+ changes , ok := c .changes [filepath .ToSlash (i .FilePath ())]
60
+ if ! ok {
61
+ // file wasn't changed
59
62
return 0 , false
60
63
}
61
64
@@ -67,16 +70,18 @@ func (c *Checker) IsNewIssue(i InputIssue) (hunkPos int, isNew bool) {
67
70
fpos pos
68
71
changed bool
69
72
)
73
+
70
74
// found file, see if lines matched
71
- for _ , pos := range fchanges {
75
+ for _ , pos := range changes {
72
76
if pos .lineNo == i .Line () {
73
77
fpos = pos
74
78
changed = true
79
+
75
80
break
76
81
}
77
82
}
78
83
79
- if changed || fchanges == nil {
84
+ if changed || changes == nil {
80
85
// either file changed or it's a new file
81
86
hunkPos := fpos .lineNo
82
87
@@ -91,7 +96,7 @@ func (c *Checker) IsNewIssue(i InputIssue) (hunkPos int, isNew bool) {
91
96
return 0 , false
92
97
}
93
98
94
- // Check scans reader and writes any lines to writer that have been added in Checker.Patch.
99
+ // Check scans reader and writes any lines to writer that have been added in [ Checker.Patch] .
95
100
//
96
101
// Returns the issues written to writer when no error occurs.
97
102
//
@@ -100,8 +105,9 @@ func (c *Checker) IsNewIssue(i InputIssue) (hunkPos int, isNew bool) {
100
105
//
101
106
// File paths in reader must be relative to current working directory or absolute.
102
107
func (c * Checker ) Check (ctx context.Context , reader io.Reader , writer io.Writer ) (issues []Issue , err error ) {
103
- returnErr := c .Prepare (ctx )
104
- writeAll := returnErr != nil
108
+ errPrepare := c .Prepare (ctx )
109
+
110
+ writeAll := errPrepare != nil
105
111
106
112
// file.go:lineNo:colNo:message
107
113
// colNo is optional, strip spaces before message
@@ -121,7 +127,7 @@ func (c *Checker) Check(ctx context.Context, reader io.Reader, writer io.Writer)
121
127
if absPath == "" {
122
128
absPath , err = os .Getwd ()
123
129
if err != nil {
124
- returnErr = fmt .Errorf ("could not get current working directory: %w" , err )
130
+ errPrepare = fmt .Errorf ("could not get current working directory: %w" , err )
125
131
}
126
132
}
127
133
@@ -189,10 +195,10 @@ func (c *Checker) Check(ctx context.Context, reader io.Reader, writer io.Writer)
189
195
}
190
196
191
197
if err := scanner .Err (); err != nil {
192
- returnErr = fmt .Errorf ("error reading standard input: %w" , err )
198
+ errPrepare = fmt .Errorf ("error reading standard input: %w" , err )
193
199
}
194
200
195
- return issues , returnErr
201
+ return issues , errPrepare
196
202
}
197
203
198
204
func (c * Checker ) debugf (format string , s ... any ) {
@@ -204,8 +210,8 @@ func (c *Checker) debugf(format string, s ...any) {
204
210
_ , _ = fmt .Fprintf (c .Debug , format + "\n " , s ... )
205
211
}
206
212
207
- // preparePatch checks if patch is supplied, if not, retrieve from VCS.
208
- func (c * Checker ) preparePatch (ctx context.Context ) error {
213
+ // loadPatch checks if patch is supplied, if not, retrieve from VCS.
214
+ func (c * Checker ) loadPatch (ctx context.Context ) error {
209
215
if c .Patch != nil {
210
216
return nil
211
217
}
@@ -311,6 +317,8 @@ func (c *Checker) linesChanged() map[string][]pos {
311
317
}
312
318
313
319
type pos struct {
314
- lineNo int // line number
315
- hunkPos int // position relative to first @@ in file
320
+ // Line number.
321
+ lineNo int
322
+ // Position relative to first @@ in file.
323
+ hunkPos int
316
324
}
0 commit comments