Skip to content
This repository has been archived by the owner on Apr 10, 2019. It is now read-only.

Commit

Permalink
Update gochecknoglobals
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored and alecthomas committed Sep 9, 2018
1 parent 2b0b5f3 commit 17a7ffa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
36 changes: 28 additions & 8 deletions _linters/src/4d63.com/gochecknoglobals/check_no_globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,23 @@ import (
"strings"
)

func checkNoGlobals(rootPath string) ([]string, error) {
func isWhitelisted(i *ast.Ident) bool {
return i.Name == "_" || looksLikeError(i)
}

// looksLikeError returns true if the AST identifier starts
// with 'err' or 'Err', or false otherwise.
//
// TODO: https://github.com/leighmcculloch/gochecknoglobals/issues/5
func looksLikeError(i *ast.Ident) bool {
prefix := "err"
if i.IsExported() {
prefix = "Err"
}
return strings.HasPrefix(i.Name, prefix)
}

func checkNoGlobals(rootPath string, includeTests bool) ([]string, error) {
const recursiveSuffix = string(filepath.Separator) + "..."
recursive := false
if strings.HasSuffix(rootPath, recursiveSuffix) {
Expand All @@ -33,6 +49,9 @@ func checkNoGlobals(rootPath string) ([]string, error) {
if !strings.HasSuffix(path, ".go") {
return nil
}
if !includeTests && strings.HasSuffix(path, "_test.go") {
return nil
}

fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, nil, 0)
Expand All @@ -50,14 +69,15 @@ func checkNoGlobals(rootPath string) ([]string, error) {
}
filename := fset.Position(genDecl.TokPos).Filename
line := fset.Position(genDecl.TokPos).Line
valueSpec := genDecl.Specs[0].(*ast.ValueSpec)
for i := 0; i < len(valueSpec.Names); i++ {
name := valueSpec.Names[i].Name
if name == "_" {
continue
for _, spec := range genDecl.Specs {
valueSpec := spec.(*ast.ValueSpec)
for _, vn := range valueSpec.Names {
if isWhitelisted(vn) {
continue
}
message := fmt.Sprintf("%s:%d %s is a global variable", filename, line, vn.Name)
messages = append(messages, message)
}
message := fmt.Sprintf("%s:%d %s is a global variable", filename, line, name)
messages = append(messages, message)
}
}
return nil
Expand Down
10 changes: 7 additions & 3 deletions _linters/src/4d63.com/gochecknoglobals/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
)

func main() {
flagPrintHelp := flag.Bool("help", false, "")
flagPrintHelp := flag.Bool("h", false, "Print help")
flagIncludeTests := flag.Bool("t", false, "Include tests")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: gochecknoglobals [path] [path] ...\n")
fmt.Fprintf(os.Stderr, "Usage: gochecknoglobals [-t] [path] [path] ...\n")
flag.PrintDefaults()
}
flag.Parse()

Expand All @@ -18,6 +20,8 @@ func main() {
return
}

includeTests := *flagIncludeTests

paths := flag.Args()
if len(paths) == 0 {
paths = []string{"./..."}
Expand All @@ -26,7 +30,7 @@ func main() {
exitWithError := false

for _, path := range paths {
messages, err := checkNoGlobals(path)
messages, err := checkNoGlobals(path, includeTests)
for _, message := range messages {
fmt.Fprintf(os.Stdout, "%s\n", message)
exitWithError = true
Expand Down
2 changes: 1 addition & 1 deletion _linters/src/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"importpath": "4d63.com/gochecknoglobals",
"repository": "https://github.com/leighmcculloch/gochecknoglobals",
"vcs": "git",
"revision": "9a66a4a931c82990a7eb42e057408ac5f2bb6bee",
"revision": "5090db600a84f7a401cb031f4e8e2e420b0e1ecd",
"branch": "master",
"notests": true
},
Expand Down

0 comments on commit 17a7ffa

Please sign in to comment.