Skip to content

Commit 0115764

Browse files
committed
Switch back to iterating over struct fields
1 parent 6173f51 commit 0115764

File tree

9 files changed

+41
-181
lines changed

9 files changed

+41
-181
lines changed

pkg/analysis/commentstart/analyzer.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,24 @@ func run(pass *analysis.Pass) (interface{}, error) {
4040
return nil, errCouldNotGetJSONTags
4141
}
4242

43-
// Filter to Fields so that we can iterate over fields in a struct.
43+
// Filter to structs so that we can iterate over fields in a struct.
4444
nodeFilter := []ast.Node{
45-
(*ast.Field)(nil),
45+
(*ast.StructType)(nil),
4646
}
4747

4848
inspect.Preorder(nodeFilter, func(n ast.Node) {
49-
field, ok := n.(*ast.Field)
49+
sTyp, ok := n.(*ast.StructType)
5050
if !ok {
5151
return
5252
}
5353

54-
checkField(pass, field, jsonTags)
54+
if sTyp.Fields == nil {
55+
return
56+
}
57+
58+
for _, field := range sTyp.Fields.List {
59+
checkField(pass, field, jsonTags)
60+
}
5561
})
5662

5763
return nil, nil //nolint:nilnil

pkg/analysis/helpers/doc.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ The helpers are used to extract data from the types, and provide common function
55
The available helpers are:
66
- [extractjsontags]: Extracts JSON tags from struct fields and returns the information in a structured format.
77
- [markers]: Extracts marker information from types and returns the information in a structured format.
8-
- [structfield]: Provides a mapping of *ast.Field to the parent *ast.StructType.
98
109
Helpers should expose an *analysis.Analyzer as a globabl variable.
1110
Other linters will use the `Requires` configuration to ensure that the helper is run before the linter.

pkg/analysis/helpers/structfield/analyzer.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

pkg/analysis/helpers/structfield/doc.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

pkg/analysis/integers/analyzer.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"go/ast"
66

7-
"github.com/JoelSpeed/kal/pkg/analysis/helpers/structfield"
87
"github.com/JoelSpeed/kal/pkg/analysis/utils"
98
"golang.org/x/tools/go/analysis"
109
"golang.org/x/tools/go/analysis/passes/inspect"
@@ -14,8 +13,7 @@ import (
1413
const name = "integers"
1514

1615
var (
17-
errCouldNotGetInspector = errors.New("could not get inspector")
18-
errCouldNotGetStructField = errors.New("could not get struct field")
16+
errCouldNotGetInspector = errors.New("could not get inspector")
1917
)
2018

2119
// Analyzer is the analyzer for the integers package.
@@ -24,7 +22,7 @@ var Analyzer = &analysis.Analyzer{
2422
Name: name,
2523
Doc: "All integers should be explicit about their size, int32 and int64 should be used over plain int. Unsigned ints are not allowed.",
2624
Run: run,
27-
Requires: []*analysis.Analyzer{inspect.Analyzer, structfield.Analyzer},
25+
Requires: []*analysis.Analyzer{inspect.Analyzer},
2826
}
2927

3028
func run(pass *analysis.Pass) (interface{}, error) {
@@ -33,15 +31,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
3331
return nil, errCouldNotGetInspector
3432
}
3533

36-
structField, ok := pass.ResultOf[structfield.Analyzer].(structfield.StructField)
37-
if !ok {
38-
return nil, errCouldNotGetStructField
39-
}
40-
4134
// Filter to fields so that we can look at fields within structs.
4235
// Filter typespecs so that we can look at type aliases.
4336
nodeFilter := []ast.Node{
44-
(*ast.Field)(nil),
37+
(*ast.StructType)(nil),
4538
(*ast.TypeSpec)(nil),
4639
}
4740

@@ -52,13 +45,6 @@ func run(pass *analysis.Pass) (interface{}, error) {
5245
//
5346
// We use the filter defined above, ensuring we only look at struct fields and type declarations.
5447
inspect.Preorder(nodeFilter, func(n ast.Node) {
55-
if field, ok := n.(*ast.Field); ok {
56-
// Do not inspect fields that are not part of a struct.
57-
if structType := structField.StructForField(field); structType == nil {
58-
return
59-
}
60-
}
61-
6248
typeChecker.CheckNode(pass, n)
6349
})
6450

pkg/analysis/jsontags/analyzer.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"regexp"
88

99
"github.com/JoelSpeed/kal/pkg/analysis/helpers/extractjsontags"
10-
"github.com/JoelSpeed/kal/pkg/analysis/helpers/structfield"
1110
"github.com/JoelSpeed/kal/pkg/config"
1211

1312
"golang.org/x/tools/go/analysis"
@@ -23,9 +22,8 @@ const (
2322
)
2423

2524
var (
26-
errCouldNotGetInspector = errors.New("could not get inspector")
27-
errCouldNotGetJSONTags = errors.New("could not get json tags")
28-
errCouldNotCreateStructField = errors.New("could not create new structField")
25+
errCouldNotGetInspector = errors.New("could not get inspector")
26+
errCouldNotGetJSONTags = errors.New("could not get json tags")
2927
)
3028

3129
type analyzer struct {
@@ -49,7 +47,7 @@ func newAnalyzer(cfg config.JSONTagsConfig) (*analysis.Analyzer, error) {
4947
Name: name,
5048
Doc: "Check that all struct fields in an API are tagged with json tags",
5149
Run: a.run,
52-
Requires: []*analysis.Analyzer{inspect.Analyzer, extractjsontags.Analyzer, structfield.Analyzer},
50+
Requires: []*analysis.Analyzer{inspect.Analyzer, extractjsontags.Analyzer},
5351
}, nil
5452
}
5553

@@ -64,28 +62,24 @@ func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {
6462
return nil, errCouldNotGetJSONTags
6563
}
6664

67-
structFields, ok := pass.ResultOf[structfield.Analyzer].(structfield.StructField)
68-
if !ok {
69-
return nil, errCouldNotCreateStructField
70-
}
71-
7265
// Filter to fields so that we can iterate over fields in a struct.
7366
nodeFilter := []ast.Node{
74-
(*ast.Field)(nil),
67+
(*ast.StructType)(nil),
7568
}
7669

7770
inspect.Preorder(nodeFilter, func(n ast.Node) {
78-
field, ok := n.(*ast.Field)
71+
sTyp, ok := n.(*ast.StructType)
7972
if !ok {
8073
return
8174
}
8275

83-
if structType := structFields.StructForField(field); structType == nil {
84-
// This field does not belong to a struct.
76+
if sTyp.Fields == nil {
8577
return
8678
}
8779

88-
a.checkField(pass, field, jsonTags)
80+
for _, field := range sTyp.Fields.List {
81+
a.checkField(pass, field, jsonTags)
82+
}
8983
})
9084

9185
return nil, nil //nolint:nilnil

pkg/analysis/nobools/analyzer.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"go/ast"
66

7-
"github.com/JoelSpeed/kal/pkg/analysis/helpers/structfield"
87
"github.com/JoelSpeed/kal/pkg/analysis/utils"
98
"golang.org/x/tools/go/analysis"
109
"golang.org/x/tools/go/analysis/passes/inspect"
@@ -14,8 +13,7 @@ import (
1413
const name = "nobools"
1514

1615
var (
17-
errCouldNotGetInspector = errors.New("could not get inspector")
18-
errCouldNotGetStructField = errors.New("could not get struct field")
16+
errCouldNotGetInspector = errors.New("could not get inspector")
1917
)
2018

2119
// Analyzer is the analyzer for the nobools package.
@@ -24,7 +22,7 @@ var Analyzer = &analysis.Analyzer{
2422
Name: name,
2523
Doc: "Boolean values cannot evolve over time, use an enum with meaningful values instead",
2624
Run: run,
27-
Requires: []*analysis.Analyzer{inspect.Analyzer, structfield.Analyzer},
25+
Requires: []*analysis.Analyzer{inspect.Analyzer},
2826
}
2927

3028
func run(pass *analysis.Pass) (interface{}, error) {
@@ -33,15 +31,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
3331
return nil, errCouldNotGetInspector
3432
}
3533

36-
structField, ok := pass.ResultOf[structfield.Analyzer].(structfield.StructField)
37-
if !ok {
38-
return nil, errCouldNotGetStructField
39-
}
40-
4134
// Filter to fields so that we can look at fields within structs.
4235
// Filter typespecs so that we can look at type aliases.
4336
nodeFilter := []ast.Node{
44-
(*ast.Field)(nil),
37+
(*ast.StructType)(nil),
4538
(*ast.TypeSpec)(nil),
4639
}
4740

@@ -52,13 +45,6 @@ func run(pass *analysis.Pass) (interface{}, error) {
5245
//
5346
// We use the filter defined above, ensuring we only look at struct fields and type declarations.
5447
inspect.Preorder(nodeFilter, func(n ast.Node) {
55-
if field, ok := n.(*ast.Field); ok {
56-
// Do not inspect fields that are not part of a struct.
57-
if structType := structField.StructForField(field); structType == nil {
58-
return
59-
}
60-
}
61-
6248
typeChecker.CheckNode(pass, n)
6349
})
6450

pkg/analysis/optionalorrequired/analyzer.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/JoelSpeed/kal/pkg/analysis/helpers/extractjsontags"
99
"github.com/JoelSpeed/kal/pkg/analysis/helpers/markers"
10-
"github.com/JoelSpeed/kal/pkg/analysis/helpers/structfield"
1110
"github.com/JoelSpeed/kal/pkg/config"
1211
"golang.org/x/tools/go/analysis"
1312
"golang.org/x/tools/go/analysis/passes/inspect"
@@ -31,10 +30,9 @@ const (
3130
)
3231

3332
var (
34-
errCouldNotGetInspector = errors.New("could not get inspector")
35-
errCouldNotGetMarkers = errors.New("could not get markers")
36-
errCouldNotGetJSONTags = errors.New("could not get jsontags")
37-
errCouldNotCreateStructField = errors.New("could not create new structField")
33+
errCouldNotGetInspector = errors.New("could not get inspector")
34+
errCouldNotGetMarkers = errors.New("could not get markers")
35+
errCouldNotGetJSONTags = errors.New("could not get jsontags")
3836
)
3937

4038
type analyzer struct {
@@ -73,7 +71,7 @@ func newAnalyzer(cfg config.OptionalOrRequiredConfig) *analysis.Analyzer {
7371
Name: name,
7472
Doc: "Checks that all struct fields are marked either with the optional or required markers.",
7573
Run: a.run,
76-
Requires: []*analysis.Analyzer{inspect.Analyzer, markers.Analyzer, extractjsontags.Analyzer, structfield.Analyzer},
74+
Requires: []*analysis.Analyzer{inspect.Analyzer, markers.Analyzer, extractjsontags.Analyzer},
7775
}
7876
}
7977

@@ -93,31 +91,27 @@ func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {
9391
return nil, errCouldNotGetJSONTags
9492
}
9593

96-
structField, ok := pass.ResultOf[structfield.Analyzer].(structfield.StructField)
97-
if !ok {
98-
return nil, errCouldNotCreateStructField
99-
}
100-
10194
// Filter to fields so that we can iterate over fields in a struct.
10295
nodeFilter := []ast.Node{
103-
(*ast.Field)(nil),
96+
(*ast.StructType)(nil),
10497
}
10598

10699
inspect.Preorder(nodeFilter, func(n ast.Node) {
107-
field, ok := n.(*ast.Field)
100+
sTyp, ok := n.(*ast.StructType)
108101
if !ok {
109102
return
110103
}
111104

112-
if structType := structField.StructForField(field); structType == nil {
113-
// This field does not belong to a struct.
105+
if sTyp.Fields == nil {
114106
return
115107
}
116108

117-
fieldMarkers := markersAccess.FieldMarkers(field)
118-
fieldTagInfo := jsonTags.FieldTags(field)
109+
for _, field := range sTyp.Fields.List {
110+
fieldMarkers := markersAccess.FieldMarkers(field)
111+
fieldTagInfo := jsonTags.FieldTags(field)
119112

120-
a.checkField(pass, field, fieldMarkers, fieldTagInfo)
113+
a.checkField(pass, field, fieldMarkers, fieldTagInfo)
114+
}
121115
})
122116

123117
return nil, nil //nolint:nilnil

0 commit comments

Comments
 (0)