Skip to content

Commit

Permalink
Rebuilding not-so-internal structure of goconst to be able to tell on…
Browse files Browse the repository at this point in the history
…e violation type from another
  • Loading branch information
iwankgb committed Nov 15, 2020
1 parent 4c8fa75 commit 46a8881
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 135 deletions.
24 changes: 8 additions & 16 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Issue struct {
OccurrencesCount int
Str string
MatchingConst string
Typ string
Typ Type
}

type Config struct {
Expand Down Expand Up @@ -51,31 +51,23 @@ func Run(files []*ast.File, fset *token.FileSet, cfg *Config) ([]Issue, error) {
}

func buildIssues(p *Parser, issues []Issue) []Issue {
for str, item := range p.strs {
violations := map[string]*Issue{}
for _, violation := range item {
issue, ok := violations[violation.typ]
if ok {
issue.OccurrencesCount++
continue
}
//return nil
for str, results := range p.strs {
for typ, violations := range results.Violations {
violation := violations[0]
i := Issue{
Pos: violation.Position,
OccurrencesCount: 1,
OccurrencesCount: len(violations),
Str: str,
Typ: violation.typ,
Typ: typ,
}

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
}
}
violations[violation.typ] = &i
}
for _, v := range violations {
issues = append(issues, *v)
issues = append(issues, i)
}
}
return issues
Expand Down
182 changes: 103 additions & 79 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,36 @@ func TestBuildIssues(t *testing.T) {
}{
{
p: &Parser{
strs: map[string][]ExtendedPos{
strs: map[string]*Violations{
"foobar": {
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 2,
Column: 3,
3,
map[Type][]ExtendedPos{
Assignment: {
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 2,
Column: 3,
},
},
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 3,
Column: 3,
},
},
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 4,
Column: 3,
},
},
},
typ: Assignment,
},
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 3,
Column: 3,
},
typ: Assignment,
},
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 4,
Column: 3,
},
typ: Assignment,
},
},
},
Expand All @@ -67,25 +69,30 @@ func TestBuildIssues(t *testing.T) {
},
{
p: &Parser{
strs: map[string][]ExtendedPos{
strs: map[string]*Violations{
"foobar": {
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 2,
Column: 3,
2,
map[Type][]ExtendedPos{
Assignment: {
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 2,
Column: 3,
},
},
},
typ: Assignment,
},
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 4,
Column: 3,
Call: {
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 4,
Column: 3,
},
},
},
typ: Call,
},
},
},
Expand All @@ -104,7 +111,6 @@ func TestBuildIssues(t *testing.T) {
OccurrencesCount: 1,
Str: "foobar",
MatchingConst: "barfoo",
Typ: Assignment,
},
{
Pos: token.Position{
Expand All @@ -116,52 +122,59 @@ func TestBuildIssues(t *testing.T) {
OccurrencesCount: 1,
Str: "foobar",
MatchingConst: "barfoo",
Typ: Call,
},
},
name: "various_types",
},
{
p: &Parser{
strs: map[string][]ExtendedPos{
strs: map[string]*Violations{
"foobar": {
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 2,
Column: 3,
2,
map[Type][]ExtendedPos{
Assignment: {
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 2,
Column: 3,
},
},
},
typ: Assignment,
},
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 4,
Column: 3,
Call: {
{
Position: token.Position{
Filename: "foo.go",
Offset: 1,
Line: 4,
Column: 3,
},
},
},
typ: Call,
},
},
"barbaz": {
{
Position: token.Position{
Filename: "bar.go",
Offset: 1,
Line: 2,
Column: 3,
2,
map[Type][]ExtendedPos{
Case: {
{
Position: token.Position{
Filename: "bar.go",
Offset: 1,
Line: 2,
Column: 3,
},
},
{
Position: token.Position{
Filename: "bar.go",
Offset: 1,
Line: 4,
Column: 3,
},
},
},
typ: Case,
},
{
Position: token.Position{
Filename: "bar.go",
Offset: 1,
Line: 4,
Column: 3,
},
typ: Case,
},
},
},
Expand Down Expand Up @@ -214,9 +227,20 @@ func TestBuildIssues(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
issues := buildIssues(testCase.p, []Issue{})
equal := reflect.DeepEqual(issues, testCase.i)
if !equal {
t.Errorf("got:\n%#v\nexpected:\n%#v", issues, testCase.i)
//equal := reflect.DeepEqual(issues, testCase.i)
if len(issues) != len(testCase.i) {
t.Errorf("length of received:\n%d, expected:\n%d", len(issues), len(testCase.i))
}
for issue := range issues {
var found bool
for expectedIssue := range testCase.i {
if reflect.DeepEqual(issue, expectedIssue) {
found = true
}
}
if !found {
t.Errorf("%#v not founf in %#v", issue, testCase.i)
}
}
})
}
Expand Down
60 changes: 32 additions & 28 deletions cmd/goconst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,29 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string)
return false, err
}
case "text":
for str, item := range strs {
for _, xpos := range item {
fmt.Printf(
`%s:%d:%d:%d other occurrence(s) of "%s" found in: %s`,
xpos.Filename,
xpos.Line,
xpos.Column,
len(item)-1,
str,
occurrences(item, xpos),
)
fmt.Print("\n")
}

if len(consts) == 0 {
continue
}
if cst, ok := consts[str]; ok {
// const should be in the same package and exported
fmt.Printf(`A matching constant has been found for "%s": %s`, str, cst.Name)
fmt.Printf("\n\t%s\n", cst.String())
for str, violations := range strs {
for _, positions := range violations.Violations {
for _, position := range positions {
fmt.Printf(
`%s:%d:%d:%d other occurrence(s) of "%s" found in: %s`,
position.Filename,
position.Line,
position.Column,
violations.Total-1,
str,
occurrences(violations, position),
)
fmt.Print("\n")
}

if len(consts) == 0 {
continue
}
if cst, ok := consts[str]; ok {
// const should be in the same package and exported
fmt.Printf(`A matching constant has been found for "%s": %s`, str, cst.Name)
fmt.Printf("\n\t%s\n", cst.String())
}
}
}
default:
Expand All @@ -150,15 +152,17 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string)
return len(strs)+len(consts) > 0, nil
}

func occurrences(item []goconst.ExtendedPos, current goconst.ExtendedPos) string {
func occurrences(item *goconst.Violations, current goconst.ExtendedPos) string {
occurrences := []string{}
for _, xpos := range item {
if xpos == current {
continue
for _, violations := range item.Violations {
for _, xpos := range violations {
if xpos == current {
continue
}
occurrences = append(occurrences, fmt.Sprintf(
"%s:%d:%d", xpos.Filename, xpos.Line, xpos.Column,
))
}
occurrences = append(occurrences, fmt.Sprintf(
"%s:%d:%d", xpos.Filename, xpos.Line, xpos.Column,
))
}
return strings.Join(occurrences, " ")
}

0 comments on commit 46a8881

Please sign in to comment.