diff --git a/parser/parser.go b/parser/parser.go index 3a1f8e0..0a8af5b 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -159,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() @@ -199,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() 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" %>`