Skip to content

Commit 6a7633b

Browse files
author
Jonathan Gautheron
committed
Implement -min-length, closes #2
1 parent ac30d44 commit 6a7633b

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Flags:
2525
-ignore exclude files matching the given regular expression
2626
-ignore-tests exclude tests from the search (default: true)
2727
-min-occurrences report from how many occurrences (default: 2)
28-
-match-constant look for existing constants matching the strings
28+
-min-length only report strings with the minimum given length (default: 3)
29+
-match-constant look for existing constants matching the values
2930
-numbers search also for duplicated numbers
3031
-min minimum value, only works with -numbers
3132
-max maximum value, only works with -numbers

cmd/goconst/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Flags:
2323
-ignore exclude files matching the given regular expression
2424
-ignore-tests exclude tests from the search (default: true)
2525
-min-occurrences report from how many occurrences (default: 2)
26+
-min-length only report strings with the minimum given length (default: 3)
2627
-match-constant look for existing constants matching the strings
2728
-numbers search also for duplicated numbers
2829
-min minimum value, only works with -numbers
@@ -41,6 +42,7 @@ var (
4142
flagIgnore = flag.String("ignore", "", "ignore files matching the given regular expression")
4243
flagIgnoreTests = flag.Bool("ignore-tests", true, "exclude tests from the search")
4344
flagMinOccurrences = flag.Int("min-occurrences", 2, "report from how many occurrences")
45+
flagMinLength = flag.Int("min-length", 3, "only report strings with the minimum given length")
4446
flagMatchConstant = flag.Bool("match-constant", false, "look for existing constants matching the strings")
4547
flagNumbers = flag.Bool("numbers", false, "search also for duplicated numbers")
4648
flagMin = flag.Int("min", 0, "minimum value, only works with -numbers")
@@ -67,6 +69,7 @@ func main() {
6769
*flagIgnoreTests,
6870
*flagMatchConstant,
6971
*flagNumbers,
72+
*flagMinLength,
7073
)
7174
strs, consts, err := gco.ParseTree()
7275
if err != nil {

parser.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Parser struct {
2525
// Meant to be passed via New()
2626
path, ignore string
2727
ignoreTests, matchConstant bool
28+
minLength int
2829

2930
supportedTokens []token.Token
3031

@@ -35,7 +36,7 @@ type Parser struct {
3536

3637
// New creates a new instance of the parser.
3738
// This is your entry point if you'd like to use goconst as an API.
38-
func New(path, ignore string, ignoreTests, matchConstant, numbers bool) *Parser {
39+
func New(path, ignore string, ignoreTests, matchConstant, numbers bool, minLength int) *Parser {
3940
supportedTokens := []token.Token{token.STRING}
4041
if numbers {
4142
supportedTokens = append(supportedTokens, token.INT, token.FLOAT)
@@ -46,6 +47,7 @@ func New(path, ignore string, ignoreTests, matchConstant, numbers bool) *Parser
4647
ignore: ignore,
4748
ignoreTests: ignoreTests,
4849
matchConstant: matchConstant,
50+
minLength: minLength,
4951
supportedTokens: supportedTokens,
5052

5153
// Initialize the maps

visitor.go

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ func (v *treeVisitor) addString(str string, pos token.Pos) {
109109
return
110110
}
111111

112+
if len(str) < v.p.minLength {
113+
return
114+
}
115+
112116
_, ok := v.p.strs[str]
113117
if !ok {
114118
v.p.strs[str] = make([]ExtendedPos, 0)

0 commit comments

Comments
 (0)