Skip to content

Commit

Permalink
Merge pull request #175 from gobuffalo/clean-up
Browse files Browse the repository at this point in the history
code cleanup, added comments, and renamed/added test cases
  • Loading branch information
sio4 authored Jan 11, 2023
2 parents 0e1753a + 5071b45 commit a2fcc6c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
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 a2fcc6c

Please sign in to comment.