mirror of
https://github.com/mgechev/revive.git
synced 2025-02-09 13:37:14 +02:00
variable to skip package name checks (#941)
* variable to skip package name checks * add tests for skipPackageNameChecks * Add documentation
This commit is contained in:
parent
d3aa99cf8b
commit
782f0f118c
@ -886,6 +886,16 @@ Example:
|
|||||||
arguments = [["ID"], ["VM"], [{upperCaseConst=true}]]
|
arguments = [["ID"], ["VM"], [{upperCaseConst=true}]]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can also add "skipPackageNameChecks=true" to skip package name checks.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[rule.var-naming]
|
||||||
|
arguments = [[], [], [{skipPackageNameChecks=true}]]
|
||||||
|
```
|
||||||
|
|
||||||
## waitgroup-by-value
|
## waitgroup-by-value
|
||||||
|
|
||||||
_Description_: Function parameters that are passed by value, are in fact a copy of the original argument. Passing a copy of a `sync.WaitGroup` is usually not what the developer wants to do.
|
_Description_: Function parameters that are passed by value, are in fact a copy of the original argument. Passing a copy of a `sync.WaitGroup` is usually not what the developer wants to do.
|
||||||
|
@ -22,6 +22,7 @@ type VarNamingRule struct {
|
|||||||
whitelist []string
|
whitelist []string
|
||||||
blacklist []string
|
blacklist []string
|
||||||
upperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants
|
upperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants
|
||||||
|
skipPackageNameChecks bool
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,9 +57,31 @@ func (r *VarNamingRule) configure(arguments lint.Arguments) {
|
|||||||
panic(fmt.Sprintf("Invalid third argument to the var-naming rule. Expecting a %s of type slice, of len==1, with map, but %T", "options", asSlice[0]))
|
panic(fmt.Sprintf("Invalid third argument to the var-naming rule. Expecting a %s of type slice, of len==1, with map, but %T", "options", asSlice[0]))
|
||||||
}
|
}
|
||||||
r.upperCaseConst = fmt.Sprint(args["upperCaseConst"]) == "true"
|
r.upperCaseConst = fmt.Sprint(args["upperCaseConst"]) == "true"
|
||||||
|
r.skipPackageNameChecks = fmt.Sprint(args["skipPackageNameChecks"]) == "true"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) {
|
||||||
|
// Package names need slightly different handling than other names.
|
||||||
|
if strings.Contains(walker.fileAst.Name.Name, "_") && !strings.HasSuffix(walker.fileAst.Name.Name, "_test") {
|
||||||
|
walker.onFailure(lint.Failure{
|
||||||
|
Failure: "don't use an underscore in package name",
|
||||||
|
Confidence: 1,
|
||||||
|
Node: walker.fileAst.Name,
|
||||||
|
Category: "naming",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if anyCapsRE.MatchString(walker.fileAst.Name.Name) {
|
||||||
|
walker.onFailure(lint.Failure{
|
||||||
|
Failure: fmt.Sprintf("don't use MixedCaps in package name; %s should be %s", walker.fileAst.Name.Name, strings.ToLower(walker.fileAst.Name.Name)),
|
||||||
|
Confidence: 1,
|
||||||
|
Node: walker.fileAst.Name,
|
||||||
|
Category: "naming",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Apply applies the rule to given file.
|
// Apply applies the rule to given file.
|
||||||
func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||||
r.configure(arguments)
|
r.configure(arguments)
|
||||||
@ -78,22 +101,8 @@ func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.
|
|||||||
upperCaseConst: r.upperCaseConst,
|
upperCaseConst: r.upperCaseConst,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Package names need slightly different handling than other names.
|
if !r.skipPackageNameChecks {
|
||||||
if strings.Contains(walker.fileAst.Name.Name, "_") && !strings.HasSuffix(walker.fileAst.Name.Name, "_test") {
|
r.applyPackageCheckRules(&walker)
|
||||||
walker.onFailure(lint.Failure{
|
|
||||||
Failure: "don't use an underscore in package name",
|
|
||||||
Confidence: 1,
|
|
||||||
Node: walker.fileAst.Name,
|
|
||||||
Category: "naming",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if anyCapsRE.MatchString(walker.fileAst.Name.Name) {
|
|
||||||
walker.onFailure(lint.Failure{
|
|
||||||
Failure: fmt.Sprintf("don't use MixedCaps in package name; %s should be %s", walker.fileAst.Name.Name, strings.ToLower(walker.fileAst.Name.Name)),
|
|
||||||
Confidence: 1,
|
|
||||||
Node: walker.fileAst.Name,
|
|
||||||
Category: "naming",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ast.Walk(&walker, fileAst)
|
ast.Walk(&walker, fileAst)
|
||||||
|
@ -18,4 +18,10 @@ func TestVarNaming(t *testing.T) {
|
|||||||
testRule(t, "var-naming_upperCaseConst-true", &rule.VarNamingRule{}, &lint.RuleConfig{
|
testRule(t, "var-naming_upperCaseConst-true", &rule.VarNamingRule{}, &lint.RuleConfig{
|
||||||
Arguments: []any{[]any{}, []any{}, []any{map[string]any{"upperCaseConst": true}}},
|
Arguments: []any{[]any{}, []any{}, []any{map[string]any{"upperCaseConst": true}}},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
testRule(t, "var-naming_skipPackageNameChecks-false", &rule.VarNamingRule{}, &lint.RuleConfig{})
|
||||||
|
testRule(t, "var-naming_skipPackageNameChecks-true", &rule.VarNamingRule{}, &lint.RuleConfig{
|
||||||
|
Arguments: []any{[]any{}, []any{}, []any{map[string]any{"skipPackageNameChecks": true}}},
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
3
testdata/var-naming_skipPackageNameChecks-false.go
vendored
Normal file
3
testdata/var-naming_skipPackageNameChecks-false.go
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// should fail if skipPackageNameChecks = false (by default)
|
||||||
|
|
||||||
|
package pkg_with_underscores // MATCH /don't use an underscore in package name/
|
3
testdata/var-naming_skipPackageNameChecks-true.go
vendored
Normal file
3
testdata/var-naming_skipPackageNameChecks-true.go
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// should pass if skipPackageNameChecks = true
|
||||||
|
|
||||||
|
package pkg_with_underscores
|
Loading…
x
Reference in New Issue
Block a user