From e3b44c8a3a23b0d26ab5754fde734bb6200bbd0f Mon Sep 17 00:00:00 2001 From: Ofer Chen Date: Fri, 29 Aug 2025 22:28:21 +0200 Subject: [PATCH] feat: allow go build tags and enforce single banner comment --- cmd/commentcheck/main.go | 10 +++++++--- cmd/commentcheck/main_test.go | 13 ++++++++++--- internal/fs/ewindows_other.go | 2 +- internal/fs/ewindows_windows.go | 2 +- internal/fs/stdio.go | 1 + 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/cmd/commentcheck/main.go b/cmd/commentcheck/main.go index 9f84c22..bf67c9b 100644 --- a/cmd/commentcheck/main.go +++ b/cmd/commentcheck/main.go @@ -35,14 +35,18 @@ func check(root string) error { if err != nil { return err } + var nonBuild []token.Position for _, cg := range file.Comments { for _, c := range cg.List { - if fset.Position(c.Slash).Line > 1 { - bad = append(bad, path) - return nil + if strings.HasPrefix(c.Text, "//go:build") { + continue } + nonBuild = append(nonBuild, fset.Position(c.Slash)) } } + if len(nonBuild) != 1 || nonBuild[0].Line != 1 { + bad = append(bad, path) + } return nil } if err := filepath.WalkDir(root, walk); err != nil { diff --git a/cmd/commentcheck/main_test.go b/cmd/commentcheck/main_test.go index ef62e55..d491efe 100644 --- a/cmd/commentcheck/main_test.go +++ b/cmd/commentcheck/main_test.go @@ -10,14 +10,21 @@ import ( func TestCheck(t *testing.T) { t.Run("compliant", func(t *testing.T) { dir := t.TempDir() - write(t, dir, "ok.go", "// ok.go\npackage main\n") + write(t, dir, "ok.go", "// ok.go\n//go:build test\n\npackage main\n") if err := check(dir); err != nil { t.Fatalf("unexpected error: %v", err) } }) - t.Run("noncompliant", func(t *testing.T) { + t.Run("noncompliant extra comment", func(t *testing.T) { dir := t.TempDir() - write(t, dir, "bad.go", "// bad.go\npackage main\n// bad\n") + write(t, dir, "bad.go", "// bad.go\n//go:build test\n\npackage main\n// bad\n") + if err := check(dir); err == nil { + t.Fatalf("expected error") + } + }) + t.Run("noncompliant missing comment", func(t *testing.T) { + dir := t.TempDir() + write(t, dir, "bad.go", "//go:build test\n\npackage main\n") if err := check(dir); err == nil { t.Fatalf("expected error") } diff --git a/internal/fs/ewindows_other.go b/internal/fs/ewindows_other.go index 2dc77e5..f3c7da6 100644 --- a/internal/fs/ewindows_other.go +++ b/internal/fs/ewindows_other.go @@ -1,6 +1,6 @@ +// internal/fs/ewindows_other.go //go:build !windows -// internal/fs/ewindows_other.go package fs func isErrWindows(err error) bool { diff --git a/internal/fs/ewindows_windows.go b/internal/fs/ewindows_windows.go index b9469a8..f6f1f6f 100644 --- a/internal/fs/ewindows_windows.go +++ b/internal/fs/ewindows_windows.go @@ -1,6 +1,6 @@ +// internal/fs/ewindows_windows.go //go:build windows -// internal/fs/ewindows_windows.go package fs import ( diff --git a/internal/fs/stdio.go b/internal/fs/stdio.go index 5714157..f92ad73 100644 --- a/internal/fs/stdio.go +++ b/internal/fs/stdio.go @@ -1,3 +1,4 @@ +// internal/fs/stdio.go package fs import "io"