Skip to content

Commit 879cdef

Browse files
author
Nishanth Shanmugham
committed
use go1.21 ast.IsGenerated when available
1 parent 829aae6 commit 879cdef

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

comment.go

-33
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,9 @@ package exhaustive
33
import (
44
"go/ast"
55
"go/token"
6-
"regexp"
76
"strings"
87
)
98

10-
// For definition of generated file see:
11-
// http://golang.org/s/generatedcode
12-
13-
var generatedCodeRe = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)
14-
15-
func isGeneratedFile(file *ast.File) bool {
16-
// NOTE: file.Comments includes file.Doc as well, so no need
17-
// to separately check file.Doc.
18-
for _, c := range file.Comments {
19-
for _, cc := range c.List {
20-
// This check handles the "must appear before the first
21-
// non-comment, non-blank text in the file" requirement.
22-
//
23-
// According to https://golang.org/ref/spec#Source_file_organization
24-
// the package clause is the first element in a file, which
25-
// should make it the first non-comment, non-blank text.
26-
if c.Pos() >= file.Package {
27-
return false
28-
}
29-
// According to the docs:
30-
// '\r' has been removed.
31-
// '\n' has been removed for //-style comments
32-
// This has also been manually verified.
33-
if generatedCodeRe.MatchString(cc.Text) {
34-
return true
35-
}
36-
}
37-
}
38-
39-
return false
40-
}
41-
429
const (
4310
ignoreComment = "//exhaustive:ignore"
4411
enforceComment = "//exhaustive:enforce"

comment_go121.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build go1.21
2+
3+
package exhaustive
4+
5+
import (
6+
"go/ast"
7+
)
8+
9+
func isGeneratedFile(file *ast.File) bool {
10+
return ast.IsGenerated(file)
11+
}

comment_pre_go121.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build !go1.21
2+
3+
package exhaustive
4+
5+
import (
6+
"go/ast"
7+
"regexp"
8+
)
9+
10+
// For definition of generated file see:
11+
// http://golang.org/s/generatedcode
12+
13+
var generatedCodeRe = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)
14+
15+
func isGeneratedFile(file *ast.File) bool {
16+
for _, c := range file.Comments {
17+
for _, cc := range c.List {
18+
if cc.Pos() > file.Package {
19+
break
20+
}
21+
if generatedCodeRe.MatchString(cc.Text) {
22+
return true
23+
}
24+
}
25+
}
26+
return false
27+
}

0 commit comments

Comments
 (0)