Skip to content

Commit

Permalink
Merge pull request #171 from Mido-sys/fix_non_initialized_variables
Browse files Browse the repository at this point in the history
Fix uninitialized variables
  • Loading branch information
paganotoni authored Dec 26, 2022
2 parents 1adebc8 + 4b2d867 commit 01712da
Show file tree
Hide file tree
Showing 2 changed files with 16 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
12 changes: 12 additions & 0 deletions variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ 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()

_, 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 01712da

Please sign in to comment.