diff --git a/README.md b/README.md index 636b8ad..fb12eed 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,13 @@ Global variables are an input to functions that is not visible in the functions https://peter.bourgon.org/blog/2017/06/09/theory-of-modern-go.html https://twitter.com/davecheney/status/871939730761547776 +### Exceptions + +There are very few exceptions to the global variable rule. This tool will ignore the following patterns: + * Variables with an `Err` prefix + * Variables named `_` + * Variables named `version` + ## Install ``` diff --git a/check_no_globals.go b/check_no_globals.go index 2565b86..18a932f 100644 --- a/check_no_globals.go +++ b/check_no_globals.go @@ -11,7 +11,7 @@ import ( ) func isWhitelisted(i *ast.Ident) bool { - return i.Name == "_" || looksLikeError(i) + return i.Name == "_" || i.Name == "version" || looksLikeError(i) } // looksLikeError returns true if the AST identifier starts diff --git a/check_no_globals_test.go b/check_no_globals_test.go index 6ae3c4e..571df8b 100644 --- a/check_no_globals_test.go +++ b/check_no_globals_test.go @@ -105,6 +105,13 @@ func TestCheckNoGlobals(t *testing.T) { "testdata/8/code.go:30 declaredErr is a global variable", }, }, + { + path: "testdata/9", + wantMessages: []string{ + "testdata/9/code.go:3 Version is a global variable", + "testdata/9/code.go:4 version22 is a global variable", + }, + }, { path: ".", wantMessages: nil, @@ -135,6 +142,8 @@ func TestCheckNoGlobals(t *testing.T) { "testdata/8/code.go:20 myVarError is a global variable", "testdata/8/code.go:21 customErr is a global variable", "testdata/8/code.go:30 declaredErr is a global variable", + "testdata/9/code.go:3 Version is a global variable", + "testdata/9/code.go:4 version22 is a global variable", }, }, } diff --git a/testdata/9/code.go b/testdata/9/code.go new file mode 100644 index 0000000..11e95e2 --- /dev/null +++ b/testdata/9/code.go @@ -0,0 +1,5 @@ +package code + +var Version string +var version22 string +var version string