Skip to content

Commit e41434a

Browse files
committed
Fix offset value in returned issues.
1 parent 66dbcbd commit e41434a

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

checks.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,18 @@ func checkPeriod(c comment) *Issue {
9292
strings.Split(c.text, "\n")[pos.line-1],
9393
)
9494

95+
// Get the offset of the first symbol in the last line of the comment.
96+
// This value is used only in golangci-lint to point to the problem, and
97+
// to replace the problem when running in auto-fix mode.
98+
offset := c.start.Offset
99+
for i := 0; i < pos.line-1; i++ {
100+
offset += len(c.lines[i]) + 1
101+
}
102+
95103
iss := Issue{
96104
Pos: token.Position{
97105
Filename: c.start.Filename,
98-
Offset: c.start.Offset,
106+
Offset: offset,
99107
Line: pos.line + c.start.Line - 1,
100108
Column: pos.column,
101109
},

checks_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestCheckPeriod(t *testing.T) {
6363
issue: &Issue{
6464
Pos: token.Position{
6565
Filename: start.Filename,
66+
Offset: 0,
6667
Line: 1,
6768
Column: 16,
6869
},
@@ -80,6 +81,7 @@ func TestCheckPeriod(t *testing.T) {
8081
issue: &Issue{
8182
Pos: token.Position{
8283
Filename: start.Filename,
84+
Offset: 10,
8385
Line: 3,
8486
Column: 6,
8587
},
@@ -244,8 +246,8 @@ func TestCheckPeriod(t *testing.T) {
244246
case tt.issue != nil && issue == nil:
245247
t.Fatalf("Expected issue, got nil")
246248
case issue.Pos != tt.issue.Pos:
247-
t.Fatalf("Wrong position\n expected: %+v\n got: %+v",
248-
tt.issue.Pos, issue.Pos)
249+
t.Fatalf("Wrong position\n expected: %+v [%d]\n got: %+v [%d]",
250+
tt.issue.Pos, tt.issue.Pos.Offset, issue.Pos, issue.Pos.Offset)
249251
case issue.Message != tt.issue.Message:
250252
t.Fatalf("Wrong message\n expected: %s\n got: %s",
251253
tt.issue.Message, issue.Message)

0 commit comments

Comments
 (0)