Skip to content

Commit ccae5bf

Browse files
iwankgbjgautheron
authored andcommitted
Making type of registered events configurable
1 parent f8e4fe8 commit ccae5bf

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Config struct {
1919
ParseNumbers bool
2020
NumberMin int
2121
NumberMax int
22+
ExcludeTypes map[Type]bool
2223
}
2324

2425
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) {
3233
cfg.NumberMax,
3334
cfg.MinStringLength,
3435
cfg.MinOccurrences,
36+
cfg.ExcludeTypes,
3537
)
3638
var issues []Issue
3739
for _, f := range files {

cmd/goconst/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func run(path string) (bool, error) {
9494
*flagMax,
9595
*flagMinLength,
9696
*flagMinOccurrences,
97+
map[goconst.Type]bool{},
9798
)
9899
strs, consts, err := gco.ParseTree()
99100
if err != nil {

parser.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Parser struct {
2828
ignoreTests, matchConstant bool
2929
minLength, minOccurrences int
3030
numberMin, numberMax int
31+
excludeTypes map[Type]bool
3132

3233
supportedTokens []token.Token
3334

@@ -38,7 +39,7 @@ type Parser struct {
3839

3940
// New creates a new instance of the parser.
4041
// This is your entry point if you'd like to use goconst as an API.
41-
func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMin, numberMax, minLength, minOccurrences int) *Parser {
42+
func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMin, numberMax, minLength, minOccurrences int, excludeTypes map[Type]bool) *Parser {
4243
supportedTokens := []token.Token{token.STRING}
4344
if numbers {
4445
supportedTokens = append(supportedTokens, token.INT, token.FLOAT)
@@ -54,6 +55,7 @@ func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMi
5455
numberMin: numberMin,
5556
numberMax: numberMax,
5657
supportedTokens: supportedTokens,
58+
excludeTypes: excludeTypes,
5759

5860
// Initialize the maps
5961
strs: Strings{},
@@ -162,3 +164,13 @@ type ExtendedPos struct {
162164
token.Position
163165
packageName string
164166
}
167+
168+
type Type int
169+
170+
const (
171+
Assignment Type = iota
172+
Binary
173+
Case
174+
Return
175+
Call
176+
)

visitor.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
5757
continue
5858
}
5959

60-
v.addString(lit.Value, rhs.(*ast.BasicLit).Pos())
60+
v.addString(lit.Value, rhs.(*ast.BasicLit).Pos(), Assignment)
6161
}
6262

6363
// if foo == "moo"
@@ -71,20 +71,20 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
7171

7272
lit, ok = t.X.(*ast.BasicLit)
7373
if ok && v.isSupported(lit.Kind) {
74-
v.addString(lit.Value, lit.Pos())
74+
v.addString(lit.Value, lit.Pos(), Binary)
7575
}
7676

7777
lit, ok = t.Y.(*ast.BasicLit)
7878
if ok && v.isSupported(lit.Kind) {
79-
v.addString(lit.Value, lit.Pos())
79+
v.addString(lit.Value, lit.Pos(), Binary)
8080
}
8181

8282
// case "foo":
8383
case *ast.CaseClause:
8484
for _, item := range t.List {
8585
lit, ok := item.(*ast.BasicLit)
8686
if ok && v.isSupported(lit.Kind) {
87-
v.addString(lit.Value, lit.Pos())
87+
v.addString(lit.Value, lit.Pos(), Case)
8888
}
8989
}
9090

@@ -93,7 +93,7 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
9393
for _, item := range t.Results {
9494
lit, ok := item.(*ast.BasicLit)
9595
if ok && v.isSupported(lit.Kind) {
96-
v.addString(lit.Value, lit.Pos())
96+
v.addString(lit.Value, lit.Pos(), Return)
9797
}
9898
}
9999

@@ -102,7 +102,7 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
102102
for _, item := range t.Args {
103103
lit, ok := item.(*ast.BasicLit)
104104
if ok && v.isSupported(lit.Kind) {
105-
v.addString(lit.Value, lit.Pos())
105+
v.addString(lit.Value, lit.Pos(), Call)
106106
}
107107
}
108108
}
@@ -111,7 +111,11 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
111111
}
112112

113113
// addString adds a string in the map along with its position in the tree.
114-
func (v *treeVisitor) addString(str string, pos token.Pos) {
114+
func (v *treeVisitor) addString(str string, pos token.Pos, typ Type) {
115+
ok, excluded := v.p.excludeTypes[typ]
116+
if ok && excluded {
117+
return
118+
}
115119
// Drop quotes if any
116120
if strings.HasPrefix(str, `"`) || strings.HasPrefix(str, "`") {
117121
str, _ = strconv.Unquote(str)
@@ -126,7 +130,7 @@ func (v *treeVisitor) addString(str string, pos token.Pos) {
126130
return
127131
}
128132

129-
_, ok := v.p.strs[str]
133+
_, ok = v.p.strs[str]
130134
if !ok {
131135
v.p.strs[str] = make([]ExtendedPos, 0)
132136
}

0 commit comments

Comments
 (0)