Skip to content

Commit aa2432c

Browse files
committed
Implement the error message for other analysers
1 parent 5b2b076 commit aa2432c

File tree

49 files changed

+600
-630
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+600
-630
lines changed

pkg/analysis/commentstart/analyzer.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ import (
1919
"fmt"
2020
"go/ast"
2121
"go/token"
22-
"go/types"
2322
"strings"
2423

2524
"golang.org/x/tools/go/analysis"
2625
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2726
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2827
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
2928
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
30-
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
3129
)
3230

3331
const name = "commentstart"
@@ -47,25 +45,20 @@ func run(pass *analysis.Pass) (any, error) {
4745
return nil, kalerrors.ErrCouldNotGetInspector
4846
}
4947

50-
inspect.InspectFields(func(field *ast.Field, jsonTagInfo extractjsontags.FieldTagInfo, _ markers.Markers, _ string) {
51-
checkField(pass, field, jsonTagInfo)
48+
inspect.InspectFields(func(field *ast.Field, jsonTagInfo extractjsontags.FieldTagInfo, _ markers.Markers, qualifiedFieldName string) {
49+
checkField(pass, field, jsonTagInfo, qualifiedFieldName)
5250
})
5351

5452
return nil, nil //nolint:nilnil
5553
}
5654

57-
func checkField(pass *analysis.Pass, field *ast.Field, tagInfo extractjsontags.FieldTagInfo) {
55+
func checkField(pass *analysis.Pass, field *ast.Field, tagInfo extractjsontags.FieldTagInfo, qualifiedFieldName string) {
5856
if tagInfo.Name == "" {
5957
return
6058
}
6159

62-
fieldName := utils.FieldName(field)
63-
if fieldName == "" {
64-
fieldName = types.ExprString(field.Type)
65-
}
66-
6760
if field.Doc == nil {
68-
pass.Reportf(field.Pos(), "field %s is missing godoc comment", fieldName)
61+
pass.Reportf(field.Pos(), "field %s is missing godoc comment", qualifiedFieldName)
6962
return
7063
}
7164

@@ -75,7 +68,7 @@ func checkField(pass *analysis.Pass, field *ast.Field, tagInfo extractjsontags.F
7568
// The comment start is correct, apart from the casing, we can fix that.
7669
pass.Report(analysis.Diagnostic{
7770
Pos: firstLine.Pos(),
78-
Message: fmt.Sprintf("godoc for field %s should start with '%s ...'", fieldName, tagInfo.Name),
71+
Message: fmt.Sprintf("godoc for field %s should start with '%s ...'", qualifiedFieldName, tagInfo.Name),
7972
SuggestedFixes: []analysis.SuggestedFix{
8073
{
8174
Message: fmt.Sprintf("should replace first word with `%s`", tagInfo.Name),
@@ -90,7 +83,7 @@ func checkField(pass *analysis.Pass, field *ast.Field, tagInfo extractjsontags.F
9083
},
9184
})
9285
} else {
93-
pass.Reportf(field.Doc.List[0].Pos(), "godoc for field %s should start with '%s ...'", fieldName, tagInfo.Name)
86+
pass.Reportf(field.Doc.List[0].Pos(), "godoc for field %s should start with '%s ...'", qualifiedFieldName, tagInfo.Name)
9487
}
9588
}
9689
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ type CommentStartTestStruct struct {
66
NoJSONTag string
77
EmptyJSONTag string `json:""`
88
InlineJSONTag string `json:",inline"`
9-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
9+
NoComment string `json:"noComment"` // want "field CommentStartTestStruct.NoComment is missing godoc comment"
1010
Ignored string `json:"-"`
11-
Hyphen string `json:"-,"` // want "field Hyphen is missing godoc comment"
11+
Hyphen string `json:"-,"` // want "field CommentStartTestStruct.Hyphen is missing godoc comment"
1212

13-
AnonymousStruct struct { // want "field AnonymousStruct is missing godoc comment"
13+
AnonymousStruct struct { // want "field CommentStartTestStruct.AnonymousStruct is missing godoc comment"
1414
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
1515
} `json:"anonymousStruct"`
1616

@@ -24,18 +24,18 @@ type CommentStartTestStruct struct {
2424

2525
StructForInlineField `json:",inline"`
2626

27-
A `json:"a"` // want "field A is missing godoc comment"
27+
A `json:"a"` // want "field CommentStartTestStruct.A is missing godoc comment"
2828

29-
PkgA pkg.A `json:"pkgA"` // want "field PkgA is missing godoc comment"
29+
PkgA pkg.A `json:"pkgA"` // want "field CommentStartTestStruct.PkgA is missing godoc comment"
3030

31-
pkg.Embedded `json:"embedded"` // want "field pkg.Embedded is missing godoc comment"
31+
pkg.Embedded `json:"embedded"` // want "field CommentStartTestStruct.pkg.Embedded is missing godoc comment"
3232

33-
*pkg.EmbeddedPointer `json:"embeddedPointer"` // want "field \\*pkg.EmbeddedPointer is missing godoc comment"
33+
*pkg.EmbeddedPointer `json:"embeddedPointer"` // want "field CommentStartTestStruct.\\*pkg.EmbeddedPointer is missing godoc comment"
3434

35-
// IncorrectStartComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectStartComment should start with 'incorrectStartComment ...'"
35+
// IncorrectStartComment is a field with an incorrect start to the comment. // want "godoc for field CommentStartTestStruct.IncorrectStartComment should start with 'incorrectStartComment ...'"
3636
IncorrectStartComment string `json:"incorrectStartComment"`
3737

38-
// IncorrectStartOptionalComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectStartOptionalComment should start with 'incorrectStartOptionalComment ...'"
38+
// IncorrectStartOptionalComment is a field with an incorrect start to the comment. // want "godoc for field CommentStartTestStruct.IncorrectStartOptionalComment should start with 'incorrectStartOptionalComment ...'"
3939
IncorrectStartOptionalComment string `json:"incorrectStartOptionalComment"`
4040

4141
// correctStartComment is a field with a correct start to the comment.
@@ -44,39 +44,39 @@ type CommentStartTestStruct struct {
4444
// correctStartOptionalComment is a field with a correct start to the comment.
4545
CorrectStartOptionalComment string `json:"correctStartOptionalComment,omitempty"`
4646

47-
// IncorrectMultiLineComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectMultiLineComment should start with 'incorrectMultiLineComment ...'"
47+
// IncorrectMultiLineComment is a field with an incorrect start to the comment. // want "godoc for field CommentStartTestStruct.IncorrectMultiLineComment should start with 'incorrectMultiLineComment ...'"
4848
// Except this time there are multiple lines to the comment.
4949
IncorrectMultiLineComment string `json:"incorrectMultiLineComment"`
5050

5151
// correctMultiLineComment is a field with a correct start to the comment.
5252
// Except this time there are multiple lines to the comment.
5353
CorrectMultiLineComment string `json:"correctMultiLineComment"`
5454

55-
// This comment just isn't correct at all, doesn't even start with anything resembling the field names. // want "godoc for field IncorrectComment should start with 'incorrectComment ...'"
55+
// This comment just isn't correct at all, doesn't even start with anything resembling the field names. // want "godoc for field CommentStartTestStruct.IncorrectComment should start with 'incorrectComment ...'"
5656
IncorrectComment string `json:"incorrectComment"`
5757
}
5858

5959
// DoNothing is used to check that the analyser doesn't report on methods.
6060
func (CommentStartTestStruct) DoNothing() {}
6161

6262
type StructForInlineField struct {
63-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
63+
NoComment string `json:"noComment"` // want "field StructForInlineField.NoComment is missing godoc comment"
6464
}
6565

6666
type A struct {
67-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
67+
NoComment string `json:"noComment"` // want "field A.NoComment is missing godoc comment"
6868
}
6969

7070
type unexportedStruct struct {
71-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
71+
NoComment string `json:"noComment"` // want "field unexportedStruct.NoComment is missing godoc comment"
7272
}
7373

7474
type (
7575
MultipleTypeDeclaration1 struct {
76-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
76+
NoComment string `json:"noComment"` // want "field MultipleTypeDeclaration1.NoComment is missing godoc comment"
7777
}
7878
MultipleTypeDeclaration2 struct {
79-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
79+
NoComment string `json:"noComment"` // want "field MultipleTypeDeclaration2.NoComment is missing godoc comment"
8080
}
8181
)
8282

pkg/analysis/commentstart/testdata/src/a/a.go.golden

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ type CommentStartTestStruct struct {
66
NoJSONTag string
77
EmptyJSONTag string `json:""`
88
InlineJSONTag string `json:",inline"`
9-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
9+
NoComment string `json:"noComment"` // want "field CommentStartTestStruct.NoComment is missing godoc comment"
1010
Ignored string `json:"-"`
11-
Hyphen string `json:"-,"` // want "field Hyphen is missing godoc comment"
11+
Hyphen string `json:"-,"` // want "field CommentStartTestStruct.Hyphen is missing godoc comment"
1212

13-
AnonymousStruct struct { // want "field AnonymousStruct is missing godoc comment"
13+
AnonymousStruct struct { // want "field CommentStartTestStruct.AnonymousStruct is missing godoc comment"
1414
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
1515
} `json:"anonymousStruct"`
1616

@@ -24,18 +24,18 @@ type CommentStartTestStruct struct {
2424

2525
StructForInlineField `json:",inline"`
2626

27-
A `json:"a"` // want "field A is missing godoc comment"
27+
A `json:"a"` // want "field CommentStartTestStruct.A is missing godoc comment"
2828

29-
PkgA pkg.A `json:"pkgA"` // want "field PkgA is missing godoc comment"
29+
PkgA pkg.A `json:"pkgA"` // want "field CommentStartTestStruct.PkgA is missing godoc comment"
3030

31-
pkg.Embedded `json:"embedded"` // want "field pkg.Embedded is missing godoc comment"
31+
pkg.Embedded `json:"embedded"` // want "field CommentStartTestStruct.pkg.Embedded is missing godoc comment"
3232

33-
*pkg.EmbeddedPointer `json:"embeddedPointer"` // want "field \\*pkg.EmbeddedPointer is missing godoc comment"
33+
*pkg.EmbeddedPointer `json:"embeddedPointer"` // want "field CommentStartTestStruct.\\*pkg.EmbeddedPointer is missing godoc comment"
3434

35-
// incorrectStartComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectStartComment should start with 'incorrectStartComment ...'"
35+
// incorrectStartComment is a field with an incorrect start to the comment. // want "godoc for field CommentStartTestStruct.IncorrectStartComment should start with 'incorrectStartComment ...'"
3636
IncorrectStartComment string `json:"incorrectStartComment"`
3737

38-
// incorrectStartOptionalComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectStartOptionalComment should start with 'incorrectStartOptionalComment ...'"
38+
// incorrectStartOptionalComment is a field with an incorrect start to the comment. // want "godoc for field CommentStartTestStruct.IncorrectStartOptionalComment should start with 'incorrectStartOptionalComment ...'"
3939
IncorrectStartOptionalComment string `json:"incorrectStartOptionalComment"`
4040

4141
// correctStartComment is a field with a correct start to the comment.
@@ -44,39 +44,39 @@ type CommentStartTestStruct struct {
4444
// correctStartOptionalComment is a field with a correct start to the comment.
4545
CorrectStartOptionalComment string `json:"correctStartOptionalComment,omitempty"`
4646

47-
// incorrectMultiLineComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectMultiLineComment should start with 'incorrectMultiLineComment ...'"
47+
// incorrectMultiLineComment is a field with an incorrect start to the comment. // want "godoc for field CommentStartTestStruct.IncorrectMultiLineComment should start with 'incorrectMultiLineComment ...'"
4848
// Except this time there are multiple lines to the comment.
4949
IncorrectMultiLineComment string `json:"incorrectMultiLineComment"`
5050

5151
// correctMultiLineComment is a field with a correct start to the comment.
5252
// Except this time there are multiple lines to the comment.
5353
CorrectMultiLineComment string `json:"correctMultiLineComment"`
5454

55-
// This comment just isn't correct at all, doesn't even start with anything resembling the field names. // want "godoc for field IncorrectComment should start with 'incorrectComment ...'"
55+
// This comment just isn't correct at all, doesn't even start with anything resembling the field names. // want "godoc for field CommentStartTestStruct.IncorrectComment should start with 'incorrectComment ...'"
5656
IncorrectComment string `json:"incorrectComment"`
5757
}
5858

5959
// DoNothing is used to check that the analyser doesn't report on methods.
6060
func (CommentStartTestStruct) DoNothing() {}
6161

6262
type StructForInlineField struct {
63-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
63+
NoComment string `json:"noComment"` // want "field StructForInlineField.NoComment is missing godoc comment"
6464
}
6565

6666
type A struct {
67-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
67+
NoComment string `json:"noComment"` // want "field A.NoComment is missing godoc comment"
6868
}
6969

7070
type unexportedStruct struct {
71-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
71+
NoComment string `json:"noComment"` // want "field unexportedStruct.NoComment is missing godoc comment"
7272
}
7373

7474
type (
7575
MultipleTypeDeclaration1 struct {
76-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
76+
NoComment string `json:"noComment"` // want "field MultipleTypeDeclaration1.NoComment is missing godoc comment"
7777
}
7878
MultipleTypeDeclaration2 struct {
79-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
79+
NoComment string `json:"noComment"` // want "field MultipleTypeDeclaration2.NoComment is missing godoc comment"
8080
}
8181
)
8282

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package pkg
22

33
type A struct {
4-
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
4+
NoComment string `json:"noComment"` // want "field A.NoComment is missing godoc comment"
55
}
66

77
// To embed the same struct multiple times, we need to rename the type.

pkg/analysis/duplicatemarkers/analyzer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func run(pass *analysis.Pass) (any, error) {
4848
return nil, kalerrors.ErrCouldNotGetInspector
4949
}
5050

51-
inspect.InspectFields(func(field *ast.Field, _ extractjsontags.FieldTagInfo, markersAccess markers.Markers, _ string) {
52-
checkField(pass, field, markersAccess)
51+
inspect.InspectFields(func(field *ast.Field, _ extractjsontags.FieldTagInfo, markersAccess markers.Markers, qualifiedFieldName string) {
52+
checkField(pass, field, markersAccess, qualifiedFieldName)
5353
})
5454

5555
inspect.InspectTypeSpec(func(typeSpec *ast.TypeSpec, markersAccess markers.Markers) {
@@ -59,7 +59,7 @@ func run(pass *analysis.Pass) (any, error) {
5959
return nil, nil //nolint:nilnil
6060
}
6161

62-
func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers) {
62+
func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers, qualifiedFieldName string) {
6363
if field == nil || len(field.Names) == 0 {
6464
return
6565
}
@@ -74,7 +74,7 @@ func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Mar
7474
continue
7575
}
7676

77-
report(pass, field.Pos(), field.Names[0].Name, marker)
77+
report(pass, field.Pos(), qualifiedFieldName, marker)
7878
}
7979
}
8080

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,42 @@ type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated ma
4545

4646
// +kubebuilder:validation:Required
4747
// +kubebuilder:validation:Required
48-
DuplicatedRequired string `json:"duplicatedRequired"` // want "DuplicatedRequired has duplicated markers kubebuilder:validation:Required"
48+
DuplicatedRequired string `json:"duplicatedRequired"` // want "DuplicateMarkerSpec.DuplicatedRequired has duplicated markers kubebuilder:validation:Required"
4949

5050
// +kubebuilder:validation:Enum=foo;bar;baz
5151
// +kubebuilder:validation:Enum=foo;bar;baz
52-
DuplicatedEnum string `json:"duplicatedEnum"` // want "DuplicatedEnum has duplicated markers kubebuilder:validation:Enum"
52+
DuplicatedEnum string `json:"duplicatedEnum"` // want "DuplicateMarkerSpec.DuplicatedEnum has duplicated markers kubebuilder:validation:Enum"
5353

5454
// +kubebuilder:validation:MaxLength=10
5555
// +kubebuilder:validation:MaxLength=10
56-
DuplicatedMaxLength int `json:"duplicatedMaxLength"` // want "DuplicatedMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
56+
DuplicatedMaxLength int `json:"duplicatedMaxLength"` // want "DuplicateMarkerSpec.DuplicatedMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
5757

5858
// +kubebuilder:validation:MaxLength=10
59-
DuplicatedMaxLengthIncludingTypeMarker MaxLength `json:"duplicatedMaxLengthIncludingTypeMarker"` // want "DuplicatedMaxLengthIncludingTypeMarker has duplicated markers kubebuilder:validation:MaxLength=10"
59+
DuplicatedMaxLengthIncludingTypeMarker MaxLength `json:"duplicatedMaxLengthIncludingTypeMarker"` // want "DuplicateMarkerSpec.DuplicatedMaxLengthIncludingTypeMarker has duplicated markers kubebuilder:validation:MaxLength=10"
6060

6161
// +listType=map
6262
// +listMapKey=primaryKey
6363
// +listMapKey=secondaryKey
6464
// +listType=map
6565
// +required
66-
DuplicatedListTypeMap Map `json:"duplicatedListTypeMap"` // want "DuplicatedListTypeMap has duplicated markers listType=map"
66+
DuplicatedListTypeMap Map `json:"duplicatedListTypeMap"` // want "DuplicateMarkerSpec.DuplicatedListTypeMap has duplicated markers listType=map"
6767

6868
// +optional
6969
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"
7070
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="replicas must be immutable"
7171
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"
72-
DuplicatedReplicas *int `json:"duplicatedReplicas"` // want "DuplicatedReplicas has duplicated markers kubebuilder:validation:XValidation:rule=\"self >= 1 && self <= 3\",message=\"must be 1 to 5\""
72+
DuplicatedReplicas *int `json:"duplicatedReplicas"` // want "DuplicateMarkerSpec.DuplicatedReplicas has duplicated markers kubebuilder:validation:XValidation:rule=\"self >= 1 && self <= 3\",message=\"must be 1 to 5\""
7373

7474
// +optional
7575
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"
7676
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="replicas must be immutable"
7777
// +kubebuilder:validation:XValidation:message="must be 1 to 5",rule="self >= 1 && self <= 3"
78-
DuplicatedUnorderedValidationReplicas *int `json:"duplicatedUnorderedValidationReplicas"` // want "DuplicatedUnorderedValidationReplicas has duplicated markers kubebuilder:validation:XValidation:message=\"must be 1 to 5\",rule=\"self >= 1 && self <= 3\""
78+
DuplicatedUnorderedValidationReplicas *int `json:"duplicatedUnorderedValidationReplicas"` // want "DuplicateMarkerSpec.DuplicatedUnorderedValidationReplicas has duplicated markers kubebuilder:validation:XValidation:message=\"must be 1 to 5\",rule=\"self >= 1 && self <= 3\""
7979

8080
StringFromAnotherFile StringFromAnotherFile `json:"stringFromAnotherFile"`
8181

8282
// +kubebuilder:validation:MaxLength=10
83-
StringFromAnotherFileWithMaxLength StringFromAnotherFile `json:"stringFromAnotherFileWithMaxLength"` // want "StringFromAnotherFileWithMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
83+
StringFromAnotherFileWithMaxLength StringFromAnotherFile `json:"stringFromAnotherFileWithMaxLength"` // want "DuplicateMarkerSpec.StringFromAnotherFileWithMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
8484
}
8585

8686
type Map struct {

0 commit comments

Comments
 (0)