Skip to content

Commit b1fecdb

Browse files
author
Jonathan Gautheron
committed
Add the possibility to search also for numbers; fixes #1
1 parent d188494 commit b1fecdb

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Flags:
2626
-ignore-tests exclude tests from the search (default: true)
2727
-min-occurrences report from how many occurrences (default: 2)
2828
-match-constant look for existing constants matching the strings
29+
-numbers search also for duplicated numbers
2930
-output output formatting (text or json)
3031
3132
Examples:

cmd/goconst/main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Flags:
2323
-ignore-tests exclude tests from the search (default: true)
2424
-min-occurrences report from how many occurrences (default: 2)
2525
-match-constant look for existing constants matching the strings
26+
-numbers search also for duplicated numbers
2627
-output output formatting (text or json)
2728
2829
Examples:
@@ -37,6 +38,7 @@ var (
3738
flagIgnoreTests = flag.Bool("ignore-tests", true, "exclude tests from the search")
3839
flagMinOccurrences = flag.Int("min-occurrences", 2, "report from how many occurrences")
3940
flagMatchConstant = flag.Bool("match-constant", false, "look for existing constants matching the strings")
41+
flagNumbers = flag.Bool("numbers", false, "search also for duplicated numbers")
4042
flagOutput = flag.String("output", "text", "output formatting")
4143
)
4244

@@ -58,6 +60,7 @@ func main() {
5860
*flagIgnore,
5961
*flagIgnoreTests,
6062
*flagMatchConstant,
63+
*flagNumbers,
6164
)
6265
strs, consts, err := gco.ParseTree()
6366
if err != nil {
@@ -84,12 +87,15 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
8487
switch output {
8588
case "json":
8689
enc := json.NewEncoder(os.Stdout)
87-
enc.Encode(struct {
90+
err := enc.Encode(struct {
8891
Strings goconst.Strings `json:"strings,omitEmpty"`
8992
Constants goconst.Constants `json:"constants,omitEmpty"`
9093
}{
9194
strs, consts,
9295
})
96+
if err != nil {
97+
log.Fatal(err)
98+
}
9399
case "text":
94100
for str, item := range strs {
95101
for _, xpos := range item {

parser.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,27 @@ type Parser struct {
2626
path, ignore string
2727
ignoreTests, matchConstant bool
2828

29+
supportedTokens []token.Token
30+
2931
// Internals
3032
strs Strings
3133
consts Constants
3234
}
3335

3436
// New creates a new instance of the parser.
3537
// This is your entry point if you'd like to use goconst as an API.
36-
func New(path, ignore string, ignoreTests, matchConstant bool) *Parser {
38+
func New(path, ignore string, ignoreTests, matchConstant, numbers bool) *Parser {
39+
supportedTokens := []token.Token{token.STRING}
40+
if numbers {
41+
supportedTokens = append(supportedTokens, token.INT, token.FLOAT)
42+
}
43+
3744
return &Parser{
38-
path: path,
39-
ignore: ignore,
40-
ignoreTests: ignoreTests,
41-
matchConstant: matchConstant,
45+
path: path,
46+
ignore: ignore,
47+
ignoreTests: ignoreTests,
48+
matchConstant: matchConstant,
49+
supportedTokens: supportedTokens,
4250

4351
// Initialize the maps
4452
strs: Strings{},

visitor.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77
)
88

9-
// TreeVisitor carries the package name and file name
9+
// treeVisitor carries the package name and file name
1010
// for passing it to the imports map, and the fileSet for
1111
// retrieving the token.Position.
1212
type treeVisitor struct {
@@ -40,19 +40,19 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
4040
val := spec.(*ast.ValueSpec)
4141
for i, str := range val.Values {
4242
lit, ok := str.(*ast.BasicLit)
43-
if !ok || lit.Kind != token.STRING {
43+
if !ok || !v.isSupported(lit.Kind) {
4444
continue
4545
}
4646

4747
v.addConst(val.Names[i].Name, lit.Value, val.Names[i].Pos())
4848
}
4949
}
5050

51-
// foo := "moo"
51+
// foo := "moo"
5252
case *ast.AssignStmt:
5353
for _, rhs := range t.Rhs {
5454
lit, ok := rhs.(*ast.BasicLit)
55-
if !ok || lit.Kind != token.STRING {
55+
if !ok || !v.isSupported(lit.Kind) {
5656
continue
5757
}
5858

@@ -69,20 +69,20 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
6969
var ok bool
7070

7171
lit, ok = t.X.(*ast.BasicLit)
72-
if ok && lit.Kind == token.STRING {
72+
if ok && v.isSupported(lit.Kind) {
7373
v.addString(lit.Value, lit.Pos())
7474
}
7575

7676
lit, ok = t.Y.(*ast.BasicLit)
77-
if ok && lit.Kind == token.STRING {
77+
if ok && v.isSupported(lit.Kind) {
7878
v.addString(lit.Value, lit.Pos())
7979
}
8080

8181
// case "foo":
8282
case *ast.CaseClause:
8383
for _, item := range t.List {
8484
lit, ok := item.(*ast.BasicLit)
85-
if ok && lit.Kind == token.STRING {
85+
if ok && v.isSupported(lit.Kind) {
8686
v.addString(lit.Value, lit.Pos())
8787
}
8888
}
@@ -91,7 +91,7 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
9191
case *ast.ReturnStmt:
9292
for _, item := range t.Results {
9393
lit, ok := item.(*ast.BasicLit)
94-
if ok && lit.Kind == token.STRING {
94+
if ok && v.isSupported(lit.Kind) {
9595
v.addString(lit.Value, lit.Pos())
9696
}
9797
}
@@ -128,3 +128,12 @@ func (v *treeVisitor) addConst(name string, val string, pos token.Pos) {
128128
Position: v.fileSet.Position(pos),
129129
}
130130
}
131+
132+
func (v *treeVisitor) isSupported(tk token.Token) bool {
133+
for _, s := range v.p.supportedTokens {
134+
if tk == s {
135+
return true
136+
}
137+
}
138+
return false
139+
}

0 commit comments

Comments
 (0)