Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into deprecate-whitelist…
Browse files Browse the repository at this point in the history
…-blacklist
  • Loading branch information
mfederowicz committed Nov 30, 2023
2 parents 1d733c6 + 782f0f1 commit 0188281
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
10 changes: 10 additions & 0 deletions RULES_DESCRIPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 24 additions & 16 deletions rule/var-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,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]))
}
r.upperCaseConst = fmt.Sprint(args["upperCaseConst"]) == "true"
r.skipPackageNameChecks = fmt.Sprint(args["skipPackageNameChecks"]) == "true"

Check failure on line 59 in rule/var-naming.go

View workflow job for this annotation

GitHub Actions / Test (1.20.x)

r.skipPackageNameChecks undefined (type *VarNamingRule has no field or method skipPackageNameChecks)

Check failure on line 59 in rule/var-naming.go

View workflow job for this annotation

GitHub Actions / Test (1.21.x)

r.skipPackageNameChecks undefined (type *VarNamingRule has no field or method skipPackageNameChecks)
}
}

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.
func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
Expand All @@ -78,22 +100,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 {

Check failure on line 103 in rule/var-naming.go

View workflow job for this annotation

GitHub Actions / Test (1.20.x)

r.skipPackageNameChecks undefined (type *VarNamingRule has no field or method skipPackageNameChecks)

Check failure on line 103 in rule/var-naming.go

View workflow job for this annotation

GitHub Actions / Test (1.21.x)

r.skipPackageNameChecks undefined (type *VarNamingRule has no field or method skipPackageNameChecks)
r.applyPackageCheckRules(&walker)
}

ast.Walk(&walker, fileAst)
Expand Down
6 changes: 6 additions & 0 deletions test/var-naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}},
})

}
3 changes: 3 additions & 0 deletions testdata/var-naming_skipPackageNameChecks-false.go
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions testdata/var-naming_skipPackageNameChecks-true.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// should pass if skipPackageNameChecks = true

package pkg_with_underscores

0 comments on commit 0188281

Please sign in to comment.