diff --git a/api.go b/api.go index f5f479b..e58894b 100644 --- a/api.go +++ b/api.go @@ -19,6 +19,7 @@ type Config struct { ParseNumbers bool NumberMin int NumberMax int + ExcludeTypes map[Type]bool } func Run(files []*ast.File, fset *token.FileSet, cfg *Config) ([]Issue, error) { @@ -32,6 +33,7 @@ func Run(files []*ast.File, fset *token.FileSet, cfg *Config) ([]Issue, error) { cfg.NumberMax, cfg.MinStringLength, cfg.MinOccurrences, + cfg.ExcludeTypes, ) var issues []Issue for _, f := range files { diff --git a/cmd/goconst/main.go b/cmd/goconst/main.go index 288c5d1..52babb0 100644 --- a/cmd/goconst/main.go +++ b/cmd/goconst/main.go @@ -94,6 +94,7 @@ func run(path string) (bool, error) { *flagMax, *flagMinLength, *flagMinOccurrences, + map[goconst.Type]bool{}, ) strs, consts, err := gco.ParseTree() if err != nil { diff --git a/parser.go b/parser.go index d44709b..2ed7a9a 100644 --- a/parser.go +++ b/parser.go @@ -28,6 +28,7 @@ type Parser struct { ignoreTests, matchConstant bool minLength, minOccurrences int numberMin, numberMax int + excludeTypes map[Type]bool supportedTokens []token.Token @@ -38,7 +39,7 @@ type Parser struct { // New creates a new instance of the parser. // This is your entry point if you'd like to use goconst as an API. -func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMin, numberMax, minLength, minOccurrences int) *Parser { +func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMin, numberMax, minLength, minOccurrences int, excludeTypes map[Type]bool) *Parser { supportedTokens := []token.Token{token.STRING} if numbers { supportedTokens = append(supportedTokens, token.INT, token.FLOAT) @@ -54,6 +55,7 @@ func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMi numberMin: numberMin, numberMax: numberMax, supportedTokens: supportedTokens, + excludeTypes: excludeTypes, // Initialize the maps strs: Strings{}, @@ -162,3 +164,13 @@ type ExtendedPos struct { token.Position packageName string } + +type Type int + +const ( + Assignment Type = iota + Binary + Case + Return + Call +) diff --git a/visitor.go b/visitor.go index b14f7c7..c0974da 100644 --- a/visitor.go +++ b/visitor.go @@ -57,7 +57,7 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor { continue } - v.addString(lit.Value, rhs.(*ast.BasicLit).Pos()) + v.addString(lit.Value, rhs.(*ast.BasicLit).Pos(), Assignment) } // if foo == "moo" @@ -71,12 +71,12 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor { lit, ok = t.X.(*ast.BasicLit) if ok && v.isSupported(lit.Kind) { - v.addString(lit.Value, lit.Pos()) + v.addString(lit.Value, lit.Pos(), Binary) } lit, ok = t.Y.(*ast.BasicLit) if ok && v.isSupported(lit.Kind) { - v.addString(lit.Value, lit.Pos()) + v.addString(lit.Value, lit.Pos(), Binary) } // case "foo": @@ -84,7 +84,7 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor { for _, item := range t.List { lit, ok := item.(*ast.BasicLit) if ok && v.isSupported(lit.Kind) { - v.addString(lit.Value, lit.Pos()) + v.addString(lit.Value, lit.Pos(), Case) } } @@ -93,7 +93,7 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor { for _, item := range t.Results { lit, ok := item.(*ast.BasicLit) if ok && v.isSupported(lit.Kind) { - v.addString(lit.Value, lit.Pos()) + v.addString(lit.Value, lit.Pos(), Return) } } @@ -102,7 +102,7 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor { for _, item := range t.Args { lit, ok := item.(*ast.BasicLit) if ok && v.isSupported(lit.Kind) { - v.addString(lit.Value, lit.Pos()) + v.addString(lit.Value, lit.Pos(), Call) } } } @@ -111,7 +111,11 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor { } // addString adds a string in the map along with its position in the tree. -func (v *treeVisitor) addString(str string, pos token.Pos) { +func (v *treeVisitor) addString(str string, pos token.Pos, typ Type) { + ok, excluded := v.p.excludeTypes[typ] + if ok && excluded { + return + } // Drop quotes if any if strings.HasPrefix(str, `"`) || strings.HasPrefix(str, "`") { str, _ = strconv.Unquote(str) @@ -126,7 +130,7 @@ func (v *treeVisitor) addString(str string, pos token.Pos) { return } - _, ok := v.p.strs[str] + _, ok = v.p.strs[str] if !ok { v.p.strs[str] = make([]ExtendedPos, 0) }