From 22c45d86747bea2e21cf4f38f90dc8fad5159abb Mon Sep 17 00:00:00 2001 From: mido Date: Wed, 11 Jan 2023 03:48:05 -0800 Subject: [PATCH 1/2] Fix panic when let statement is not followed by an equal sign --- parser/parser.go | 5 ++++- variables_test.go | 14 ++++++++++++++ 2 files changed, 18 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..c540974 100644 --- a/variables_test.go +++ b/variables_test.go @@ -28,6 +28,20 @@ func Test_Let_Reassignment(t *testing.T) { 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" %>` From 79cc8e14b86c08200a7ac4659019f2987c89cc60 Mon Sep 17 00:00:00 2001 From: mido Date: Wed, 11 Jan 2023 03:49:10 -0800 Subject: [PATCH 2/2] fix merge conflict --- parser/parser.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index d5ccc6b..0a8af5b 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -2,7 +2,6 @@ package parser import ( "fmt" - "reflect" "strconv" "strings" @@ -108,9 +107,7 @@ func (p *parser) parseProgram() *ast.Program { } } - if stmt != nil && - (reflect.ValueOf(stmt).Kind() == reflect.Ptr && !reflect.ValueOf(stmt).IsNil()) && - strings.TrimSpace(stmt.String()) != "" { + if stmt != nil && strings.TrimSpace(stmt.String()) != "" { program.Statements = append(program.Statements, stmt) } @@ -162,6 +159,9 @@ func (p *parser) parseStatement() ast.Statement { switch p.curToken.Type { case token.LET: l := p.parseLetStatement() + if l == nil { + return nil + } return l case token.S_START: p.nextToken() @@ -202,7 +202,7 @@ func (p *parser) parseLetStatement() *ast.LetStatement { stmt.Name = &ast.Identifier{TokenAble: ast.TokenAble{Token: p.curToken}, Value: p.curToken.Literal} if !p.expectPeek(token.ASSIGN) { - return nil + return stmt } p.nextToken()