Skip to content

Commit

Permalink
Add more checks to avoid panics.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Sep 20, 2021
1 parent b60172c commit 9f0a33a
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func checkCommentForPeriod(c comment) *Issue {
// attached lines. Use `iss.Pos.Column` because it's a position in
// the original line.
original := c.lines[pos.line-1]
if len(original) < iss.Pos.Column-1 {
// This should never happen. Avoid panics, skip this check.
return nil
}
iss.Replacement = original[:iss.Pos.Column-1] + "." +
original[iss.Pos.Column-1:]

Expand Down Expand Up @@ -117,14 +121,18 @@ func checkCommentForCapital(c comment) []Issue {
Message: noCapitalMessage,
}

// Make a replacement. Use `pos.line` to get an original line from
// Make a replacement. Use `pos.original` to get an original original from
// attached lines. Use `iss.Pos.Column` because it's a position in
// the original line.
line := c.lines[pos.line-1]
col := byteToRuneColumn(line, iss.Pos.Column) - 1
rep := string(unicode.ToTitle([]rune(line)[col])) // capital letter
iss.Replacement = line[:iss.Pos.Column-1] + rep +
line[iss.Pos.Column-1+len(rep):]
// the original original.
original := c.lines[pos.line-1]
col := byteToRuneColumn(original, iss.Pos.Column) - 1
rep := string(unicode.ToTitle([]rune(original)[col])) // capital letter
if len(original) < iss.Pos.Column-1+len(rep) {
// This should never happen. Avoid panics, skip this check.
continue
}
iss.Replacement = original[:iss.Pos.Column-1] + rep +
original[iss.Pos.Column-1+len(rep):]

// Save replacement to raw lines to be able to combine it with
// further replacements
Expand Down

0 comments on commit 9f0a33a

Please sign in to comment.