Skip to content

Commit 91bd9a7

Browse files
committed
fix: add offest with // comment
1 parent 03b6d1a commit 91bd9a7

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pkg/golinters/goheader/goheader.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,20 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error {
7676

7777
f := pass.Fset.File(file.Pos())
7878

79+
commentLine := 1
80+
var offset int
81+
82+
// Inspired by https://github.com/denis-tingaikin/go-header/blob/4c75a6a2332f025705325d6c71fff4616aedf48f/analyzer.go#L85-L92
83+
if len(file.Comments) > 0 && file.Comments[0].Pos() < file.Package {
84+
if !strings.HasPrefix(file.Comments[0].List[0].Text, "/*") {
85+
// When the comment are "//" there is a one character offset.
86+
offset = 1
87+
}
88+
commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line
89+
}
90+
7991
diag := analysis.Diagnostic{
80-
Pos: f.LineStart(issue.Location().Line+1) + token.Pos(issue.Location().Position), // The position of the first divergence.
92+
Pos: f.LineStart(issue.Location().Line+1) + token.Pos(issue.Location().Position-offset), // The position of the first divergence.
8193
Message: issue.Message(),
8294
}
8395

@@ -87,13 +99,6 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error {
8799
current += len(s)
88100
}
89101

90-
commentLine := 1
91-
92-
// Inspired by https://github.com/denis-tingaikin/go-header/blob/4c75a6a2332f025705325d6c71fff4616aedf48f/analyzer.go#L85-L92
93-
if len(file.Comments) > 0 && file.Comments[0].Pos() < file.Package {
94-
commentLine = goanalysis.GetFilePositionFor(pass.Fset, file.Comments[0].Pos()).Line
95-
}
96-
97102
start := f.LineStart(commentLine)
98103

99104
end := start + token.Pos(current)

0 commit comments

Comments
 (0)