Skip to content

Commit

Permalink
fix: 'package-comments' false positive for CRLF sources
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Feb 21, 2025
1 parent 9177f50 commit 8a00ee6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# These Go files must have CRLF endings
testdata/package_comments_issue607*.go text eol=crlf
9 changes: 9 additions & 0 deletions lint/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func (f *File) Content() []byte {
return f.content
}

// IsCRLFTerminated returns if the file is terminated with CRLF.
func (f *File) IsCRLFTerminated() bool {
return bytes.HasSuffix(f.content, []byte("\r\n"))
}

// NewFile creates a new file
func NewFile(name string, content []byte, pkg *Package) (*File, error) {
f, err := parser.ParseFile(pkg.fset, name, content, parser.ParseComments)
Expand All @@ -48,6 +53,10 @@ func (f *File) ToPosition(pos token.Pos) token.Position {
return f.Pkg.fset.Position(pos)
}

func (f *File) Fset() *token.FileSet {
return f.Pkg.fset
}

Check warning on line 58 in lint/file.go

View workflow job for this annotation

GitHub Actions / Lint

exported method File.Fset should have comment or be unexported

// Render renders a node.
func (f *File) Render(x any) string {
var buf bytes.Buffer
Expand Down
7 changes: 6 additions & 1 deletion rule/package_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ func (l *lintPackageComments) Visit(_ ast.Node) ast.Visitor {
if lastCG != nil && strings.HasPrefix(lastCG.Text(), prefix) {
endPos := l.file.ToPosition(lastCG.End())
pkgPos := l.file.ToPosition(l.fileAst.Package)
if endPos.Line+1 < pkgPos.Line {
isDetached := endPos.Line+1 < pkgPos.Line
if l.file.IsCRLFTerminated() {
// Workaround for https://github.com/mgechev/revive/issues/607
isDetached = endPos.Line+2 < pkgPos.Line
}
if isDetached {
// There isn't a great place to anchor this error;
// the start of the blank lines between the doc and the package statement
// is at least pointing at the location of the problem.
Expand Down
16 changes: 16 additions & 0 deletions test/package_comments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test

import (
"testing"

"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)

func TestPackageCommentsIssue607NotMatch(t *testing.T) {
testRule(t, "package_comments_issue607_not_match", &rule.PackageCommentsRule{}, &lint.RuleConfig{})
}

func TestPackageCommentsIssue607Match(t *testing.T) {
testRule(t, "package_comments_issue607_match", &rule.PackageCommentsRule{}, &lint.RuleConfig{})
}
8 changes: 8 additions & 0 deletions testdata/package_comments_issue607_match.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Package fixtures has a multi-line comment.
Its line endings have a carriage return.
*/

package fixtures

// MATCH:4 /package comment is detached; there should be no blank lines between it and the package statement/
5 changes: 5 additions & 0 deletions testdata/package_comments_issue607_not_match.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
Package fixtures has a multi-line comment.
Its line endings have a carriage return.
*/
package fixtures

0 comments on commit 8a00ee6

Please sign in to comment.