|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package references |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "go/ast" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "golang.org/x/tools/go/analysis" |
| 24 | + kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors" |
| 25 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" |
| 26 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector" |
| 27 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" |
| 28 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/utils" |
| 29 | +) |
| 30 | + |
| 31 | +const name = "references" |
| 32 | + |
| 33 | +type analyzer struct { |
| 34 | + allowRefAndRefs bool |
| 35 | +} |
| 36 | + |
| 37 | +// newAnalyzer creates a new analysis.Analyzer for the references linter. |
| 38 | +func newAnalyzer(cfg *Config) *analysis.Analyzer { |
| 39 | + if cfg == nil { |
| 40 | + cfg = &Config{} |
| 41 | + } |
| 42 | + |
| 43 | + a := &analyzer{ |
| 44 | + allowRefAndRefs: cfg.AllowRefAndRefs, |
| 45 | + } |
| 46 | + |
| 47 | + analyzer := &analysis.Analyzer{ |
| 48 | + Name: name, |
| 49 | + Doc: "Enforces that fields use Ref/Refs and not Reference/References", |
| 50 | + Run: a.run, |
| 51 | + Requires: []*analysis.Analyzer{inspector.Analyzer, extractjsontags.Analyzer}, |
| 52 | + } |
| 53 | + |
| 54 | + return analyzer |
| 55 | +} |
| 56 | + |
| 57 | +func (a *analyzer) run(pass *analysis.Pass) (any, error) { |
| 58 | + inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) |
| 59 | + if !ok { |
| 60 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 61 | + } |
| 62 | + |
| 63 | + inspect.InspectFields(func(field *ast.Field, stack []ast.Node, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markers.Markers) { |
| 64 | + a.checkField(pass, field, jsonTagInfo) |
| 65 | + }) |
| 66 | + |
| 67 | + return nil, nil //nolint:nilnil |
| 68 | +} |
| 69 | + |
| 70 | +func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field, jsonTagInfo extractjsontags.FieldTagInfo) { |
| 71 | + if field == nil || len(field.Names) == 0 { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + fieldName := utils.FieldName(field) |
| 76 | + |
| 77 | + if strings.HasSuffix(fieldName, "Reference") { |
| 78 | + suggestedName := strings.TrimSuffix(fieldName, "Reference") + "Ref" |
| 79 | + pass.Report(analysis.Diagnostic{ |
| 80 | + Pos: field.Pos(), |
| 81 | + Message: fmt.Sprintf("field %s should use 'Ref' instead of 'Reference'", fieldName), |
| 82 | + SuggestedFixes: []analysis.SuggestedFix{ |
| 83 | + { |
| 84 | + Message: "replace 'Reference' with 'Ref'", |
| 85 | + TextEdits: []analysis.TextEdit{ |
| 86 | + { |
| 87 | + Pos: field.Names[0].Pos(), |
| 88 | + NewText: []byte(suggestedName), |
| 89 | + End: field.Names[0].End(), |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if strings.HasSuffix(fieldName, "References") { |
| 99 | + suggestedName := strings.TrimSuffix(fieldName, "References") + "Refs" |
| 100 | + pass.Report(analysis.Diagnostic{ |
| 101 | + Pos: field.Pos(), |
| 102 | + Message: fmt.Sprintf("field %s should use 'Refs' instead of 'References'", fieldName), |
| 103 | + SuggestedFixes: []analysis.SuggestedFix{ |
| 104 | + { |
| 105 | + Message: "replace 'References' with 'Refs'", |
| 106 | + TextEdits: []analysis.TextEdit{ |
| 107 | + { |
| 108 | + Pos: field.Names[0].Pos(), |
| 109 | + NewText: []byte(suggestedName), |
| 110 | + End: field.Names[0].End(), |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + // If allowRefAndRefs is false, report errors for standalone Ref/Refs fields |
| 120 | + // If allowRefAndRefs is true (OpenShift), don't report errors for Ref/Refs fields |
| 121 | + if !a.allowRefAndRefs { |
| 122 | + // Check for fields ending with Ref or Refs (excluding those already handled above) |
| 123 | + if fieldName != "Ref" && strings.HasSuffix(fieldName, "Ref") && !strings.HasSuffix(fieldName, "eRef") { |
| 124 | + pass.Reportf(field.Pos(), "field %s should not use 'Ref' suffix", fieldName) |
| 125 | + } |
| 126 | + |
| 127 | + if fieldName != "Refs" && strings.HasSuffix(fieldName, "Refs") && !strings.HasSuffix(fieldName, "eRefs") { |
| 128 | + pass.Reportf(field.Pos(), "field %s should not use 'Refs' suffix", fieldName) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments