Skip to content

Commit

Permalink
Expose API
Browse files Browse the repository at this point in the history
  • Loading branch information
jgautheron committed Nov 8, 2020
1 parent 8f5268c commit ce0d7c0
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 28 deletions.
64 changes: 64 additions & 0 deletions api.go
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
}
27 changes: 6 additions & 21 deletions cmd/goconst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"log"
"os"
"strconv"
"strings"

"github.com/jgautheron/goconst"
Expand Down Expand Up @@ -91,38 +90,24 @@ func run(path string) (bool, error) {
*flagIgnoreTests,
*flagMatchConstant,
*flagNumbers,
*flagMin,
*flagMax,
*flagMinLength,
*flagMinOccurrences,
)
strs, consts, err := gco.ParseTree()
if err != nil {
return false, err
}

return printOutput(strs, consts, *flagOutput, *flagMinOccurrences, *flagMin, *flagMax)
return printOutput(strs, consts, *flagOutput)
}

func usage(out io.Writer) {
fmt.Fprintf(out, usageDoc)
}

func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences, min, max int) (bool, error) {
for str, item := range strs {
// Filter out items whose occurrences don't match the min value
if len(item) < minOccurrences {
delete(strs, str)
}

// If the value is a number
if i, err := strconv.Atoi(str); err == nil {
if min != 0 && i < min {
delete(strs, str)
}
if max != 0 && i > max {
delete(strs, str)
}
}
}

func printOutput(strs goconst.Strings, consts goconst.Constants, output string) (bool, error) {
switch output {
case "json":
enc := json.NewEncoder(os.Stdout)
Expand Down Expand Up @@ -162,7 +147,7 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
default:
return false, fmt.Errorf(`Unsupported output format: %s`, output)
}
return len(strs) + len(consts) > 0, nil
return len(strs)+len(consts) > 0, nil
}

func occurrences(item []goconst.ExtendedPos, current goconst.ExtendedPos) string {
Expand Down
32 changes: 30 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)

Expand All @@ -25,7 +26,8 @@ type Parser struct {
// Meant to be passed via New()
path, ignore string
ignoreTests, matchConstant bool
minLength int
minLength, minOccurrences int
numberMin, numberMax int

supportedTokens []token.Token

Expand All @@ -36,7 +38,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, minLength int) *Parser {
func New(path, ignore string, ignoreTests, matchConstant, numbers bool, numberMin, numberMax, minLength, minOccurrences int) *Parser {
supportedTokens := []token.Token{token.STRING}
if numbers {
supportedTokens = append(supportedTokens, token.INT, token.FLOAT)
Expand All @@ -48,6 +50,9 @@ func New(path, ignore string, ignoreTests, matchConstant, numbers bool, minLengt
ignoreTests: ignoreTests,
matchConstant: matchConstant,
minLength: minLength,
minOccurrences: minOccurrences,
numberMin: numberMin,
numberMax: numberMax,
supportedTokens: supportedTokens,

// Initialize the maps
Expand Down Expand Up @@ -77,9 +82,32 @@ func (p *Parser) ParseTree() (Strings, Constants, error) {
} else {
p.parseDir(p.path)
}

p.ProcessResults()

return p.strs, p.consts, nil
}

// ProcessResults post-processes the raw results.
func (p *Parser) ProcessResults() {
for str, item := range p.strs {
// Filter out items whose occurrences don't match the min value
if len(item) < p.minOccurrences {
delete(p.strs, str)
}

// If the value is a number
if i, err := strconv.Atoi(str); err == nil {
if p.numberMin != 0 && i < p.numberMin {
delete(p.strs, str)
}
if p.numberMax != 0 && i > p.numberMax {
delete(p.strs, str)
}
}
}
}

func (p *Parser) parseDir(dir string) error {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, dir, func(info os.FileInfo) bool {
Expand Down
10 changes: 7 additions & 3 deletions tests/foo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package main
import "fmt"

func foo() {
foo := "test"
boo := "test"
fmt.Println(foo, boo)
testString1 := "test"
testString2 := "test"

testInt1 := 123
testInt2 := 123

fmt.Println(testString1, testString2, testInt1, testInt2)
}
10 changes: 10 additions & 0 deletions tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

const Foo = "bar"
const NumberConst = 123

var url string

Expand All @@ -28,3 +29,12 @@ func testCase() string {
}
return "foo"
}

func testInt() int {
test := 123
if test == 123 {
return 123
}

return 123
}
9 changes: 7 additions & 2 deletions visitor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goconst

import (
"fmt"
"go/ast"
"go/token"
"strings"
Expand Down Expand Up @@ -111,8 +112,12 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {

// addString adds a string in the map along with its position in the tree.
func (v *treeVisitor) addString(str string, pos token.Pos) {
// Drop first and last character, quote, backquote...
str = str[1 : len(str)-1]
if strings.HasPrefix(str, `"`) || strings.HasPrefix(str, "`") {
// Drop first and last character, quote, backquote...
str = str[1 : len(str)-1]

This comment has been minimized.

}

fmt.Println(str)

This comment has been minimized.

Copy link
@Antonboom

Antonboom Nov 8, 2020

@jgautheron, is it debug print?


// Ignore empty strings
if len(str) == 0 {
Expand Down

0 comments on commit ce0d7c0

Please sign in to comment.