From 3a9de59088b917f81eef5f70a97069e036c437d5 Mon Sep 17 00:00:00 2001 From: Daniel Oliveira Date: Wed, 18 Nov 2026 14:27:24 +0100 Subject: [PATCH] Brace an if body that an else would otherwise bind into in the formatter A tree built by hand can hold an if whose body ends with an if without an else. Printed without braces, the outer else then belongs to the inner if. --- pkg/visitor/formatter/dangling_else_test.go | 45 ++++++++++++++++++++ pkg/visitor/formatter/formatter.go | 47 +++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 pkg/visitor/formatter/dangling_else_test.go diff --git a/pkg/visitor/formatter/dangling_else_test.go b/pkg/visitor/formatter/dangling_else_test.go new file mode 100644 index 0000000..aed30ee --- /dev/null +++ b/pkg/visitor/formatter/dangling_else_test.go @@ -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(), " 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(" ")) @@ -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