Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Rebase with new feature #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/jgautheron/goconst

go 1.13
52 changes: 52 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,55 @@ type ExtendedPos struct {
token.Position
packageName string
}

type Issue struct {
Pos token.Position
OccurencesCount int
Str string
MatchingConst string
}

type Config struct {
MatchWithConstants bool
MinStringLength int
MinOccurrences int
}

func Run(files []*ast.File, fset *token.FileSet, cfg *Config) ([]Issue, error) {
p := New("", "", false, cfg.MatchWithConstants, false, cfg.MinStringLength)
var issues []Issue
for _, f := range files {
ast.Walk(&treeVisitor{
fileSet: fset,
packageName: "",
fileName: "",
p: p,
}, f)
}

for str, item := range p.strs {
// Filter out items whose occurrences don't match the min value
if len(item) < cfg.MinOccurrences {
delete(p.strs, str)
}
}

for str, item := range p.strs {
fi := item[0]
i := Issue{
Pos: fi.Position,
OccurencesCount: 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
}
9 changes: 9 additions & 0 deletions tests/foo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "fmt"

func foo() {
foo := "test"
boo := "test"
fmt.Println(foo, boo)
}
30 changes: 30 additions & 0 deletions tests/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"strings"
)

const Foo = "bar"

var url string

func main() {
if strings.HasPrefix(url, "http://") {
url = strings.TrimPrefix(url, "http://")
}
url = strings.TrimPrefix(url, "/")
fmt.Println(url)
}

func testCase() string {
test := `test`
if url == "test" {
return test
}
switch url {
case "moo":
return ""
}
return "foo"
}
12 changes: 11 additions & 1 deletion visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,24 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
v.addString(lit.Value, lit.Pos())
}
}

// fn("http://")
case *ast.CallExpr:
for _, item := range t.Args {
lit, ok := item.(*ast.BasicLit)
if ok && v.isSupported(lit.Kind) {
v.addString(lit.Value, lit.Pos())
}
}
}

return v
}

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

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