Skip to content

Commit 2ce4abc

Browse files
authored
Merge pull request #228 from oferchen/codex/update-comment-formatting-in-multiple-files
Allow //go:build lines in comment check
2 parents ab3a613 + e3b44c8 commit 2ce4abc

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

cmd/commentcheck/main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ func check(root string) error {
3535
if err != nil {
3636
return err
3737
}
38+
var nonBuild []token.Position
3839
for _, cg := range file.Comments {
3940
for _, c := range cg.List {
40-
if fset.Position(c.Slash).Line > 1 {
41-
bad = append(bad, path)
42-
return nil
41+
if strings.HasPrefix(c.Text, "//go:build") {
42+
continue
4343
}
44+
nonBuild = append(nonBuild, fset.Position(c.Slash))
4445
}
4546
}
47+
if len(nonBuild) != 1 || nonBuild[0].Line != 1 {
48+
bad = append(bad, path)
49+
}
4650
return nil
4751
}
4852
if err := filepath.WalkDir(root, walk); err != nil {

cmd/commentcheck/main_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ import (
1010
func TestCheck(t *testing.T) {
1111
t.Run("compliant", func(t *testing.T) {
1212
dir := t.TempDir()
13-
write(t, dir, "ok.go", "// ok.go\npackage main\n")
13+
write(t, dir, "ok.go", "// ok.go\n//go:build test\n\npackage main\n")
1414
if err := check(dir); err != nil {
1515
t.Fatalf("unexpected error: %v", err)
1616
}
1717
})
18-
t.Run("noncompliant", func(t *testing.T) {
18+
t.Run("noncompliant extra comment", func(t *testing.T) {
1919
dir := t.TempDir()
20-
write(t, dir, "bad.go", "// bad.go\npackage main\n// bad\n")
20+
write(t, dir, "bad.go", "// bad.go\n//go:build test\n\npackage main\n// bad\n")
21+
if err := check(dir); err == nil {
22+
t.Fatalf("expected error")
23+
}
24+
})
25+
t.Run("noncompliant missing comment", func(t *testing.T) {
26+
dir := t.TempDir()
27+
write(t, dir, "bad.go", "//go:build test\n\npackage main\n")
2128
if err := check(dir); err == nil {
2229
t.Fatalf("expected error")
2330
}

internal/fs/ewindows_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
// internal/fs/ewindows_other.go
12
//go:build !windows
23

3-
// internal/fs/ewindows_other.go
44
package fs
55

66
func isErrWindows(err error) bool {

internal/fs/ewindows_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
// internal/fs/ewindows_windows.go
12
//go:build windows
23

3-
// internal/fs/ewindows_windows.go
44
package fs
55

66
import (

internal/fs/stdio.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// internal/fs/stdio.go
12
package fs
23

34
import "io"

0 commit comments

Comments
 (0)