Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkg/visitor/formatter/dangling_else_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package formatter_test

import (
"bytes"
"strings"
"testing"

"github.com/rectorphp/php-parser-in-go/pkg/ast"
"github.com/rectorphp/php-parser-in-go/pkg/visitor/formatter"
"github.com/rectorphp/php-parser-in-go/pkg/visitor/printer"
)

// An if whose body ends with an if without an else gets its body braced
// when it has an else or an elseif itself: printed without braces, the
// else would belong to the inner if.
func TestFormatter_BracesDanglingElse(test *testing.T) {
echo := func(value string) ast.Vertex {
return &ast.StmtEcho{Exprs: []ast.Vertex{&ast.ScalarLnumber{Value: []byte(value)}}}
}
variable := func(name string) ast.Vertex {
return &ast.ExprVariable{Name: &ast.Identifier{Value: []byte("$" + name)}}
}
inner := &ast.StmtIf{Cond: variable("a"), Stmt: echo("1")}
loop := &ast.StmtWhile{Cond: variable("b"), Stmt: &ast.StmtIf{Cond: variable("c"), Stmt: echo("2")}}
root := &ast.Root{Stmts: []ast.Vertex{
&ast.StmtIf{Cond: variable("p"), Stmt: inner, Else: &ast.StmtElse{Stmt: echo("3")}},
&ast.StmtIf{
Cond: variable("q"),
Stmt: echo("4"),
ElseIf: []ast.Vertex{&ast.StmtElseIf{Cond: variable("r"), Stmt: loop}},
Else: &ast.StmtElse{Stmt: echo("5")},
},
}}
root.Accept(formatter.NewFormatter())
var buffer bytes.Buffer
root.Accept(printer.NewPrinter(&buffer))

// Without the open tag, and with elseif spaced either way.
actual := strings.TrimLeft(strings.TrimPrefix(buffer.String(), "<?php"), " \n")
actual = strings.ReplaceAll(actual, "elseif (", "elseif(")
expected := "if ($p) {\n if ($a) echo 1;\n} else echo 3;\nif ($q) echo 4; elseif($r) {\n while($b) if ($c) echo 2;\n} else echo 5;"
if actual != expected {
test.Errorf("\nexpected: %q\nactual: %q\n", expected, actual)
}
}
47 changes: 47 additions & 0 deletions pkg/visitor/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,16 @@ func (formatter *formatter) StmtIf(node *ast.StmtIf) {
node.EndIfTkn = nil
node.SemiColonTkn = nil

// An else would bind into a body that ends with an if without one.
if len(node.ElseIf) > 0 || node.Else != nil {
node.Stmt = braceOpenIf(node.Stmt)
}
for i, elseIf := range node.ElseIf {
if elseIf, ok := elseIf.(*ast.StmtElseIf); ok && (i < len(node.ElseIf)-1 || node.Else != nil) {
elseIf.Stmt = braceOpenIf(elseIf.Stmt)
}
}

node.IfTkn = formatter.newToken(token.T_IF, []byte("if"))
formatter.addFreeFloating(token.T_WHITESPACE, []byte(" "))

Expand All @@ -709,6 +719,43 @@ func (formatter *formatter) StmtIf(node *ast.StmtIf) {
}
}

// braceOpenIf puts a body in braces where it ends with an if without an
// else, out of braces, which an else after it would belong to.
func braceOpenIf(body ast.Vertex) ast.Vertex {
if !opensElse(body) {
return body
}
return &ast.StmtStmtList{Stmts: []ast.Vertex{body}}
}

func opensElse(stmt ast.Vertex) bool {
switch n := stmt.(type) {
case *ast.StmtIf:
if n.Else != nil {
if elseStmt, ok := n.Else.(*ast.StmtElse); ok {
return opensElse(elseStmt.Stmt)
}
return false
}
if len(n.ElseIf) > 0 {
if elseIf, ok := n.ElseIf[len(n.ElseIf)-1].(*ast.StmtElseIf); ok {
return opensElse(elseIf.Stmt)
}
return false
}
return true
case *ast.StmtWhile:
return opensElse(n.Stmt)
case *ast.StmtFor:
return opensElse(n.Stmt)
case *ast.StmtForeach:
return opensElse(n.Stmt)
case *ast.StmtDeclare:
return opensElse(n.Stmt)
}
return false
}

func (formatter *formatter) StmtInlineHtml(node *ast.StmtInlineHtml) {
node.InlineHtmlTkn = formatter.newToken(token.T_STRING, node.Value)
formatter.state = FormatterStateHTML
Expand Down