Skip to content

Commit

Permalink
Add exception for version (#10)
Browse files Browse the repository at this point in the history
### What
Add exception for global variables named version.

### Why
Current best practice for setting the version of a Go executable requires a global variable. This change adds a narrow exception for variables name version exactly.
  • Loading branch information
ww-daniel-mora authored and leighmcculloch committed Mar 6, 2019
1 parent abbdf6e commit 7c3491d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
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`

## Install

```
Expand Down
2 changes: 1 addition & 1 deletion check_no_globals.go
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
9 changes: 9 additions & 0 deletions check_no_globals_test.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
},
},
}
Expand Down
5 changes: 5 additions & 0 deletions testdata/9/code.go
@@ -0,0 +1,5 @@
package code

var Version string
var version22 string
var version string

0 comments on commit 7c3491d

Please sign in to comment.