Skip to content

Commit

Permalink
Making type of registered events configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
iwankgb authored and jgautheron committed Nov 17, 2020
1 parent f8e4fe8 commit ccae5bf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/goconst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Parser struct {
ignoreTests, matchConstant bool
minLength, minOccurrences int
numberMin, numberMax int
excludeTypes map[Type]bool

supportedTokens []token.Token

Expand All @@ -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)
Expand All @@ -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{},
Expand Down Expand Up @@ -162,3 +164,13 @@ type ExtendedPos struct {
token.Position
packageName string
}

type Type int

const (
Assignment Type = iota
Binary
Case
Return
Call
)
20 changes: 12 additions & 8 deletions visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -71,20 +71,20 @@ 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":
case *ast.CaseClause:
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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit ccae5bf

Please sign in to comment.