Skip to content

Commit

Permalink
regexp based false positive strings filtration (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarasmadan authored Nov 10, 2023
1 parent fd5a494 commit 4945fde
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Usage:
Flags:
-ignore exclude files matching the given regular expression
-ignore-strings exclude strings matching the given regular expression
-ignore-tests exclude tests from the search (default: true)
-min-occurrences report from how many occurrences (default: 2)
-min-length only report strings with the minimum given length (default: 3)
Expand Down
2 changes: 2 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Issue struct {
}

type Config struct {
IgnoreStrings string
IgnoreTests bool
MatchWithConstants bool
MinStringLength int
Expand All @@ -28,6 +29,7 @@ func Run(files []*ast.File, fset *token.FileSet, cfg *Config) ([]Issue, error) {
p := New(
"",
"",
cfg.IgnoreStrings,
cfg.IgnoreTests,
cfg.MatchWithConstants,
cfg.ParseNumbers,
Expand Down
3 changes: 3 additions & 0 deletions cmd/goconst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Usage:
Flags:
-ignore exclude files matching the given regular expression
-ignore-strings exclude strings matching the given regular expression
-ignore-tests exclude tests from the search (default: true)
-min-occurrences report from how many occurrences (default: 2)
-min-length only report strings with the minimum given length (default: 3)
Expand All @@ -42,6 +43,7 @@ Examples:

var (
flagIgnore = flag.String("ignore", "", "ignore files matching the given regular expression")
flagIgnoreStrings = flag.String("ignore-strings", "", "ignore strings matching the given regular expression")
flagIgnoreTests = flag.Bool("ignore-tests", true, "exclude tests from the search")
flagMinOccurrences = flag.Int("min-occurrences", 2, "report from how many occurrences")
flagMinLength = flag.Int("min-length", 3, "only report strings with the minimum given length")
Expand Down Expand Up @@ -89,6 +91,7 @@ func run(path string) (bool, error) {
gco := goconst.New(
path,
*flagIgnore,
*flagIgnoreStrings,
*flagIgnoreTests,
*flagMatchConstant,
*flagNumbers,
Expand Down
23 changes: 17 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const (

type Parser struct {
// Meant to be passed via New()
path, ignore string
ignoreTests, matchConstant bool
minLength, minOccurrences int
numberMin, numberMax int
excludeTypes map[Type]bool
path, ignore, ignoreStrings string
ignoreTests, matchConstant bool
minLength, minOccurrences int
numberMin, numberMax int
excludeTypes map[Type]bool

supportedTokens []token.Token

Expand All @@ -39,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, excludeTypes map[Type]bool) *Parser {
func New(path, ignore, ignoreStrings 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 @@ -48,6 +48,7 @@ func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMi
return &Parser{
path: path,
ignore: ignore,
ignoreStrings: ignoreStrings,
ignoreTests: ignoreTests,
matchConstant: matchConstant,
minLength: minLength,
Expand Down Expand Up @@ -98,6 +99,16 @@ func (p *Parser) ProcessResults() {
delete(p.strs, str)
}

if p.ignoreStrings != "" {
match, err := regexp.MatchString(p.ignoreStrings, str)
if err != nil {
log.Println(err)
}
if match {
delete(p.strs, str)
}
}

// If the value is a number
if i, err := strconv.ParseInt(str, 0, 0); err == nil {
if p.numberMin != 0 && i < int64(p.numberMin) {
Expand Down

0 comments on commit 4945fde

Please sign in to comment.