@@ -44,18 +44,21 @@ type Checker struct {
4444}
4545
4646// Prepare extracts a patch and changed lines.
47+ // It should only be used with [Checker.IsNewIssue].
4748func (c * Checker ) Prepare (ctx context.Context ) error {
48- err := c .preparePatch (ctx )
49+ err := c .loadPatch (ctx )
4950
5051 c .changes = c .linesChanged ()
5152
5253 return err
5354}
5455
5556// 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.
5658func (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
5962 return 0 , false
6063 }
6164
@@ -67,16 +70,18 @@ func (c *Checker) IsNewIssue(i InputIssue) (hunkPos int, isNew bool) {
6770 fpos pos
6871 changed bool
6972 )
73+
7074 // found file, see if lines matched
71- for _ , pos := range fchanges {
75+ for _ , pos := range changes {
7276 if pos .lineNo == i .Line () {
7377 fpos = pos
7478 changed = true
79+
7580 break
7681 }
7782 }
7883
79- if changed || fchanges == nil {
84+ if changed || changes == nil {
8085 // either file changed or it's a new file
8186 hunkPos := fpos .lineNo
8287
@@ -91,7 +96,7 @@ func (c *Checker) IsNewIssue(i InputIssue) (hunkPos int, isNew bool) {
9196 return 0 , false
9297}
9398
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] .
95100//
96101// Returns the issues written to writer when no error occurs.
97102//
@@ -100,8 +105,9 @@ func (c *Checker) IsNewIssue(i InputIssue) (hunkPos int, isNew bool) {
100105//
101106// File paths in reader must be relative to current working directory or absolute.
102107func (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
105111
106112 // file.go:lineNo:colNo:message
107113 // colNo is optional, strip spaces before message
@@ -121,7 +127,7 @@ func (c *Checker) Check(ctx context.Context, reader io.Reader, writer io.Writer)
121127 if absPath == "" {
122128 absPath , err = os .Getwd ()
123129 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 )
125131 }
126132 }
127133
@@ -189,10 +195,10 @@ func (c *Checker) Check(ctx context.Context, reader io.Reader, writer io.Writer)
189195 }
190196
191197 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 )
193199 }
194200
195- return issues , returnErr
201+ return issues , errPrepare
196202}
197203
198204func (c * Checker ) debugf (format string , s ... any ) {
@@ -204,8 +210,8 @@ func (c *Checker) debugf(format string, s ...any) {
204210 _ , _ = fmt .Fprintf (c .Debug , format + "\n " , s ... )
205211}
206212
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 {
209215 if c .Patch != nil {
210216 return nil
211217 }
@@ -311,6 +317,8 @@ func (c *Checker) linesChanged() map[string][]pos {
311317}
312318
313319type 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
316324}
0 commit comments