-
Notifications
You must be signed in to change notification settings - Fork 14
linters: add forbiddenmarkers and nonullable linters #111
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
base: main
Are you sure you want to change the base?
linters: add forbiddenmarkers and nonullable linters #111
Conversation
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: everettraven The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
I'll need to add the linter documentation to the new location as well :P /hold |
const name = "forbiddenmarkers" | ||
|
||
type analyzer struct { | ||
forbiddenMarkers []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.
What if I wanted to forbid a marker only if it had a certain parameter? Or a certain value?
E.g. I want to prevent someone from using a particular kubebuilder:validation:Format
, would this allow me to do that?
Or I wanted to say, you can't use the optionalOldSelf
property on an XValidation?
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.
Good questions. This would not allow that for now. I think those are reasonable use cases and make sense to consider.
I can add some configuration options to allow for this type of customization.
pkg/analysis/nonullable/analyzer.go
Outdated
analyzer.Name = name | ||
analyzer.Doc = "Check that nullable marker is not present on any types or fields." |
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.
Maybe create a constructor on the forbidden markers side that allows you to pass options for Name and Doc, rather than overriding them here? Then this just becomes
return NewAnalyzerWithOpts(forbidden.Opts{
Name: ...
Doc: ...
Markers: []string{...}
})
Or even, you could just call that inside the initializer.
* The `nonullable` linter ensures that types and fields do not have the `nullable` marker. | ||
* | ||
* Fixes are suggested to remove the `nullable` marker. | ||
* |
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.
Why the asterisks at the beginning of each line?
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.
Copied format from another linter doc.go - but also I think my editor does this by default. I can strip the asterisks at the beginning of each line if necessary
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.
Doesn't matter to me, but normally with /* */
syntax I didn't think it was needed
// Initializer returns the AnalyzerInitializer for this | ||
// Analyzer so that it can be added to the registry. | ||
func Initializer() initializer { | ||
return initializer{} | ||
} | ||
|
||
// intializer implements the AnalyzerInitializer interface. | ||
type initializer struct{} | ||
|
||
// Name returns the name of the Analyzer. | ||
func (initializer) Name() string { | ||
return name | ||
} | ||
|
||
// Init returns the intialized Analyzer. | ||
func (initializer) Init(_ config.LintersConfig) (*analysis.Analyzer, error) { | ||
return newAnalyzer(), nil | ||
} | ||
|
||
// Default determines whether this Analyzer is on by default, or not. | ||
func (initializer) Default() bool { | ||
return true | ||
} |
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'm starting to wonder if we should create a util to generate the initializer from a func 🤔
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 like that idea, but to keep the scope of this PR tightened to these two linters I'd suggest that be a follow up.
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.
This is now implemented, PR will need a rebase
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
Working on allowing configuration of forbidden attributes and values. /hold |
PR needs rebase. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
Adds a disabled by default
forbiddenmarkers
linter that has configuration options to specify marker identifiers that should never exist on a type/field. Suggests fixes to remove the forbidden markers.Adds an enabled by default
nonullable
linter that wraps theforbiddenmarkers
linter and configures thenullable
marker to be forbidden.Fixes #100