Skip to content

Commit b446a4e

Browse files
authored
Merge pull request #191 from JoelSpeed/qualified-names-for-type-checker
Update typechecker to use qualified name
2 parents 28b6469 + 20a53e8 commit b446a4e

File tree

8 files changed

+198
-125
lines changed

8 files changed

+198
-125
lines changed

pkg/analysis/helpers/inspector/inspector.go

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"go/ast"
2121
"go/token"
2222
"go/types"
23-
"slices"
2423

2524
astinspector "golang.org/x/tools/go/ast/inspector"
2625
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
@@ -102,7 +101,7 @@ func (i *inspector) inspectFields(inspectField func(field *ast.Field, jsonTagInf
102101
// The 0th node in the stack is the *ast.File.
103102
file, ok := stack[0].(*ast.File)
104103
if ok {
105-
structName = getStructName(file, field)
104+
structName = utils.GetStructNameFromFile(file, field)
106105
}
107106

108107
if structName != "" {
@@ -202,45 +201,3 @@ func printDebugInfo(field *ast.Field) string {
202201

203202
return debug
204203
}
205-
206-
func getStructName(file *ast.File, field *ast.Field) string {
207-
var (
208-
structName string
209-
found bool
210-
)
211-
212-
ast.Inspect(file, func(n ast.Node) bool {
213-
if found {
214-
return false
215-
}
216-
217-
typeSpec, ok := n.(*ast.TypeSpec)
218-
if !ok {
219-
return true
220-
}
221-
222-
structType, ok := typeSpec.Type.(*ast.StructType)
223-
if !ok {
224-
return true
225-
}
226-
227-
structName = typeSpec.Name.Name
228-
229-
if structType.Fields == nil {
230-
return true
231-
}
232-
233-
if slices.Contains(structType.Fields.List, field) {
234-
found = true
235-
return false
236-
}
237-
238-
return true
239-
})
240-
241-
if found {
242-
return structName
243-
}
244-
245-
return ""
246-
}

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,39 @@ type Integers struct {
1313

1414
ValidInt64Ptr *int64
1515

16-
InvalidInt int // want "field InvalidInt should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
16+
InvalidInt int // want "field Integers.InvalidInt should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
1717

18-
InvalidIntPtr *int // want "field InvalidIntPtr pointer should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
18+
InvalidIntPtr *int // want "field Integers.InvalidIntPtr pointer should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
1919

20-
InvalidInt8 int8 // want "field InvalidInt8 should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
20+
InvalidInt8 int8 // want "field Integers.InvalidInt8 should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
2121

22-
InvalidInt16 int16 // want "field InvalidInt16 should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
22+
InvalidInt16 int16 // want "field Integers.InvalidInt16 should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
2323

24-
InvalidUInt uint // want "field InvalidUInt should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
24+
InvalidUInt uint // want "field Integers.InvalidUInt should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
2525

26-
InvalidUIntPtr uint // want "field InvalidUIntPtr should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
26+
InvalidUIntPtr uint // want "field Integers.InvalidUIntPtr should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
2727

28-
InvalidUInt8 uint8 // want "field InvalidUInt8 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
28+
InvalidUInt8 uint8 // want "field Integers.InvalidUInt8 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
2929

30-
InvalidUInt16 uint16 // want "field InvalidUInt16 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
30+
InvalidUInt16 uint16 // want "field Integers.InvalidUInt16 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
3131

32-
InvalidUInt32 uint32 // want "field InvalidUInt32 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
32+
InvalidUInt32 uint32 // want "field Integers.InvalidUInt32 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
3333

34-
InvalidUInt64 uint64 // want "field InvalidUInt64 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
34+
InvalidUInt64 uint64 // want "field Integers.InvalidUInt64 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
3535

3636
ValidInt32Alias ValidInt32Alias
3737

3838
ValidInt32AliasPtr *ValidInt32Alias
3939

40-
InvalidIntAlias InvalidIntAlias // want "field InvalidIntAlias type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
40+
InvalidIntAlias InvalidIntAlias // want "field Integers.InvalidIntAlias type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
4141

42-
InvalidIntAliasPtr *InvalidIntAlias // want "field InvalidIntAliasPtr pointer type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
42+
InvalidIntAliasPtr *InvalidIntAlias // want "field Integers.InvalidIntAliasPtr pointer type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
4343

44-
InvalidUIntAlias InvalidUIntAlias // want "field InvalidUIntAlias type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
44+
InvalidUIntAlias InvalidUIntAlias // want "field Integers.InvalidUIntAlias type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
4545

46-
InvalidUIntAliasPtr *InvalidUIntAlias // want "field InvalidUIntAliasPtr pointer type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
46+
InvalidUIntAliasPtr *InvalidUIntAlias // want "field Integers.InvalidUIntAliasPtr pointer type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
4747

48-
InvalidIntAliasAlias InvalidIntAliasAlias // want "field InvalidIntAliasAlias type InvalidIntAliasAlias type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
48+
InvalidIntAliasAlias InvalidIntAliasAlias // want "field Integers.InvalidIntAliasAlias type InvalidIntAliasAlias type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
4949

5050
ValidSliceInt32 []int32
5151

@@ -55,41 +55,41 @@ type Integers struct {
5555

5656
ValidSliceInt64Ptr []*int64
5757

58-
InvalidSliceInt []int // want "field InvalidSliceInt array element should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
58+
InvalidSliceInt []int // want "field Integers.InvalidSliceInt array element should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
5959

60-
InvalidSliceIntPtr []*int // want "field InvalidSliceIntPtr array element pointer should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
60+
InvalidSliceIntPtr []*int // want "field Integers.InvalidSliceIntPtr array element pointer should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
6161

62-
InvalidSliceUInt []uint // want "field InvalidSliceUInt array element should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
62+
InvalidSliceUInt []uint // want "field Integers.InvalidSliceUInt array element should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
6363

64-
InvalidSliceUIntPtr []*uint // want "field InvalidSliceUIntPtr array element pointer should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
64+
InvalidSliceUIntPtr []*uint // want "field Integers.InvalidSliceUIntPtr array element pointer should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
6565

66-
InvalidSliceIntAlias []InvalidIntAlias // want "field InvalidSliceIntAlias array element type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
66+
InvalidSliceIntAlias []InvalidIntAlias // want "field Integers.InvalidSliceIntAlias array element type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
6767

68-
InvalidSliceIntAliasPtr []*InvalidIntAlias // want "field InvalidSliceIntAliasPtr array element pointer type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
68+
InvalidSliceIntAliasPtr []*InvalidIntAlias // want "field Integers.InvalidSliceIntAliasPtr array element pointer type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
6969

70-
InvalidSliceUIntAlias []InvalidUIntAlias // want "field InvalidSliceUIntAlias array element type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
70+
InvalidSliceUIntAlias []InvalidUIntAlias // want "field Integers.InvalidSliceUIntAlias array element type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
7171

72-
InvalidSliceUIntAliasPtr []*InvalidUIntAlias // want "field InvalidSliceUIntAliasPtr array element pointer type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
72+
InvalidSliceUIntAliasPtr []*InvalidUIntAlias // want "field Integers.InvalidSliceUIntAliasPtr array element pointer type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
7373

7474
ValidMapStringToInt32 map[string]int32
7575

7676
ValidMapStringToInt64 map[string]int64
7777

78-
InvalidMapStringToInt map[string]int // want "field InvalidMapStringToInt map value should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
78+
InvalidMapStringToInt map[string]int // want "field Integers.InvalidMapStringToInt map value should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
7979

80-
InvalidMapStringToUInt map[string]uint // want "field InvalidMapStringToUInt map value should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
80+
InvalidMapStringToUInt map[string]uint // want "field Integers.InvalidMapStringToUInt map value should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
8181

8282
ValidMapInt32ToString map[int32]string
8383

8484
ValidMapInt64ToString map[int64]string
8585

86-
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"
86+
InvalidMapIntToString map[int]string // want "field Integers.InvalidMapIntToString map key should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
8787

88-
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"
88+
InvalidMapUIntToString map[uint]string // want "field Integers.InvalidMapUIntToString map key should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
8989

90-
InvalidIntFromAnotherFile IntB // want "field InvalidIntFromAnotherFile type IntB should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
90+
InvalidIntFromAnotherFile IntB // want "field Integers.InvalidIntFromAnotherFile type IntB should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
9191

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"
92+
InvalidSliceIntAliasFromAnotherFile InvalidSliceIntAliasB // want "field Integers.InvalidSliceIntAliasFromAnotherFile type InvalidSliceIntAliasB array element should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
9393
}
9494

9595
// DoNothing is used to check that the analyser doesn't report on methods.

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@ type Bools struct {
99

1010
ValidInt64 int64
1111

12-
InvalidBool bool // want "field InvalidBool should not use a bool. Use a string type with meaningful constant values as an enum."
12+
InvalidBool bool // want "field Bools.InvalidBool should not use a bool. Use a string type with meaningful constant values as an enum."
1313

14-
InvalidBoolPtr *bool // want "field InvalidBoolPtr pointer should not use a bool. Use a string type with meaningful constant values as an enum."
14+
InvalidBoolPtr *bool // want "field Bools.InvalidBoolPtr pointer should not use a bool. Use a string type with meaningful constant values as an enum."
1515

16-
InvalidBoolSlice []bool // want "field InvalidBoolSlice array element should not use a bool. Use a string type with meaningful constant values as an enum."
16+
InvalidBoolSlice []bool // want "field Bools.InvalidBoolSlice array element should not use a bool. Use a string type with meaningful constant values as an enum."
1717

18-
InvalidBoolPtrSlice []*bool // want "field InvalidBoolPtrSlice array element pointer should not use a bool. Use a string type with meaningful constant values as an enum."
18+
InvalidBoolPtrSlice []*bool // want "field Bools.InvalidBoolPtrSlice array element pointer should not use a bool. Use a string type with meaningful constant values as an enum."
1919

20-
InvalidBoolAlias BoolAlias // want "field InvalidBoolAlias type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
20+
InvalidBoolAlias BoolAlias // want "field Bools.InvalidBoolAlias type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
2121

22-
InvalidBoolPtrAlias *BoolAlias // want "field InvalidBoolPtrAlias pointer type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
22+
InvalidBoolPtrAlias *BoolAlias // want "field Bools.InvalidBoolPtrAlias pointer type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
2323

24-
InvalidBoolSliceAlias []BoolAlias // want "field InvalidBoolSliceAlias array element type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
24+
InvalidBoolSliceAlias []BoolAlias // want "field Bools.InvalidBoolSliceAlias array element type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
2525

26-
InvalidBoolPtrSliceAlias []*BoolAlias // want "field InvalidBoolPtrSliceAlias array element pointer type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
26+
InvalidBoolPtrSliceAlias []*BoolAlias // want "field Bools.InvalidBoolPtrSliceAlias array element pointer type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
2727

28-
InvalidMapStringToBool map[string]bool // want "field InvalidMapStringToBool map value should not use a bool. Use a string type with meaningful constant values as an enum."
28+
InvalidMapStringToBool map[string]bool // want "field Bools.InvalidMapStringToBool map value should not use a bool. Use a string type with meaningful constant values as an enum."
2929

30-
InvalidMapStringToBoolPtr map[string]*bool // want "field InvalidMapStringToBoolPtr map value pointer should not use a bool. Use a string type with meaningful constant values as an enum."
30+
InvalidMapStringToBoolPtr map[string]*bool // want "field Bools.InvalidMapStringToBoolPtr map value pointer should not use a bool. Use a string type with meaningful constant values as an enum."
3131

32-
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."
32+
InvalidMapBoolToString map[bool]string // want "field Bools.InvalidMapBoolToString map key should not use a bool. Use a string type with meaningful constant values as an enum."
3333

34-
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."
34+
InvalidMapBoolPtrToString map[*bool]string // want "field Bools.InvalidMapBoolPtrToString map key pointer should not use a bool. Use a string type with meaningful constant values as an enum."
3535

36-
InvalidBoolAliasFromAnotherFile BoolAliasB // want "field InvalidBoolAliasFromAnotherFile type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."
36+
InvalidBoolAliasFromAnotherFile BoolAliasB // want "field Bools.InvalidBoolAliasFromAnotherFile type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."
3737

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."
38+
InvalidBoolPtrAliasFromAnotherFile *BoolAliasB // want "field Bools.InvalidBoolPtrAliasFromAnotherFile pointer type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."
3939
}
4040

4141
// DoNothing is used to check that the analyser doesn't report on methods.

0 commit comments

Comments
 (0)