Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception for version #10

Merged
merged 5 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 to support [compile time version setting](https://medium.com/@joshroppo/setting-go-1-5-variables-at-compile-time-for-versioning-5b30a965d33e)
leighmcculloch marked this conversation as resolved.
Show resolved Hide resolved

## Install

```
Expand Down
2 changes: 1 addition & 1 deletion check_no_globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions check_no_globals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ func TestCheckNoGlobals(t *testing.T) {
"testdata/8/code.go:30 declaredErr is a global variable",
},
},
{
path: "testdata/9",
wantMessages: []string{
"testdata/9/code.go:4 Version22 is a global variable",
},
},
{
path: ".",
wantMessages: nil,
Expand Down Expand Up @@ -135,6 +141,7 @@ 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:4 Version22 is a global variable",
},
},
}
Expand Down
4 changes: 4 additions & 0 deletions testdata/9/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package code

var Version string
var Version22 string