From 782f0f118c3bab70d7bd4e70f201657fbd867115 Mon Sep 17 00:00:00 2001 From: Georgy Buranov Date: Thu, 30 Nov 2023 14:19:31 +0100 Subject: [PATCH] variable to skip package name checks (#941) * variable to skip package name checks * add tests for skipPackageNameChecks * Add documentation --- RULES_DESCRIPTIONS.md | 10 ++++ rule/var-naming.go | 49 +++++++++++-------- test/var-naming_test.go | 6 +++ .../var-naming_skipPackageNameChecks-false.go | 3 ++ .../var-naming_skipPackageNameChecks-true.go | 3 ++ 5 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 testdata/var-naming_skipPackageNameChecks-false.go create mode 100644 testdata/var-naming_skipPackageNameChecks-true.go diff --git a/RULES_DESCRIPTIONS.md b/RULES_DESCRIPTIONS.md index c6096e45a..49d8f45c8 100644 --- a/RULES_DESCRIPTIONS.md +++ b/RULES_DESCRIPTIONS.md @@ -886,6 +886,16 @@ Example: 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 _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. diff --git a/rule/var-naming.go b/rule/var-naming.go index 7f0ce7155..8dfba5b41 100644 --- a/rule/var-naming.go +++ b/rule/var-naming.go @@ -18,10 +18,11 @@ var upperCaseConstRE = regexp.MustCompile(`^_?[A-Z][A-Z\d]*(_[A-Z\d]+)*$`) // VarNamingRule lints given else constructs. type VarNamingRule struct { - configured bool - whitelist []string - blacklist []string - upperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants + configured bool + whitelist []string + blacklist []string + upperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants + skipPackageNameChecks bool sync.Mutex } @@ -56,7 +57,29 @@ 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])) } 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. @@ -78,22 +101,8 @@ func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint. upperCaseConst: r.upperCaseConst, } - // 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", - }) + if !r.skipPackageNameChecks { + r.applyPackageCheckRules(&walker) } ast.Walk(&walker, fileAst) diff --git a/test/var-naming_test.go b/test/var-naming_test.go index 5ceb7dd01..f55428670 100644 --- a/test/var-naming_test.go +++ b/test/var-naming_test.go @@ -18,4 +18,10 @@ func TestVarNaming(t *testing.T) { testRule(t, "var-naming_upperCaseConst-true", &rule.VarNamingRule{}, &lint.RuleConfig{ 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}}}, + }) + } diff --git a/testdata/var-naming_skipPackageNameChecks-false.go b/testdata/var-naming_skipPackageNameChecks-false.go new file mode 100644 index 000000000..7fb06a973 --- /dev/null +++ b/testdata/var-naming_skipPackageNameChecks-false.go @@ -0,0 +1,3 @@ +// should fail if skipPackageNameChecks = false (by default) + +package pkg_with_underscores // MATCH /don't use an underscore in package name/ diff --git a/testdata/var-naming_skipPackageNameChecks-true.go b/testdata/var-naming_skipPackageNameChecks-true.go new file mode 100644 index 000000000..e1110064e --- /dev/null +++ b/testdata/var-naming_skipPackageNameChecks-true.go @@ -0,0 +1,3 @@ +// should pass if skipPackageNameChecks = true + +package pkg_with_underscores