|
| 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 | + |
| 17 | +package notimestamp |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "go/ast" |
| 22 | + "go/token" |
| 23 | + "regexp" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "golang.org/x/tools/go/analysis" |
| 27 | + kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors" |
| 28 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" |
| 29 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector" |
| 30 | + markershelper "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" |
| 31 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/utils" |
| 32 | +) |
| 33 | + |
| 34 | +const name = "notimestamp" |
| 35 | + |
| 36 | +// Analyzer is the analyzer for the notimestamp package. |
| 37 | +// It checks that no struct fields named 'timestamp', or that contain timestamp as a |
| 38 | +// substring are present. |
| 39 | +var Analyzer = &analysis.Analyzer{ |
| 40 | + Name: name, |
| 41 | + Doc: "Suggest the usage of the term 'time' over 'timestamp'", |
| 42 | + Run: run, |
| 43 | + Requires: []*analysis.Analyzer{inspector.Analyzer}, |
| 44 | +} |
| 45 | + |
| 46 | +// case-insensitive regular expression to match 'timestamp' string in field or json tag. |
| 47 | +var timeStampRegEx = regexp.MustCompile("(?i)timestamp") |
| 48 | + |
| 49 | +func run(pass *analysis.Pass) (any, error) { |
| 50 | + inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) |
| 51 | + if !ok { |
| 52 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 53 | + } |
| 54 | + |
| 55 | + inspect.InspectFields(func(field *ast.Field, stack []ast.Node, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markershelper.Markers) { |
| 56 | + checkFieldsAndTags(pass, field, jsonTagInfo) |
| 57 | + }) |
| 58 | + |
| 59 | + return nil, nil //nolint:nilnil |
| 60 | +} |
| 61 | + |
| 62 | +func checkFieldsAndTags(pass *analysis.Pass, field *ast.Field, tagInfo extractjsontags.FieldTagInfo) { |
| 63 | + fieldName := utils.FieldName(field) |
| 64 | + if fieldName == "" { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + var suggestedFixes []analysis.SuggestedFix |
| 69 | + |
| 70 | + // check if filed name contains timestamp in it. |
| 71 | + fieldReplacementName := timeStampRegEx.ReplaceAllString(fieldName, "Time") |
| 72 | + if fieldReplacementName != fieldName { |
| 73 | + suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{ |
| 74 | + Message: fmt.Sprintf("replace %s with %s", fieldName, fieldReplacementName), |
| 75 | + TextEdits: []analysis.TextEdit{ |
| 76 | + { |
| 77 | + Pos: field.Pos(), |
| 78 | + NewText: []byte(fieldReplacementName), |
| 79 | + End: field.Pos() + token.Pos(len(fieldName)), |
| 80 | + }, |
| 81 | + }, |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + // check if the tag contains timestamp in it. |
| 86 | + tagReplacementName := timeStampRegEx.ReplaceAllString(tagInfo.Name, "Time") |
| 87 | + if strings.HasPrefix(strings.ToLower(tagInfo.Name), "time") { |
| 88 | + // If the tag starts with 'timeStamp', the replacement should be 'time' not 'Time'. |
| 89 | + tagReplacementName = timeStampRegEx.ReplaceAllString(tagInfo.Name, "time") |
| 90 | + } |
| 91 | + |
| 92 | + if tagReplacementName != tagInfo.Name { |
| 93 | + suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{ |
| 94 | + Message: fmt.Sprintf("replace %s json tag with %s", tagInfo.Name, tagReplacementName), |
| 95 | + TextEdits: []analysis.TextEdit{ |
| 96 | + { |
| 97 | + Pos: tagInfo.Pos, |
| 98 | + NewText: []byte(tagReplacementName), |
| 99 | + End: tagInfo.Pos + token.Pos(len(tagInfo.Name)), |
| 100 | + }, |
| 101 | + }, |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + if len(suggestedFixes) > 0 { |
| 106 | + pass.Report(analysis.Diagnostic{ |
| 107 | + Pos: field.Pos(), |
| 108 | + Message: fmt.Sprintf("field %s: prefer use of the term time over timestamp", fieldName), |
| 109 | + SuggestedFixes: suggestedFixes, |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments