Skip to content

Commit

Permalink
evalPrefixExpression now handles unknown identifiers (#44)
Browse files Browse the repository at this point in the history
Previously evalIfExpression was updated to handle unknown identifiers,
so the following would go down the else case.

if (unknownIdentifier) {
} else {
}

evalPrefixExpression did not not handle unknown identifiers the same
way, so the following would also go down the else case.

if (!unknownIdentifier) {
} else {
}

evalPrefixExpression will now properly return !isTruthy.
  • Loading branch information
Scott Brooks authored and markbates committed Mar 15, 2018
1 parent 14e8d79 commit 15b6b36
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ func (c *compiler) evalFunctionLiteral(node *ast.FunctionLiteral) (interface{},
func (c *compiler) evalPrefixExpression(node *ast.PrefixExpression) (interface{}, error) {
res, err := c.evalExpression(node.Right)
if err != nil {
return nil, errors.WithStack(err)
if errors.Cause(err) != ErrUnknownIdentifier {
return nil, errors.WithStack(err)
}
}
switch node.Operator {
case "!":
Expand Down
8 changes: 8 additions & 0 deletions if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func Test_Render_If_Nil(t *testing.T) {
r.Equal("", s)
}

func Test_Render_If_NotNil(t *testing.T) {
r := require.New(t)
input := `<%= if (!names) { %>hi<%} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_Nil_Else(t *testing.T) {
r := require.New(t)
input := `<%= if (names && len(names) >= 1) { %>hi<%} else { %>something else<% } %>`
Expand Down

0 comments on commit 15b6b36

Please sign in to comment.