Skip to content

Commit 0d8d190

Browse files
committed
Fix panic when comment occur in the last line
Signed-off-by: Xiang Dai <[email protected]>
1 parent 66f1df7 commit 0d8d190

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

pkg/gci/gci.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,19 @@ func newPkg(data [][]byte, localFlag string) *pkg {
6161
}
6262
}
6363

64-
for i := len(formatData) - 1; i >= 0; i-- {
64+
n := len(formatData)
65+
for i := n - 1; i >= 0; i-- {
6566
line := formatData[i]
6667

6768
// check commentFlag:
6869
// 1. one line commentFlag
6970
// 2. commentFlag after import path
7071
commentIndex := strings.Index(line, commentFlag)
7172
if commentIndex == 0 {
73+
// comment in the last line is useless, ignore it
74+
if i+1 >= n {
75+
continue
76+
}
7277
pkg, _, _ := getPkgInfo(formatData[i+1], strings.Index(formatData[i+1], commentFlag) >= 0)
7378
p.comment[pkg] = line
7479
continue

0 commit comments

Comments
 (0)