From 7576609bb544dc5e4ea41d1b09f288b0313e3f41 Mon Sep 17 00:00:00 2001 From: Tommy Muehle Date: Mon, 31 Aug 2020 21:23:58 +0200 Subject: [PATCH] Minor improvements --- analyzer.go | 6 +++++ checks/argument_test.go | 50 ----------------------------------------- cmd/mnd/main.go | 5 +++-- config/config.go | 2 ++ config/config_test.go | 25 ++++++++++++++++----- tools/printer/main.go | 1 + 6 files changed, 31 insertions(+), 58 deletions(-) delete mode 100644 checks/argument_test.go diff --git a/analyzer.go b/analyzer.go index 9930170..4b8629b 100644 --- a/analyzer.go +++ b/analyzer.go @@ -57,18 +57,23 @@ func run(pass *analysis.Pass) (interface{}, error) { if conf.IsCheckEnabled(checks.ArgumentCheck) { checker = append(checker, checks.NewArgumentAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.CaseCheck) { checker = append(checker, checks.NewCaseAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.ConditionCheck) { checker = append(checker, checks.NewConditionAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.OperationCheck) { checker = append(checker, checks.NewOperationAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.ReturnCheck) { checker = append(checker, checks.NewReturnAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.AssignCheck) { checker = append(checker, checks.NewAssignAnalyzer(pass, conf)) } @@ -76,6 +81,7 @@ func run(pass *analysis.Pass) (interface{}, error) { i := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) for _, c := range checker { + c := c i.Preorder(c.NodeFilter(), func(node ast.Node) { for _, exclude := range conf.Excludes { if exclude.MatchString(pass.Fset.Position(node.Pos()).Filename) { diff --git a/checks/argument_test.go b/checks/argument_test.go deleted file mode 100644 index 3d816c8..0000000 --- a/checks/argument_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package checks - -import ( - "github.com/stretchr/testify/assert" - - "go/ast" - "testing" -) - -var excludeTests = []struct { - name string - in *ast.SelectorExpr - out bool -}{ - { - "time.Date", - &ast.SelectorExpr{ - X: &ast.Ident{ - Name: "time", - }, - Sel: &ast.Ident{ - Name: "Date", - }, - }, - true, - }, - { - "http.StatusText", - &ast.SelectorExpr{ - X: &ast.Ident{ - Name: "http", - }, - Sel: &ast.Ident{ - Name: "StatusText", - }, - }, - false, - }, -} - -func Test_isExcluded(t *testing.T) { - assert := assert.New(t) - a := NewArgumentAnalyzer(nil, nil) - - for _, tt := range excludeTests { - t.Run(tt.name, func(t *testing.T) { - assert.Equal(tt.out, a.isExcluded(tt.in)) - }) - } -} diff --git a/cmd/mnd/main.go b/cmd/mnd/main.go index 962b9bd..b71241f 100644 --- a/cmd/mnd/main.go +++ b/cmd/mnd/main.go @@ -1,8 +1,9 @@ package main import ( - "github.com/tommy-muehle/go-mnd" "golang.org/x/tools/go/analysis/singlechecker" + + mnd "github.com/tommy-muehle/go-mnd" ) -func main() { singlechecker.Main(magic_numbers.Analyzer) } +func main() { singlechecker.Main(mnd.Analyzer) } diff --git a/config/config.go b/config/config.go index 35c82ea..f0618d5 100644 --- a/config/config.go +++ b/config/config.go @@ -28,9 +28,11 @@ func DefaultConfig() *Config { func WithOptions(options ...Option) *Config { c := DefaultConfig() + for _, option := range options { option(c) } + return c } diff --git a/config/config_test.go b/config/config_test.go index 361b796..feb1639 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,16 +1,19 @@ -package config +package config_test import ( + "regexp" "testing" "github.com/stretchr/testify/assert" + + "github.com/tommy-muehle/go-mnd/config" ) func TestWithCustomChecks(t *testing.T) { assert := assert.New(t) - c := WithOptions( - WithCustomChecks("return,operation"), + c := config.WithOptions( + config.WithCustomChecks("return,operation"), ) assert.True(c.IsCheckEnabled("return")) @@ -25,14 +28,14 @@ func TestWithCustomChecks(t *testing.T) { func TestZeroIsIgnoredNumber(t *testing.T) { assert := assert.New(t) - assert.True(DefaultConfig().IsIgnoredNumber("0")) + assert.True(config.DefaultConfig().IsIgnoredNumber("0")) } func TestCanIgnoreCustomNumbers(t *testing.T) { assert := assert.New(t) - c := WithOptions( - WithIgnoredNumbers("1,1000"), + c := config.WithOptions( + config.WithIgnoredNumbers("1,1000"), ) assert.True(c.IsIgnoredNumber("0")) @@ -42,3 +45,13 @@ func TestCanIgnoreCustomNumbers(t *testing.T) { assert.False(c.IsIgnoredNumber("2")) assert.False(c.IsIgnoredNumber("999")) } + +func TestWithExcludes(t *testing.T) { + assert := assert.New(t) + + c := config.WithOptions( + config.WithExcludes(".*"), + ) + + assert.Contains(c.Excludes, regexp.MustCompile(".*")) +} diff --git a/tools/printer/main.go b/tools/printer/main.go index cc38bd9..72b7944 100644 --- a/tools/printer/main.go +++ b/tools/printer/main.go @@ -25,6 +25,7 @@ var ( func main() { fset := token.NewFileSet() + f, err := parser.ParseFile(fset, "", src, 0) if err != nil { panic(err)