Skip to content

Commit

Permalink
Fix pkgnames
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Sep 11, 2023
1 parent 4d9530b commit 8fccb0b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
24 changes: 21 additions & 3 deletions analyzer/decisions/pkgnames/pkgnames.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pkgnames
import (
"fmt"
"go/ast"
"slices"
"strings"

"github.com/gostaticanalysis/comment/passes/commentmap"
Expand All @@ -16,9 +17,18 @@ const (
name = "pkgnames"
doc = "Analyzer based on https://google.github.io/styleguide/go/decisions#package-names"
msg = "Go package names should be short and contain only lowercase letters. A package name composed of multiple words should be left unbroken in all lowercase. (ref: https://google.github.io/styleguide/go/decisions#package-names)"
msg2 = "Avoid uninformative package names like util, utility, common, helper, and so on. (ref: https://google.github.io/styleguide/go/decisions#package-names)"
)

var disable bool
var (
disable bool
uninformatives = []string{
"util",
"utility",
"common",
"helper",
}
)

// Analyzer based on https://google.github.io/styleguide/go/decisions#package-names
var Analyzer = &analysis.Analyzer{
Expand Down Expand Up @@ -57,8 +67,16 @@ func run(pass *analysis.Pass) (any, error) {
pkg = true
return
case *ast.Ident:
if pkg && strings.Contains(strings.TrimSuffix(n.Name, "_test"), "_") {
r.Append(n.Pos(), fmt.Sprintf("%s: %s", msg, n.Name))
if pkg {
if strings.Contains(strings.TrimSuffix(n.Name, "_test"), "_") {
r.Append(n.Pos(), fmt.Sprintf("%s: %s", msg, n.Name))
}
if strings.ToLower(n.Name) != n.Name {
r.Append(n.Pos(), fmt.Sprintf("%s: %s", msg, n.Name))
}
if slices.Contains(uninformatives, strings.ToLower(n.Name)) {
r.Append(n.Pos(), fmt.Sprintf("%s: %s", msg2, n.Name))
}
}
}
pkg = false
Expand Down
2 changes: 1 addition & 1 deletion analyzer/decisions/pkgnames/pkgnames_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import (
// TestAnalyzer is a test for Analyzer.
func TestAnalyzer(t *testing.T) {
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.Run(t, testdata, Analyzer, "a", "b_bar", "c_car")
analysistest.Run(t, testdata, Analyzer, "a", "b_bar", "c_car", "util")
}
3 changes: 3 additions & 0 deletions analyzer/decisions/pkgnames/testdata/src/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package util // want "package"

func f() {}

0 comments on commit 8fccb0b

Please sign in to comment.