Skip to content

Commit cda7ea3

Browse files
committed
Support function params, ignore backquotes
1 parent 9740945 commit cda7ea3

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/jgautheron/goconst
2+
3+
go 1.13

tests/foo_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func foo() {
6+
foo := "test"
7+
boo := "test"
8+
fmt.Println(foo, boo)
9+
}

tests/main.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
const Foo = "bar"
9+
10+
var url string
11+
12+
func main() {
13+
if strings.HasPrefix(url, "http://") {
14+
url = strings.TrimPrefix(url, "http://")
15+
}
16+
url = strings.TrimPrefix(url, "/")
17+
fmt.Println(url)
18+
}
19+
20+
func testCase() string {
21+
test := `test`
22+
if url == "test" {
23+
return test
24+
}
25+
switch url {
26+
case "moo":
27+
return ""
28+
}
29+
return "foo"
30+
}

visitor.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,24 @@ func (v *treeVisitor) Visit(node ast.Node) ast.Visitor {
9595
v.addString(lit.Value, lit.Pos())
9696
}
9797
}
98+
99+
// fn("http://")
100+
case *ast.CallExpr:
101+
for _, item := range t.Args {
102+
lit, ok := item.(*ast.BasicLit)
103+
if ok && v.isSupported(lit.Kind) {
104+
v.addString(lit.Value, lit.Pos())
105+
}
106+
}
98107
}
99108

100109
return v
101110
}
102111

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

107117
// Ignore empty strings
108118
if len(str) == 0 {

0 commit comments

Comments
 (0)