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

Updated master #3

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Flags:
-min minimum value, only works with -numbers
-max maximum value, only works with -numbers
-output output formatting (text or json)
-set-exit-status Set exit status to 2 if any issues are found

Examples:

Expand Down
27 changes: 20 additions & 7 deletions cmd/goconst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Flags:
-min minimum value, only works with -numbers
-max maximum value, only works with -numbers
-output output formatting (text or json)
-set-exit-status Set exit status to 2 if any issues are found

Examples:

Expand All @@ -49,6 +50,7 @@ var (
flagMin = flag.Int("min", 0, "minimum value, only works with -numbers")
flagMax = flag.Int("max", 0, "maximum value, only works with -numbers")
flagOutput = flag.String("output", "text", "output formatting")
flagSetExitStatus = flag.Bool("set-exit-status", false, "Set exit status to 2 if any issues are found")
)

func main() {
Expand All @@ -63,15 +65,26 @@ func main() {
usage(os.Stderr)
os.Exit(1)
}

lintFailed := false
for _, path := range args {
if err := run(path); err != nil {
anyIssues, err := run(path)
if err != nil {
log.Println(err)
os.Exit(1)
}

if anyIssues {
lintFailed = true
}
}

if lintFailed && *flagSetExitStatus {
os.Exit(2)
}
}

func run(path string) error {
func run(path string) (bool, error) {
gco := goconst.New(
path,
*flagIgnore,
Expand All @@ -82,7 +95,7 @@ func run(path string) error {
)
strs, consts, err := gco.ParseTree()
if err != nil {
return err
return false, err
}

return printOutput(strs, consts, *flagOutput, *flagMinOccurrences, *flagMin, *flagMax)
Expand All @@ -92,7 +105,7 @@ func usage(out io.Writer) {
fmt.Fprintf(out, usageDoc)
}

func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences, min, max int) error {
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 {
Expand Down Expand Up @@ -120,7 +133,7 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
strs, consts,
})
if err != nil {
return err
return false, err
}
case "text":
for str, item := range strs {
Expand All @@ -147,9 +160,9 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
}
}
default:
return fmt.Errorf(`Unsupported output format: %s`, output)
return false, fmt.Errorf(`Unsupported output format: %s`, output)
}
return nil
return len(strs) + len(consts) > 0, nil
}

func occurrences(item []goconst.ExtendedPos, current goconst.ExtendedPos) string {
Expand Down
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
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