Skip to content

Commit 7fd5702

Browse files
committed
Ensure type declarations are picked up across file boundaries
1 parent 0c6bb03 commit 7fd5702

File tree

16 files changed

+170
-11
lines changed

16 files changed

+170
-11
lines changed

pkg/analysis/helpers/inspector/analyzer.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"reflect"
2020

2121
"golang.org/x/tools/go/analysis"
22-
"golang.org/x/tools/go/analysis/passes/inspect"
2322
astinspector "golang.org/x/tools/go/ast/inspector"
2423

2524
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
@@ -36,15 +35,12 @@ var Analyzer = &analysis.Analyzer{
3635
Name: name,
3736
Doc: "Provides common functionality for analyzers that need to inspect fields and struct",
3837
Run: run,
39-
Requires: []*analysis.Analyzer{inspect.Analyzer, extractjsontags.Analyzer, markers.Analyzer},
38+
Requires: []*analysis.Analyzer{extractjsontags.Analyzer, markers.Analyzer},
4039
ResultType: reflect.TypeOf(newInspector(nil, nil, nil)),
4140
}
4241

4342
func run(pass *analysis.Pass) (any, error) {
44-
astinspector, ok := pass.ResultOf[inspect.Analyzer].(*astinspector.Inspector)
45-
if !ok {
46-
return nil, kalerrors.ErrCouldNotGetInspector
47-
}
43+
astInspector := astinspector.New(pass.Files)
4844

4945
jsonTags, ok := pass.ResultOf[extractjsontags.Analyzer].(extractjsontags.StructFieldTags)
5046
if !ok {
@@ -56,5 +52,5 @@ func run(pass *analysis.Pass) (any, error) {
5652
return nil, kalerrors.ErrCouldNotGetMarkers
5753
}
5854

59-
return newInspector(astinspector, jsonTags, markersAccess), nil
55+
return newInspector(astInspector, jsonTags, markersAccess), nil
6056
}

pkg/analysis/integers/testdata/src/a/a.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ type Integers struct {
8686
InvalidMapIntToString map[int]string // want "field InvalidMapIntToString map key should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
8787

8888
InvalidMapUIntToString map[uint]string // want "field InvalidMapUIntToString map key should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
89+
90+
InvalidIntFromAnotherFile IntB // want "field InvalidIntFromAnotherFile type IntB should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
91+
92+
InvalidSliceIntAliasFromAnotherFile InvalidSliceIntAliasB // want "field InvalidSliceIntAliasFromAnotherFile type InvalidSliceIntAliasB array element should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
8993
}
9094

9195
// DoNothing is used to check that the analyser doesn't report on methods.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package a
2+
3+
// IntB is being used to show a type from a different file.
4+
type IntB int // want "type IntB should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
5+
6+
type InvalidSliceIntAliasB []int // want "type InvalidSliceIntAliasB array element should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

pkg/analysis/maxlength/analyzer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2525
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
2626
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
27+
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
2728
)
2829

2930
const (
@@ -74,13 +75,13 @@ func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Mar
7475
}
7576

7677
func checkIdent(pass *analysis.Pass, ident *ast.Ident, node ast.Node, aliases []*ast.TypeSpec, markersAccess markers.Markers, prefix, marker string, needsMaxLength func(markers.MarkerSet) bool) {
77-
if ident.Obj == nil { // Built-in type
78+
if utils.IsBasicType(pass, ident) { // Built-in type
7879
checkString(pass, ident, node, aliases, markersAccess, prefix, marker, needsMaxLength)
7980

8081
return
8182
}
8283

83-
tSpec, ok := ident.Obj.Decl.(*ast.TypeSpec)
84+
tSpec, ok := utils.LookupTypeSpec(pass, ident)
8485
if !ok {
8586
return
8687
}

pkg/analysis/maxlength/testdata/src/a/a.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ type MaxLength struct {
99

1010
StringAliasWithMaxLengthOnAlias StringAliasWithMaxLength
1111

12+
StringAliasFromAnotherFile StringAliasB // want "field StringAliasFromAnotherFile type StringAliasB must have a maximum length, add kubebuilder:validation:MaxLength marker"
13+
14+
// +kubebuilder:validation:MaxLength:=128
15+
StringAliasFromAnotherFileWithMaxLengthOnField StringAliasB
16+
17+
StringAliasWithMaxLengthFromAnotherFile StringAliasWithMaxLengthB
18+
1219
StringWithoutMaxLength string // want "field StringWithoutMaxLength must have a maximum length, add kubebuilder:validation:MaxLength marker"
1320

1421
StringAliasWithoutMaxLength StringAlias // want "field StringAliasWithoutMaxLength type StringAlias must have a maximum length, add kubebuilder:validation:MaxLength marker"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package a
2+
3+
// StringAliasB is a string without a MaxLength.
4+
type StringAliasB string
5+
6+
// StringAliasWithMaxLengthB is a string with a MaxLength.
7+
// +kubebuilder:validation:MaxLength:=512
8+
type StringAliasWithMaxLengthB string

pkg/analysis/nobools/testdata/src/a/a.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type Bools struct {
3232
InvalidMapBoolToString map[bool]string // want "field InvalidMapBoolToString map key should not use a bool. Use a string type with meaningful constant values as an enum."
3333

3434
InvalidMapBoolPtrToString map[*bool]string // want "field InvalidMapBoolPtrToString map key pointer should not use a bool. Use a string type with meaningful constant values as an enum."
35+
36+
InvalidBoolAliasFromAnotherFile BoolAliasB // want "field InvalidBoolAliasFromAnotherFile type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."
37+
38+
InvalidBoolPtrAliasFromAnotherFile *BoolAliasB // want "field InvalidBoolPtrAliasFromAnotherFile pointer type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."
3539
}
3640

3741
// DoNothing is used to check that the analyser doesn't report on methods.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package a
2+
3+
type BoolAliasB bool // want "type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."
4+
5+
type BoolAliasPtrB *bool // want "type BoolAliasPtrB pointer should not use a bool. Use a string type with meaningful constant values as an enum."

pkg/analysis/nofloats/testdata/src/a/a.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ type Floats struct {
5656
InvalidMapFloat32PtrToString map[*float32]string // want "field InvalidMapFloat32PtrToString map key pointer should not use a float value because they cannot be reliably round-tripped."
5757

5858
InvalidMapFloat64PtrToString map[*float64]string // want "field InvalidMapFloat64PtrToString map key pointer should not use a float value because they cannot be reliably round-tripped."
59+
60+
InvalidFloat32AliasFromAnotherFile Float32AliasB // want "field InvalidFloat32AliasFromAnotherFile type Float32AliasB should not use a float value because they cannot be reliably round-tripped."
61+
62+
InvalidFloat32PtrAliasFromAnotherFile Float32AliasPtrB // want "field InvalidFloat32PtrAliasFromAnotherFile type Float32AliasPtrB pointer should not use a float value because they cannot be reliably round-tripped."
5963
}
6064

6165
// DoNothingFloat32 is used to check that the analyser doesn't report on methods.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package a
2+
3+
type Float32AliasB float32 // want "type Float32AliasB should not use a float value because they cannot be reliably round-tripped."
4+
5+
type Float32AliasPtrB *float32 // want "type Float32AliasPtrB pointer should not use a float value because they cannot be reliably round-tripped."

0 commit comments

Comments
 (0)