Skip to content

Commit 4945fde

Browse files
authored
regexp based false positive strings filtration (#23)
1 parent fd5a494 commit 4945fde

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Usage:
2323
Flags:
2424
2525
-ignore exclude files matching the given regular expression
26+
-ignore-strings exclude strings matching the given regular expression
2627
-ignore-tests exclude tests from the search (default: true)
2728
-min-occurrences report from how many occurrences (default: 2)
2829
-min-length only report strings with the minimum given length (default: 3)

api.go

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Issue struct {
1414
}
1515

1616
type Config struct {
17+
IgnoreStrings string
1718
IgnoreTests bool
1819
MatchWithConstants bool
1920
MinStringLength int
@@ -28,6 +29,7 @@ func Run(files []*ast.File, fset *token.FileSet, cfg *Config) ([]Issue, error) {
2829
p := New(
2930
"",
3031
"",
32+
cfg.IgnoreStrings,
3133
cfg.IgnoreTests,
3234
cfg.MatchWithConstants,
3335
cfg.ParseNumbers,

cmd/goconst/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Usage:
2121
Flags:
2222
2323
-ignore exclude files matching the given regular expression
24+
-ignore-strings exclude strings matching the given regular expression
2425
-ignore-tests exclude tests from the search (default: true)
2526
-min-occurrences report from how many occurrences (default: 2)
2627
-min-length only report strings with the minimum given length (default: 3)
@@ -42,6 +43,7 @@ Examples:
4243

4344
var (
4445
flagIgnore = flag.String("ignore", "", "ignore files matching the given regular expression")
46+
flagIgnoreStrings = flag.String("ignore-strings", "", "ignore strings matching the given regular expression")
4547
flagIgnoreTests = flag.Bool("ignore-tests", true, "exclude tests from the search")
4648
flagMinOccurrences = flag.Int("min-occurrences", 2, "report from how many occurrences")
4749
flagMinLength = flag.Int("min-length", 3, "only report strings with the minimum given length")
@@ -89,6 +91,7 @@ func run(path string) (bool, error) {
8991
gco := goconst.New(
9092
path,
9193
*flagIgnore,
94+
*flagIgnoreStrings,
9295
*flagIgnoreTests,
9396
*flagMatchConstant,
9497
*flagNumbers,

parser.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ const (
2424

2525
type Parser struct {
2626
// Meant to be passed via New()
27-
path, ignore string
28-
ignoreTests, matchConstant bool
29-
minLength, minOccurrences int
30-
numberMin, numberMax int
31-
excludeTypes map[Type]bool
27+
path, ignore, ignoreStrings string
28+
ignoreTests, matchConstant bool
29+
minLength, minOccurrences int
30+
numberMin, numberMax int
31+
excludeTypes map[Type]bool
3232

3333
supportedTokens []token.Token
3434

@@ -39,7 +39,7 @@ type Parser struct {
3939

4040
// New creates a new instance of the parser.
4141
// This is your entry point if you'd like to use goconst as an API.
42-
func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMin, numberMax, minLength, minOccurrences int, excludeTypes map[Type]bool) *Parser {
42+
func New(path, ignore, ignoreStrings string, ignoreTests, matchConstant, numbers bool, numberMin, numberMax, minLength, minOccurrences int, excludeTypes map[Type]bool) *Parser {
4343
supportedTokens := []token.Token{token.STRING}
4444
if numbers {
4545
supportedTokens = append(supportedTokens, token.INT, token.FLOAT)
@@ -48,6 +48,7 @@ func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMi
4848
return &Parser{
4949
path: path,
5050
ignore: ignore,
51+
ignoreStrings: ignoreStrings,
5152
ignoreTests: ignoreTests,
5253
matchConstant: matchConstant,
5354
minLength: minLength,
@@ -98,6 +99,16 @@ func (p *Parser) ProcessResults() {
9899
delete(p.strs, str)
99100
}
100101

102+
if p.ignoreStrings != "" {
103+
match, err := regexp.MatchString(p.ignoreStrings, str)
104+
if err != nil {
105+
log.Println(err)
106+
}
107+
if match {
108+
delete(p.strs, str)
109+
}
110+
}
111+
101112
// If the value is a number
102113
if i, err := strconv.ParseInt(str, 0, 0); err == nil {
103114
if p.numberMin != 0 && i < int64(p.numberMin) {

0 commit comments

Comments
 (0)