From fe951522373cd176ef3994cd23583c9821390076 Mon Sep 17 00:00:00 2001 From: Daniel Mora Date: Mon, 4 Mar 2019 11:27:52 -0500 Subject: [PATCH 1/5] Added an exception for variables named Version --- check_no_globals.go | 2 +- check_no_globals_test.go | 7 +++++++ testdata/9/code.go | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 testdata/9/code.go diff --git a/check_no_globals.go b/check_no_globals.go index 2565b86..9378caa 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..a5488ec 100644 --- a/check_no_globals_test.go +++ b/check_no_globals_test.go @@ -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, @@ -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", }, }, } diff --git a/testdata/9/code.go b/testdata/9/code.go new file mode 100644 index 0000000..5537ba5 --- /dev/null +++ b/testdata/9/code.go @@ -0,0 +1,4 @@ +package code + +var Version string +var Version22 string From 0daeb2a4f3d52bd6906b64a9ea7c9f46d994f893 Mon Sep 17 00:00:00 2001 From: Daniel Mora Date: Mon, 4 Mar 2019 12:02:56 -0500 Subject: [PATCH 2/5] Add documentation about exceptions to the README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 636b8ad..475747c 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 will 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) + ## Install ``` From ea8099f864e05e4109890774fed8846c3e4939f7 Mon Sep 17 00:00:00 2001 From: Daniel Mora Date: Mon, 4 Mar 2019 12:07:23 -0500 Subject: [PATCH 3/5] Fix typo in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 475747c..83b0b42 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ https://twitter.com/davecheney/status/871939730761547776 ### Exceptions -There are **very** few exceptions to the global variable rule. This will will ignore the following patterns: +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) From c0c97ea8943d4b887d9db9e323e8c5bee56967eb Mon Sep 17 00:00:00 2001 From: Daniel Mora Date: Wed, 6 Mar 2019 10:27:12 -0500 Subject: [PATCH 4/5] Switch to whitelisting lowercase 'version' instead of capital 'Version' --- README.md | 2 +- check_no_globals.go | 2 +- check_no_globals_test.go | 6 ++++-- testdata/9/code.go | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 83b0b42..a9bc53f 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ https://twitter.com/davecheney/status/871939730761547776 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) + * Variables named "version" to support [compile time version setting](https://medium.com/@joshroppo/setting-go-1-5-variables-at-compile-time-for-versioning-5b30a965d33e) ## Install diff --git a/check_no_globals.go b/check_no_globals.go index 9378caa..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 == "_" || i.Name == "Version" || 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 a5488ec..571df8b 100644 --- a/check_no_globals_test.go +++ b/check_no_globals_test.go @@ -108,7 +108,8 @@ func TestCheckNoGlobals(t *testing.T) { { path: "testdata/9", wantMessages: []string{ - "testdata/9/code.go:4 Version22 is a global variable", + "testdata/9/code.go:3 Version is a global variable", + "testdata/9/code.go:4 version22 is a global variable", }, }, { @@ -141,7 +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:4 Version22 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 index 5537ba5..11e95e2 100644 --- a/testdata/9/code.go +++ b/testdata/9/code.go @@ -1,4 +1,5 @@ package code var Version string -var Version22 string +var version22 string +var version string From 935dc8135522b7d727c5c02944410b42f66a4baa Mon Sep 17 00:00:00 2001 From: Leigh McCulloch Date: Wed, 6 Mar 2019 08:22:26 -0800 Subject: [PATCH 5/5] Keep README shorter --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a9bc53f..fb12eed 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ 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) +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