Skip to content

Commit

Permalink
cleanup recent code change and clarified the name of test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Jan 11, 2023
1 parent 0e1753a commit 8b62f8c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
4 changes: 3 additions & 1 deletion ast/let_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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}
Expand Down
22 changes: 14 additions & 8 deletions variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 8b62f8c

Please sign in to comment.