Skip to content

Commit

Permalink
arg... stupid unknown identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Nov 3, 2017
1 parent a70a4ff commit 1b9a822
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
7 changes: 6 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/pkg/errors"
)

var ErrUnknownIdentifier = errors.New("unknown identifier")

type compiler struct {
ctx *Context
program *ast.Program
Expand Down Expand Up @@ -143,6 +145,9 @@ func (c *compiler) evalIfExpression(node *ast.IfExpression) (interface{}, error)
// fmt.Println("evalIfExpression")
con, err := c.evalExpression(node.Condition)
if err != nil {
if errors.Cause(err) == ErrUnknownIdentifier {
return nil, nil
}
return nil, errors.WithStack(err)
}

Expand Down Expand Up @@ -248,7 +253,7 @@ func (c *compiler) evalIdentifier(node *ast.Identifier) (interface{}, error) {
if node.Value == "nil" {
return nil, nil
}
return nil, errors.Errorf("could not find identifier '%s'", node.Value)
return nil, errors.Wrap(ErrUnknownIdentifier, node.Value)
}

func (c *compiler) evalInfixExpression(node *ast.InfixExpression) (interface{}, error) {
Expand Down
5 changes: 3 additions & 2 deletions if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func Test_Render_If_Or(t *testing.T) {
func Test_Render_If_Nil(t *testing.T) {
r := require.New(t)
input := `<%= if (names && len(names) >= 1) { %>hi<%} %>`
_, err := Render(input, NewContext())
r.Error(err)
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
}

func Test_Render_If_Else_Return(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion plush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func Test_UndefinedArg(t *testing.T) {

_, err := Render(input, ctx)
r.Error(err)
r.Contains(err.Error(), "could not find identifier 'bar'")
r.Equal(ErrUnknownIdentifier, errors.Cause(err))
}

func Test_RunScript(t *testing.T) {
Expand Down

0 comments on commit 1b9a822

Please sign in to comment.