diff --git a/builder/visitor.go b/builder/visitor.go index c813557..ea24bde 100644 --- a/builder/visitor.go +++ b/builder/visitor.go @@ -192,6 +192,16 @@ func (v *visitor) evalSelector(expr *ast.CallExpr, sel *ast.SelectorExpr) error for _, e := range expr.Args { switch at := e.(type) { case *ast.Ident: + switch at.Obj.Kind { + case ast.Var: + if as, ok := at.Obj.Decl.(*ast.AssignStmt); ok { + v.addVariable(as) + } + case ast.Con: + if vs, ok := at.Obj.Decl.(*ast.ValueSpec); ok { + v.addConstant(vs) + } + } return v.evalIdent(at) case *ast.BasicLit: v.addBox(at.Value) @@ -218,3 +228,21 @@ func (v *visitor) addBox(b string) { b = strings.Replace(b, "\"", "", -1) v.Boxes = append(v.Boxes, b) } + +func (v *visitor) addVariable(as *ast.AssignStmt) error { + if len(as.Rhs) == 1 { + if bs, ok := as.Rhs[0].(*ast.BasicLit); ok { + v.addBox(bs.Value) + } + } + return nil +} + +func (v *visitor) addConstant(vs *ast.ValueSpec) error { + if len(vs.Values) == 1 { + if bs, ok := vs.Values[0].(*ast.BasicLit); ok { + v.addBox(bs.Value) + } + } + return nil +} diff --git a/builder/visitor_test.go b/builder/visitor_test.go index d9c27c1..1be3d78 100644 --- a/builder/visitor_test.go +++ b/builder/visitor_test.go @@ -13,6 +13,6 @@ func Test_Visitor(t *testing.T) { r.Equal("example", v.Package) r.Len(v.Errors, 0) - r.Len(v.Boxes, 5) - r.Equal([]string{"./assets", "./bar", "./foo", "./sf", "./templates"}, v.Boxes) + r.Len(v.Boxes, 7) + r.Equal([]string{"./assets", "./bar", "./constant", "./foo", "./sf", "./templates", "./variable"}, v.Boxes) } diff --git a/example/constant/constant.html b/example/constant/constant.html new file mode 100644 index 0000000..e69de29 diff --git a/example/example.go b/example/example.go index 243111b..835f494 100644 --- a/example/example.go +++ b/example/example.go @@ -6,6 +6,8 @@ import ( var a = packr.NewBox("./foo") +const constString = "./constant" + type S struct{} func (S) f(packr.Box) {} @@ -13,8 +15,17 @@ func (S) f(packr.Box) {} func init() { // packr.NewBox("../idontexists") - b := "./baz" - packr.NewBox(b) // won't work, no variables allowed, only strings + b := "./variable" + packr.NewBox(b) + + packr.NewBox(constString) + + // Cannot work from a function + packr.NewBox(strFromFunc()) + + // This variable should not be added + fromFunc := strFromFunc() + packr.NewBox(fromFunc) foo("/templates", packr.NewBox("./templates")) packr.NewBox("./assets") @@ -25,4 +36,8 @@ func init() { s.f(packr.NewBox("./sf")) } +func strFromFunc() string { + return "./fromFunc" +} + func foo(s string, box packr.Box) {} diff --git a/example/variable/variable.html b/example/variable/variable.html new file mode 100644 index 0000000..e69de29