|
| 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 noreferences |
| 17 | + |
| 18 | +import ( |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "golang.org/x/tools/go/analysis" |
| 23 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 24 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/initializer" |
| 25 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/namingconventions" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + name = "noreferences" |
| 30 | + doc = "Enforces that fields use Ref/Refs and not Reference/References" |
| 31 | +) |
| 32 | + |
| 33 | +var ( |
| 34 | + errUnexpectedInitializerType = errors.New("expected namingconventions.Initializer() to be of type initializer.ConfigurableAnalyzerInitializer, but was not") |
| 35 | + errInvalidPolicy = errors.New("invalid policy") |
| 36 | +) |
| 37 | + |
| 38 | +// newAnalyzer creates a new analyzer for the noreferences linter that is a wrapper around the namingconventions linter. |
| 39 | +func newAnalyzer(cfg *Config) *analysis.Analyzer { |
| 40 | + if cfg == nil { |
| 41 | + cfg = &Config{} |
| 42 | + } |
| 43 | + |
| 44 | + // Default to PreferAbbreviatedReference if no policy is specified |
| 45 | + policy := cfg.Policy |
| 46 | + if policy == "" { |
| 47 | + policy = PolicyPreferAbbreviatedReference |
| 48 | + } |
| 49 | + |
| 50 | + // Build the namingconventions config based on the policy |
| 51 | + ncConfig := &namingconventions.Config{ |
| 52 | + Conventions: buildConventions(policy), |
| 53 | + } |
| 54 | + |
| 55 | + // Get the configurable initializer for namingconventions |
| 56 | + configInit, ok := namingconventions.Initializer().(initializer.ConfigurableAnalyzerInitializer) |
| 57 | + if !ok { |
| 58 | + panic(fmt.Errorf("getting initializer: %w", errUnexpectedInitializerType)) |
| 59 | + } |
| 60 | + |
| 61 | + // Validate generated namingconventions configuration |
| 62 | + errs := configInit.ValidateConfig(ncConfig, field.NewPath("noreferences")) |
| 63 | + if err := errs.ToAggregate(); err != nil { |
| 64 | + panic(fmt.Errorf("noreferences linter has an invalid namingconventions configuration: %w", err)) |
| 65 | + } |
| 66 | + |
| 67 | + // Initialize the wrapped analyzer |
| 68 | + analyzer, err := configInit.Init(ncConfig) |
| 69 | + if err != nil { |
| 70 | + panic(fmt.Errorf("noreferences linter encountered an error initializing wrapped namingconventions analyzer: %w", err)) |
| 71 | + } |
| 72 | + |
| 73 | + analyzer.Name = name |
| 74 | + analyzer.Doc = doc |
| 75 | + |
| 76 | + return analyzer |
| 77 | +} |
| 78 | + |
| 79 | +// buildConventions creates the naming conventions based on the policy. |
| 80 | +func buildConventions(policy Policy) []namingconventions.Convention { |
| 81 | + switch policy { |
| 82 | + case PolicyPreferAbbreviatedReference: |
| 83 | + // Replace "Reference" or "References" with "Ref" or "Refs" anywhere in field name |
| 84 | + return []namingconventions.Convention{ |
| 85 | + { |
| 86 | + Name: "reference-to-ref", |
| 87 | + ViolationMatcher: "^[Rr]eference|Reference(s?)$", |
| 88 | + Operation: namingconventions.OperationReplacement, |
| 89 | + Replacement: "Ref$1", |
| 90 | + Message: "field names should use 'Ref' instead of 'Reference'", |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + case PolicyNoReferences: |
| 95 | + // Warn about reference words anywhere in field name without providing fixes |
| 96 | + return []namingconventions.Convention{ |
| 97 | + { |
| 98 | + Name: "no-references", |
| 99 | + ViolationMatcher: "^[Rr]ef(erence)?(s?)([A-Z])|Ref(erence)?(s?)$", |
| 100 | + Operation: namingconventions.OperationInform, |
| 101 | + Message: "field names should not contain reference-related words", |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + default: |
| 106 | + // Should not happen due to validation |
| 107 | + panic(fmt.Errorf("%w: %s", errInvalidPolicy, policy)) |
| 108 | + } |
| 109 | +} |
0 commit comments