Skip to content

Commit b78c0ea

Browse files
committed
Include declaration comments for markers
1 parent 3f04947 commit b78c0ea

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

pkg/analysis/helpers/markers/analyzer.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,23 @@ func run(pass *analysis.Pass) (interface{}, error) {
118118
}
119119

120120
nodeFilter := []ast.Node{
121-
(*ast.TypeSpec)(nil),
121+
// In order to get the godoc comments from a type
122+
// definition as such:
123+
//
124+
// // comment
125+
// type Foo struct {...}
126+
//
127+
// We need to use the ast.GenDecl type instead of the
128+
// ast.TypeSpec type. The ast.TypeSpec.Doc field will only
129+
// be populated if types are defined as such:
130+
//
131+
// type(
132+
// // comment
133+
// Foo struct {...}
134+
// )
135+
//
136+
// For more information, see https://github.com/golang/go/issues/27477
137+
(*ast.GenDecl)(nil),
122138
(*ast.Field)(nil),
123139
}
124140

@@ -129,8 +145,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
129145

130146
inspect.Preorder(nodeFilter, func(n ast.Node) {
131147
switch typ := n.(type) {
132-
case *ast.TypeSpec:
133-
extractTypeSpecMarkers(typ, results)
148+
case *ast.GenDecl:
149+
extractGenDeclMarkers(typ, results)
134150
case *ast.Field:
135151
extractFieldMarkers(typ, results)
136152
}
@@ -139,21 +155,30 @@ func run(pass *analysis.Pass) (interface{}, error) {
139155
return results, nil
140156
}
141157

142-
func extractTypeSpecMarkers(typ *ast.TypeSpec, results *markers) {
143-
typeMarkers := NewMarkerSet()
158+
func extractGenDeclMarkers(typ *ast.GenDecl, results *markers) {
159+
declMarkers := NewMarkerSet()
144160

145161
if typ.Doc != nil {
146162
for _, comment := range typ.Doc.List {
147163
if marker := extractMarker(comment); marker.Identifier != "" {
148-
typeMarkers.Insert(marker)
164+
declMarkers.Insert(marker)
149165
}
150166
}
151167
}
152168

153-
results.insertTypeMarkers(typ, typeMarkers)
169+
if len(typ.Specs) == 0 {
170+
return
171+
}
172+
173+
tSpec, ok := typ.Specs[0].(*ast.TypeSpec)
174+
if !ok {
175+
return
176+
}
177+
178+
results.insertTypeMarkers(tSpec, declMarkers)
154179

155-
if uTyp, ok := typ.Type.(*ast.StructType); ok {
156-
results.insertStructMarkers(uTyp, typeMarkers)
180+
if sTyp, ok := tSpec.Type.(*ast.StructType); ok {
181+
results.insertStructMarkers(sTyp, declMarkers)
157182
}
158183
}
159184

0 commit comments

Comments
 (0)