Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-muehle committed Aug 31, 2020
1 parent 5a4b1c6 commit 7576609
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 58 deletions.
6 changes: 6 additions & 0 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,31 @@ 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))
}

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) {
Expand Down
50 changes: 0 additions & 50 deletions checks/argument_test.go

This file was deleted.

5 changes: 3 additions & 2 deletions cmd/mnd/main.go
Original file line number Diff line number Diff line change
@@ -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) }
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ func DefaultConfig() *Config {

func WithOptions(options ...Option) *Config {
c := DefaultConfig()

for _, option := range options {
option(c)
}

return c
}

Expand Down
25 changes: 19 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -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"))
Expand All @@ -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"))
Expand All @@ -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(".*"))
}
1 change: 1 addition & 0 deletions tools/printer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (

func main() {
fset := token.NewFileSet()

f, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
panic(err)
Expand Down

0 comments on commit 7576609

Please sign in to comment.