-
Notifications
You must be signed in to change notification settings - Fork 17
linter: add namingconventions
linter
#106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
linter: add namingconventions
linter
#106
Conversation
Just a PoC, not mergeable /hold |
pkg/analysis/fieldnaming/analyzer.go
Outdated
var defaults = []NamingConvention{ | ||
{ | ||
Matcher: *regexp.MustCompile("(?i)phase"), | ||
Operation: "Drop", | ||
Message: "phase fields are deprecated and discouraged. conditions should be used instead.", | ||
}, | ||
{ | ||
Matcher: *regexp.MustCompile("(?i)timestamp"), | ||
Operation: "Replace", | ||
Message: "prefer use of the term 'time' over 'timestamp'", | ||
// replacement of `Time` follows CamelCase principles for field names and JSON tags | ||
// TODO: Handle case where it is _not_ the second word in CamelCase for json tag | ||
Replacement: "Time", | ||
}, | ||
{ | ||
Matcher: *regexp.MustCompile("(?i)reference"), | ||
Operation: "Replace", | ||
Message: "prefer use of the term 'ref' over 'reference'", | ||
// replacement of `Ref` follows CamelCase principles for field names and JSON tags | ||
// TODO: Handle case where it is _not_ the second word in CamelCase for json tag | ||
Replacement: "Ref", | ||
}, | ||
} | ||
|
||
type NamingConvention struct { | ||
// Matcher is a regular expression | ||
// used to identify field names | ||
// where this convention applies | ||
Matcher regexp.Regexp | ||
|
||
// Replacement is an optional | ||
// string value used to replace the matched content | ||
// in a suggested fix. | ||
// Only used when Operation is Replace. | ||
Replacement string | ||
|
||
// Operation is the type of operation that should take place for this | ||
// naming convention. | ||
// One of Drop, Replace. | ||
Operation string | ||
|
||
// Message is the message that should be included in the | ||
// linter report when this naming convention is applied. | ||
Message string | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we were to continue with this approach, the thinking is that we would implement a configuration layer for this linter that exposes this NamingConvention
type to allow the development of custom naming conventions.
We could also add a configuration option to include/exclude the defaults we have put in place based on the Kubernetes API conventions doc.
/cc @JoelSpeed @sivchari - I'm curious about your thoughts here. @Karthik-K-N - just a heads up since you have #105 open and I took some liberties to include that convention in this example as well. I don't think this should block your PR from going in (or supersede it). Also happy to hear your thoughts if you have any here. |
Even I thought a bit about covering multiple linters under a cover, We have few linters doing similar things like nophase, notimestamp, noduration. Only thought came to my mind at that time was, what if user wants to relax one among many linterns. But with your approach of having a configuration file would be handy at that time. |
I'm a fan of the approach being taken for the NoNullable linter, where we still have a top level linter that contains the descriptions and reasoning of why we do/don't do certain things, but under the hood is just configuring another linter. Perhaps we can do similar here |
a05f7c4
to
67aa900
Compare
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: revert this
d386eb7
to
1ad36f3
Compare
that allows for defining custom namingconventions to be enforced on Go and serialized field names Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
1ad36f3
to
013a77c
Compare
namingconventions
linter
@JoelSpeed I've spent some time updating this and think this is ready for a review. |
/hold cancel |
docs/linters.md
Outdated
linterConfig: | ||
namingconventions: | ||
conventions: | ||
- name: englishcolour |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, probably BritishEnglishColour? Is casing meaningful here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can change it.
Casing will matter here - I'll update the documentation on the field and docs.
suggestedFixes := []analysis.SuggestedFix{} | ||
suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{ | ||
Message: "remove the field", | ||
TextEdits: []analysis.TextEdit{ | ||
{ | ||
Pos: field.Pos(), | ||
NewText: []byte(""), | ||
End: field.End(), | ||
}, | ||
}, | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if we need to declare an empty list first?
suggestedFixes := []analysis.SuggestedFix{} | |
suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{ | |
Message: "remove the field", | |
TextEdits: []analysis.TextEdit{ | |
{ | |
Pos: field.Pos(), | |
NewText: []byte(""), | |
End: field.End(), | |
}, | |
}, | |
}) | |
suggestedFixes := append([]analysis.SuggestedFix{}, analysis.SuggestedFix{ | |
Message: "remove the field", | |
TextEdits: []analysis.TextEdit{ | |
{ | |
Pos: field.Pos(), | |
NewText: []byte(""), | |
End: field.End(), | |
}, | |
}, | |
}) |
Or
suggestedFixes := []analysis.SuggestedFix{} | |
suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{ | |
Message: "remove the field", | |
TextEdits: []analysis.TextEdit{ | |
{ | |
Pos: field.Pos(), | |
NewText: []byte(""), | |
End: field.End(), | |
}, | |
}, | |
}) | |
suggestedFixes := []analysis.SuggestedFix{ | |
{ | |
Message: "remove the field", | |
TextEdits: []analysis.TextEdit{ | |
{ | |
Pos: field.Pos(), | |
NewText: []byte(""), | |
End: field.End(), | |
}, | |
}, | |
}, | |
} |
// violationMatcher is a required regular expression | ||
// used to identify violating portions of a field name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular regex library we are using? Should we document which regex version we are compatible with? Go's standard library regix is RE2 compatible IIRC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using Go's standard library - I'll update this documentation
|
||
type NoTimeStampTestStruct struct { | ||
// +optional | ||
TimeStamp *time.Time `json:"timeStamp,omitempty"` // want "field TimeStamp: prefer use of the term time over timestamp" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we don't have field <>
in the name, it makes exceptions hard to craft in the golangci-lint file, I think ideally we would restore this part
*/ | ||
|
||
package notimestamp_test | ||
package notimestamp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't normally need to do this, we use the public Initializer()
for the test normally
pkg/analysis/notimestamp/analyzer.go
Outdated
ViolationMatcher: "(?i)timestamp", | ||
Operation: namingconventions.OperationReplace, | ||
Message: "prefer use of the term 'time' over 'timestamp'", | ||
Replace: "Time", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacement?
It has only just clicked to me that this is the new string, I thought we were replacing this initially.
IIUC, the section that matches the ViolationMatcher
gets replaced with this string right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacement?
It has only just clicked to me that this is the new string, I thought we were replacing this initially.
Fair enough. I used replace
because the operation is Replace
, but I can change both to "replacement" if that would be more clear.
IIUC, the section that matches the ViolationMatcher gets replaced with this string right?
Correct
limitations under the License. | ||
*/ | ||
package nophase_test | ||
package nophase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be able to use the public Initializer()
function rather than changing this
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestConditions(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func TestConditions(t *testing.T) { | |
func TestNamingConventions(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
2b67e7b
to
8d19d9e
Compare
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: everettraven, JoelSpeed The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Adds a new linter,
namingconventions
, that allows for configuration of custom naming conventions to be enforced on both Go type and serialized field names.Additionally, this PR updates the
nophase
andnotimestamp
linters to wrap thenamingconventions
linter similarly to how thenonullable
linter wraps theforbiddenmarkers
linter.This should make future default field naming convention linters much easier to write, while giving users flexibility to add their own custom naming conventions.