-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f5268c
commit ce0d7c0
Showing
6 changed files
with
124 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package goconst | ||
|
||
import ( | ||
"go/ast" | ||
"go/token" | ||
) | ||
|
||
type Issue struct { | ||
Pos token.Position | ||
OccurrencesCount int | ||
Str string | ||
MatchingConst string | ||
} | ||
|
||
type Config struct { | ||
MatchWithConstants bool | ||
MinStringLength int | ||
MinOccurrences int | ||
ParseNumbers bool | ||
NumberMin int | ||
NumberMax int | ||
} | ||
|
||
func Run(files []*ast.File, fset *token.FileSet, cfg *Config) ([]Issue, error) { | ||
p := New( | ||
"", | ||
"", | ||
false, | ||
cfg.MatchWithConstants, | ||
cfg.ParseNumbers, | ||
cfg.NumberMin, | ||
cfg.NumberMax, | ||
cfg.MinStringLength, | ||
cfg.MinOccurrences, | ||
) | ||
var issues []Issue | ||
for _, f := range files { | ||
ast.Walk(&treeVisitor{ | ||
fileSet: fset, | ||
packageName: "", | ||
fileName: "", | ||
p: p, | ||
}, f) | ||
} | ||
|
||
for str, item := range p.strs { | ||
fi := item[0] | ||
i := Issue{ | ||
Pos: fi.Position, | ||
OccurrencesCount: len(item), | ||
Str: str, | ||
} | ||
|
||
if len(p.consts) != 0 { | ||
if cst, ok := p.consts[str]; ok { | ||
// const should be in the same package and exported | ||
i.MatchingConst = cst.Name | ||
} | ||
} | ||
issues = append(issues, i) | ||
} | ||
|
||
return issues, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://golang.org/pkg/strconv/#Unquote
?