Skip to content

Commit 68435a3

Browse files
committed
Enable SuggestedFixes
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent 0f22ba9 commit 68435a3

File tree

4 files changed

+112
-20
lines changed

4 files changed

+112
-20
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,16 @@ When the `json` tag is present, and matches the first word of the field comment
165165

166166
## DuplicateMarkers
167167

168-
The `duplicatemarkers` linter checks for duplicate markers related with specific type or field.
169-
This linter diagnose _only_ if the marker and value together are completely unique. e.g. the `duplicatemarkers` will not diagnose the field has kubebuilder:validation:MaxLength=10 and kubebuilder:validation:MaxLength=11
168+
The duplicatemarkers linter checks for exact duplicates of markers for types and fields.
169+
This means that something like:
170170

171-
About the duplicated markers which has different value, it requires specific rule for each marker, these are processed by its corresponding linter.
171+
// +kubebuilder:validation:MaxLength=10
172+
// +kubebuilder:validation:MaxLength=10
173+
Will be flagged by this linter, while something like:
174+
175+
// +kubebuilder:validation:MaxLength=10
176+
// +kubebuilder:validation:MaxLength=11
177+
will not.
172178

173179
## Integers
174180

pkg/analysis/duplicatemarkers/analyzer.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package duplicatemarkers
1818
import (
1919
"fmt"
2020
"go/ast"
21+
"go/token"
2122

2223
"golang.org/x/tools/go/analysis"
2324

@@ -72,10 +73,7 @@ func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Mar
7273
continue
7374
}
7475

75-
pass.Report(analysis.Diagnostic{
76-
Pos: field.Pos(),
77-
Message: fmt.Sprintf("%s has duplicated markers %s", field.Names[0].Name, marker.String()),
78-
})
76+
report(pass, field.Pos(), field.Names[0].Name, marker)
7977
}
8078
}
8179

@@ -94,9 +92,25 @@ func checkTypeSpec(pass *analysis.Pass, typeSpec *ast.TypeSpec, markersAccess ma
9492
continue
9593
}
9694

97-
pass.Report(analysis.Diagnostic{
98-
Pos: typeSpec.Pos(),
99-
Message: fmt.Sprintf("%s has duplicated markers %s", typeSpec.Name.Name, marker.String()),
100-
})
95+
report(pass, typeSpec.Pos(), typeSpec.Name.Name, marker)
10196
}
10297
}
98+
99+
func report(pass *analysis.Pass, pos token.Pos, fieldName string, marker markers.Marker) {
100+
pass.Report(analysis.Diagnostic{
101+
Pos: pos,
102+
Message: fmt.Sprintf("%s has duplicated markers %s", fieldName, marker),
103+
SuggestedFixes: []analysis.SuggestedFix{
104+
{
105+
Message: fmt.Sprintf("Remove duplicated marker %s", marker),
106+
TextEdits: []analysis.TextEdit{
107+
{
108+
Pos: marker.Pos,
109+
// To remove the duplicated marker, we need to remove the whole line.
110+
End: marker.End + 1,
111+
},
112+
},
113+
},
114+
},
115+
})
116+
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package a
22

33
// It must be ignored since it is not a type
44
// +kubebuilder:validation:Enum=foo;bar;baz
5+
// +kubebuilder:validation:Enum=foo;bar;baz
56
var Variable string
67

78
// +kubebuilder:validation:Enum=foo;bar;baz
@@ -12,6 +13,10 @@ type Enum string // want "Enum has duplicated markers kubebuilder:validation:Enu
1213
// +kubebuilder:validation:MaxLength=11
1314
type MaxLength int
1415

16+
// +kubebuilder:validation:MaxLength=10
17+
// +kubebuilder:validation:MaxLength=10
18+
type DuplicatedMaxLength int // want "DuplicatedMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
19+
1520
// +kubebuilder:object:root=true
1621
// +kubebuilder:subresource:status
1722
// +kubebuilder:object:root=true
@@ -33,6 +38,11 @@ type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated ma
3338
// should be ignored since XValidation is allowed to have different values
3439
Replicas *int `json:"replicas"`
3540

41+
// +kubebuilder:validation:MaxLength=11
42+
// +kubebuilder:validation:MaxLength=10
43+
// should be ignored since MaxLength is allowed to have different values
44+
Maxlength int `json:"maxlength"`
45+
3646
// +kubebuilder:validation:Required
3747
// +kubebuilder:validation:Required
3848
DuplicatedRequired string `json:"duplicatedRequired"` // want "DuplicatedRequired has duplicated markers kubebuilder:validation:Required"
@@ -41,9 +51,9 @@ type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated ma
4151
// +kubebuilder:validation:Enum=foo;bar;baz
4252
DuplicatedEnum string `json:"duplicatedEnum"` // want "DuplicatedEnum has duplicated markers kubebuilder:validation:Enum"
4353

44-
// +kubebuilder:validation:MaxLength=11
4554
// +kubebuilder:validation:MaxLength=10
46-
DuplicatedMaxlength int `json:"maxlength"`
55+
// +kubebuilder:validation:MaxLength=10
56+
DuplicatedMaxLength int `json:"duplicatedMaxLength"` // want "DuplicatedMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
4757

4858
// +listType=map
4959
// +listMapKey=primaryKey
@@ -57,6 +67,12 @@ type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated ma
5767
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="replicas must be immutable"
5868
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"
5969
DuplicatedReplicas *int `json:"duplicatedReplicas"` // want "DuplicatedReplicas has duplicated markers kubebuilder:validation:XValidation:rule=\"self >= 1 && self <= 3\",message=\"must be 1 to 5\""
70+
71+
// +optional
72+
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"
73+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="replicas must be immutable"
74+
// +kubebuilder:validation:XValidation:message="must be 1 to 5",rule="self >= 1 && self <= 3"
75+
DuplicatedUnorderedValidationReplicas *int `json:"duplicatedUnorderedValidationReplicas"` // want "DuplicatedUnorderedValidationReplicas has duplicated markers kubebuilder:validation:XValidation:message=\"must be 1 to 5\",rule=\"self >= 1 && self <= 3\""
6076
}
6177

6278
type Map struct {
Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,76 @@
11
package a
22

3-
// +optional
3+
// It must be ignored since it is not a type
4+
// +kubebuilder:validation:Enum=foo;bar;baz
5+
// +kubebuilder:validation:Enum=foo;bar;baz
6+
var Variable string
7+
48
// +kubebuilder:validation:Enum=foo;bar;baz
59
type Enum string // want "Enum has duplicated markers kubebuilder:validation:Enum"
610

7-
// It must be ignored since it is not a type
8-
// +required
9-
var Variable string
11+
// +kubebuilder:validation:MaxLength=10
12+
// +kubebuilder:validation:MaxLength=11
13+
type MaxLength int
14+
15+
// +kubebuilder:validation:MaxLength=10
16+
type DuplicatedMaxLength int // want "DuplicatedMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
1017

11-
// +kubebuilder:subresource:status
1218
// +kubebuilder:object:root=true
19+
// +kubebuilder:subresource:status
1320
type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated markers kubebuilder:object:root"
1421
// +kubebuilder:validation:Required
15-
Foo string `json:"foo"`
22+
// shpuld be ignored since it only has single marker
23+
UniqueRequired string `json:"uniqueRequired"`
24+
25+
// +listType=map
26+
// +listMapKey=primaryKey
27+
// +listMapKey=secondaryKey
28+
// +required
29+
// should be ignored since listMapKey is allowed to have different values
30+
Map Map `json:"map"`
31+
32+
// +optional
33+
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"
34+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="replicas must be immutable"
35+
// should be ignored since XValidation is allowed to have different values
36+
Replicas *int `json:"replicas"`
37+
38+
// +kubebuilder:validation:MaxLength=11
39+
// +kubebuilder:validation:MaxLength=10
40+
// should be ignored since MaxLength is allowed to have different values
41+
Maxlength int `json:"maxlength"`
1642

1743
// +kubebuilder:validation:Required
44+
DuplicatedRequired string `json:"duplicatedRequired"` // want "DuplicatedRequired has duplicated markers kubebuilder:validation:Required"
45+
46+
// +kubebuilder:validation:Enum=foo;bar;baz
47+
DuplicatedEnum string `json:"duplicatedEnum"` // want "DuplicatedEnum has duplicated markers kubebuilder:validation:Enum"
48+
1849
// +kubebuilder:validation:MaxLength=10
19-
DuplicatedFoo string `json:"duplicatedFoo"` // want "DuplicatedFoo has duplicated markers kubebuilder:validation:Required"
50+
DuplicatedMaxLength int `json:"duplicatedMaxLength"` // want "DuplicatedMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
51+
52+
// +listType=map
53+
// +listMapKey=primaryKey
54+
// +listMapKey=secondaryKey
55+
// +required
56+
DuplicatedListTypeMap Map `json:"duplicatedListTypeMap"` // want "DuplicatedListTypeMap has duplicated markers listType=map"
57+
58+
// +optional
59+
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"
60+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="replicas must be immutable"
61+
DuplicatedReplicas *int `json:"duplicatedReplicas"` // want "DuplicatedReplicas has duplicated markers kubebuilder:validation:XValidation:rule=\"self >= 1 && self <= 3\",message=\"must be 1 to 5\""
62+
63+
// +optional
64+
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"
65+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="replicas must be immutable"
66+
DuplicatedUnorderedValidationReplicas *int `json:"duplicatedUnorderedValidationReplicas"` // want "DuplicatedUnorderedValidationReplicas has duplicated markers kubebuilder:validation:XValidation:message=\"must be 1 to 5\",rule=\"self >= 1 && self <= 3\""
67+
}
68+
69+
type Map struct {
70+
// +required
71+
PrimaryKey string `json:"primaryKey"`
72+
// +required
73+
SecondaryKey string `json:"secondaryKey"`
74+
// +required
75+
Value string `json:"value"`
2076
}

0 commit comments

Comments
 (0)