From 6aa89872defb4a9a2f5af00e80404d98bfea696b Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Mon, 15 Jul 2019 20:17:50 +0200 Subject: [PATCH] Fix func call line breaks in String() (#98) Fixes https://github.com/gobuffalo/pop/issues/410 --- ast/call_expression.go | 3 --- ast/program.go | 21 +++++++++++++++++---- parser/parser_test.go | 5 +++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/ast/call_expression.go b/ast/call_expression.go index 34061e0..30a1ab3 100644 --- a/ast/call_expression.go +++ b/ast/call_expression.go @@ -38,9 +38,6 @@ func (ce *CallExpression) String() string { out.WriteString(ce.ElseBlock.String()) out.WriteString(" }") } - if ce.Block != nil || ce.ElseBlock != nil { - out.WriteString("\n") - } return out.String() } diff --git a/ast/program.go b/ast/program.go index 0e6f738..0b339ff 100644 --- a/ast/program.go +++ b/ast/program.go @@ -1,6 +1,9 @@ package ast -import "bytes" +import ( + "bytes" + "strings" +) type Program struct { Statements []Statement @@ -9,12 +12,12 @@ type Program struct { func (p *Program) TokenLiteral() string { if len(p.Statements) > 0 { return p.Statements[0].TokenLiteral() - } else { - return "" } + return "" } -func (p *Program) String() string { +// InnerText gets the raw string representation of the program's contents. +func (p *Program) InnerText() string { var out bytes.Buffer for _, s := range p.Statements { @@ -23,3 +26,13 @@ func (p *Program) String() string { return out.String() } + +func (p *Program) String() string { + var out bytes.Buffer + + for _, s := range p.Statements { + out.WriteString(s.String() + "\n") + } + + return strings.TrimRight(out.String(), "\n") +} diff --git a/parser/parser_test.go b/parser/parser_test.go index be648aa..a12a663 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -307,7 +307,7 @@ func Test_OperatorPrecedence(t *testing.T) { program, err := Parse("<%" + tt.input + "%>") r.NoError(err) - r.Equal(tt.expected, program.String()) + r.Equal(tt.expected, program.InnerText()) } } @@ -1002,7 +1002,8 @@ create_table("users_2", {"timestamps": false}) { t.Column("price", "numeric", {"null": true, "default": "1.00"}) t.Column("email", "string", {"default": "foo@example.com", "size": 50}) } -` +drop_column("table_name", "column1_name") +drop_column("table_name", "column2_name")` p, err := Parse("<%" + input + "%>") r.NoError(err) r.Equal(input, p.String())