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/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) {