From 7cc0884a3edb6fd2d731a39b1b0d0fd32d970adc Mon Sep 17 00:00:00 2001 From: mido Date: Wed, 14 Dec 2022 01:18:49 -0800 Subject: [PATCH 1/2] Fix uninitialized variables --- parser/parser.go | 5 ++++- variables_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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..a2694b2 100644 --- a/variables_test.go +++ b/variables_test.go @@ -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" %>` From 4b2d8677ebab2a36b1dbb8aa26066b52af8e2cc3 Mon Sep 17 00:00:00 2001 From: mido Date: Wed, 14 Dec 2022 01:36:08 -0800 Subject: [PATCH 2/2] code cleanup --- variables_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/variables_test.go b/variables_test.go index a2694b2..55d0b02 100644 --- a/variables_test.go +++ b/variables_test.go @@ -36,7 +36,6 @@ func Test_Let_Ident_NotInitialized(t *testing.T) { %>` ctx := NewContext() - //ctx.Set("myArray", []string{"a", "b"}) _, err := Render(input, ctx) r.Error(err)