@@ -26,8 +26,6 @@ import (
2626 "fillmore-labs.com/zerolint/pkg/internal/filter"
2727 "fillmore-labs.com/zerolint/pkg/internal/set"
2828 "golang.org/x/tools/go/analysis"
29- "golang.org/x/tools/go/analysis/passes/inspect"
30- "golang.org/x/tools/go/ast/inspector"
3129)
3230
3331// run performs the analysis to identify excluded types.
@@ -37,35 +35,15 @@ import (
3735// 3. Types with a "//zerolint:exclude" comment.
3836// It exports an [excludeFact] fact for each newly identified excluded type in the current package
3937// and returns a [filter.Filter] containing the token.Pos of all excluded type definitions.
40- func run (pass * analysis.Pass ) (any , error ) { //nolint:cyclop
41- addExclusions (pass )
42-
43- in , ok := pass .ResultOf [inspect .Analyzer ].(* inspector.Inspector )
44- if ! ok {
45- return nil , ErrNoInspectorResult
46- }
47-
48- for decl := range inspector.All [* ast.GenDecl ](in ) {
49- if decl .Tok != token .TYPE {
50- continue
51- }
38+ // In run.go
5239
53- // Exclude via "//zerolint:exclude" comment.
54- excludeAll := hasExcludeComment (decl .Doc )
55-
56- for _ , spec := range decl .Specs {
57- if ts , ok := spec .(* ast.TypeSpec ); ok {
58- // Exclude cgo-generated types.
59- if strings .HasPrefix (ts .Name .Name , "_Ctype_" ) {
60- excludeType (pass , ts )
61-
62- continue
63- }
40+ func run (pass * analysis.Pass ) (any , error ) {
41+ addExclusions (pass )
6442
65- // Exclude via "//zerolint:exclude" comment.
66- if excludeAll || hasExcludeComment ( ts . Doc ) || hasExcludeComment ( ts . Comment ) {
67- excludeType ( pass , ts )
68- }
43+ for _ , f := range pass . Files {
44+ for _ , decl := range f . Decls {
45+ if genDecl , ok := decl .( * ast. GenDecl ); ok {
46+ processGenDecl ( pass , genDecl )
6947 }
7048 }
7149 }
@@ -81,24 +59,52 @@ func run(pass *analysis.Pass) (any, error) { //nolint:cyclop
8159 return filter .New (excludedTypeDefs ), nil
8260}
8361
62+ func processGenDecl (pass * analysis.Pass , genDecl * ast.GenDecl ) {
63+ if genDecl .Tok != token .TYPE {
64+ return
65+ }
66+
67+ // Exclude all via "//zerolint:exclude" comment on the type block.
68+ excludeAll := hasExcludeComment (genDecl .Doc )
69+
70+ for _ , spec := range genDecl .Specs {
71+ ts , ok := spec .(* ast.TypeSpec )
72+ if ! ok {
73+ continue
74+ }
75+
76+ // Exclude cgo-generated types.
77+ if strings .HasPrefix (ts .Name .Name , "_Ctype_" ) {
78+ excludeType (pass , ts )
79+
80+ continue
81+ }
82+
83+ // Exclude via "//zerolint:exclude" comment on the type spec itself or its associated comment.
84+ if excludeAll || hasExcludeComment (ts .Doc ) || hasExcludeComment (ts .Comment ) {
85+ excludeType (pass , ts )
86+ }
87+ }
88+ }
89+
8490// hasExcludeComment checks if a comment group contains "zerolint:exclude".
8591func hasExcludeComment (comments * ast.CommentGroup ) bool {
8692 if comments == nil {
8793 return false
8894 }
8995
9096 for _ , comment := range comments .List {
91- i := strings .Index (comment .Text , zerolintMarker )
92- if i < 0 {
97+ text := strings .TrimLeft (comment .Text , "/ " )
98+ if ! strings . HasPrefix ( text , zerolintMarker ) {
9399 continue
94100 }
95101
96- argsPart := strings .Fields ( comment . Text [ i + len (zerolintMarker ):])
97- if len (argsPart ) == 0 {
102+ argsText , _ , _ := strings .Cut ( text [ len (zerolintMarker ):], " " )
103+ if len (argsText ) == 0 {
98104 continue
99105 }
100106
101- args := strings .Split (argsPart [ 0 ] , "," )
107+ args := strings .Split (argsText , "," )
102108 if slices .Contains (args , "exclude" ) {
103109 return true
104110 }
0 commit comments