diff --git a/ast/let_statement.go b/ast/let_statement.go index 53f1d9c..6202fbc 100644 --- a/ast/let_statement.go +++ b/ast/let_statement.go @@ -18,7 +18,9 @@ func (ls *LetStatement) String() string { var out bytes.Buffer out.WriteString(ls.TokenLiteral() + " ") - out.WriteString(ls.Name.String()) + if ls.Name != nil { + out.WriteString(ls.Name.String()) + } out.WriteString(" = ") if ls.Value != nil { diff --git a/go.mod b/go.mod index dac5c25..3c206de 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,6 @@ require ( github.com/gobuffalo/github_flavored_markdown v1.1.3 github.com/gobuffalo/helpers v0.6.7 github.com/gobuffalo/tags/v3 v3.1.4 - github.com/stretchr/testify v1.8.0 - golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 + github.com/stretchr/testify v1.8.1 + golang.org/x/sync v0.1.0 ) diff --git a/go.sum b/go.sum index 33b3713..d9ab157 100644 --- a/go.sum +++ b/go.sum @@ -36,15 +36,17 @@ github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e h1:qpG github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.0.0-20221002022538-bcab6841153b h1:6e93nYa3hNqAvLr0pD4PN1fFS+gKzp2zAXqrnTCstqU= golang.org/x/net v0.0.0-20221002022538-bcab6841153b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= -golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 h1:cu5kTvlzcw1Q5S9f5ip1/cpiB4nXvw1XYzFPGgzLUOY= -golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/parser/parser.go b/parser/parser.go index 0a8af5b..066b5aa 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -156,13 +156,14 @@ func (p *parser) noPrefixParseFnError(t token.Type) { } func (p *parser) parseStatement() ast.Statement { + // Warning: ast.Statement is an interface so please make sure all callee + // methods do not return nil from themselves. + // If you are adding another case here, please make sure the callee does + // not return nil or you should add nil checking and explicitly return + // concrete nil. (https://github.com/gobuffalo/plush/pull/171) switch p.curToken.Type { case token.LET: - l := p.parseLetStatement() - if l == nil { - return nil - } - return l + return p.parseLetStatement() case token.S_START: p.nextToken() return p.parseStatement() @@ -196,7 +197,7 @@ func (p *parser) parseLetStatement() *ast.LetStatement { stmt := &ast.LetStatement{TokenAble: ast.TokenAble{Token: p.curToken}} if !p.expectPeek(token.IDENT) { - return nil + return stmt } stmt.Name = &ast.Identifier{TokenAble: ast.TokenAble{Token: p.curToken}, Value: p.curToken.Literal} diff --git a/variables_test.go b/variables_test.go index c540974..8eff281 100644 --- a/variables_test.go +++ b/variables_test.go @@ -28,18 +28,24 @@ func Test_Let_Reassignment(t *testing.T) { r.Equal("bar\n \n \nbaz", strings.TrimSpace(s)) } -func Test_Let_Ident_NotInitialized(t *testing.T) { +func Test_Let_SyntaxError_NoEqualSign(t *testing.T) { r := require.New(t) - input := `<% let foo - if (foo){ - foo = 1 - } - %>` + input := `<% let foo %>` ctx := NewContext() _, err := Render(input, ctx) - r.Error(err) + r.ErrorContains(err, "expected next token to be =") +} + +func Test_Let_SyntaxError_NoIdentifier(t *testing.T) { + r := require.New(t) + input := `<% let = %>` + + ctx := NewContext() + + _, err := Render(input, ctx) + r.ErrorContains(err, "expected next token to be IDENT") } func Test_Let_Reassignment_UnknownIdent(t *testing.T) { @@ -50,7 +56,7 @@ func Test_Let_Reassignment_UnknownIdent(t *testing.T) { ctx.Set("myArray", []string{"a", "b"}) _, err := Render(input, ctx) - r.Error(err) + r.ErrorContains(err, "\"foo\": unknown identifier") } func Test_Let_Inside_Helper(t *testing.T) {