Skip to content

Commit

Permalink
Fix uninitialized variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Mido-sys committed Dec 14, 2022
1 parent 1adebc8 commit 7cc0884
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"fmt"
"reflect"
"strconv"
"strings"

Expand Down Expand Up @@ -107,7 +108,9 @@ func (p *parser) parseProgram() *ast.Program {
}
}

if stmt != nil && strings.TrimSpace(stmt.String()) != "" {
if stmt != nil &&
(reflect.ValueOf(stmt).Kind() == reflect.Ptr && !reflect.ValueOf(stmt).IsNil()) &&
strings.TrimSpace(stmt.String()) != "" {
program.Statements = append(program.Statements, stmt)
}

Expand Down
13 changes: 13 additions & 0 deletions variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,20 @@ func Test_Let_Reassignment(t *testing.T) {
r.NoError(err)
r.Equal("bar\n \n \nbaz", strings.TrimSpace(s))
}
func Test_Let_Ident_NotInitialized(t *testing.T) {
r := require.New(t)
input := `<% let foo
if (foo){
foo = 1
}
%>`

ctx := NewContext()
//ctx.Set("myArray", []string{"a", "b"})

_, err := Render(input, ctx)
r.Error(err)
}
func Test_Let_Reassignment_UnknownIdent(t *testing.T) {
r := require.New(t)
input := `<% foo = "baz" %>`
Expand Down

0 comments on commit 7cc0884

Please sign in to comment.