Skip to content

Commit b8641a2

Browse files
willbeasonJeffFaer
andauthored
feat: Add warning if skip_lines causes empty block (#124)
Fixes: #123 Specifically, this triggers if skip_lines causes the block's start to be at or after the block's end. So fully-empty blocks with no skip_lines (or no-op skip_lines, such as skip_lines=0,0) are still fine. Modify tests in skip_lines golden test to show this behavior. Modify how start and end are logged in cmd.go. As-is causes the slightly confusing behavior that "end" is always logged before start because it comes first alphabetically. Now these are logged as [start,end]=[x,y]. The warning for if start>=end shows both the original start/end and their offsets to make it more obvious how the values are arrived at. This helps finding the problematic lines, as otherwise you just see where the block start and end is, not where the directives are. --------- Co-authored-by: Jeffrey Faer <jeffrey.faer@gmail.com>
1 parent 4c4bb29 commit b8641a2

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func fix(fixer *keepsorted.Fixer, filenames []string, modifiedLines []keepsorted
237237
if warn.Lines.Start == warn.Lines.End {
238238
log = log.Int("line", warn.Lines.Start)
239239
} else {
240-
log = log.Int("start", warn.Lines.Start).Int("end", warn.Lines.End)
240+
log = log.Ints("[start,end]", []int{warn.Lines.Start, warn.Lines.End})
241241
}
242242
log.Msg(warn.Message)
243243
}

goldens/skip_lines.err

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
WRN block start is at or after end, possibly due to skip_lines [6+1,7-0] [start,end]=[6,7]
2+
WRN block start is at or after end, possibly due to skip_lines [10+0,11-1] [start,end]=[10,11]
3+
WRN block start is at or after end, possibly due to skip_lines [23+10,27-0] [start,end]=[23,27]
4+
exit status 1

goldens/skip_lines.in

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
Skip lines with an empty block:
1+
Skip no lines with an empty block is ok:
2+
keep-sorted-test start skip_lines=0
3+
keep-sorted-test end
4+
5+
Skip lines at start with an empty block:
26
keep-sorted-test start skip_lines=1
37
keep-sorted-test end
48

9+
Skip lines at end with an empty block:
10+
keep-sorted-test start skip_lines=-1
11+
keep-sorted-test end
12+
513
Skip two lines:
614
// keep-sorted-test start skip_lines=2
715
foo
@@ -11,7 +19,7 @@ b
1119
a
1220
// keep-sorted-test end
1321

14-
Number of skipped lines is greater than block size, so block is ignored:
22+
Number of skipped lines is greater than block size, so skip_lines causes warning:
1523
// keep-sorted-test start skip_lines=10
1624
z
1725
y

goldens/skip_lines.out

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
Skip lines with an empty block:
1+
Skip no lines with an empty block is ok:
2+
keep-sorted-test start skip_lines=0
3+
keep-sorted-test end
4+
5+
Skip lines at start with an empty block:
26
keep-sorted-test start skip_lines=1
37
keep-sorted-test end
48

9+
Skip lines at end with an empty block:
10+
keep-sorted-test start skip_lines=-1
11+
keep-sorted-test end
12+
513
Skip two lines:
614
// keep-sorted-test start skip_lines=2
715
foo
@@ -11,7 +19,7 @@ b
1119
c
1220
// keep-sorted-test end
1321

14-
Number of skipped lines is greater than block size, so block is ignored:
22+
Number of skipped lines is greater than block size, so skip_lines causes warning:
1523
// keep-sorted-test start skip_lines=10
1624
z
1725
y

keepsorted/block.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package keepsorted
1616

1717
import (
18+
"fmt"
1819
"slices"
1920
"strings"
2021

@@ -116,6 +117,10 @@ func (f *Fixer) newBlocks(filename string, lines []string, offset int, include f
116117
start.index += opts.startOffset()
117118
endIndex += opts.endOffset()
118119
if start.index >= endIndex {
120+
warnings = append(warnings, finding(filename, start.index-opts.startOffset()+offset, endIndex-opts.endOffset()+offset,
121+
fmt.Sprintf("block start is at or after end, possibly due to skip_lines [%d+%d,%d-%d]",
122+
start.index-opts.startOffset()+offset, opts.startOffset(), endIndex-opts.endOffset()+offset, -opts.endOffset())),
123+
)
119124
continue
120125
}
121126

0 commit comments

Comments
 (0)