diff --git a/parser/parser.go b/parser/parser.go index 3a1f8e0..d5ccc6b 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -2,6 +2,7 @@ package parser import ( "fmt" + "reflect" "strconv" "strings" @@ -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) } diff --git a/variables_test.go b/variables_test.go index a3d2c89..55d0b02 100644 --- a/variables_test.go +++ b/variables_test.go @@ -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" %>`