stmt/if: collapse else if from else(if( into elseif(

This commit is contained in:
mappu 2020-04-10 11:23:37 +12:00
parent 72b8da5e4a
commit f1b7f301cc

25
node.go
View File

@ -739,6 +739,31 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error)
return "", parseErr{n, err}
}
//
// `elseif` is a valid PHP keyword; but `else if` gets treated as `else { if ()`
// If our discovered `else` clause has exactly one child If statement,
// rewrite it as an else-if clause (and recurse)
for n.Else != nil {
els, ok := n.Else.(*stmt.Else)
if !ok {
return "", parseErr{n, fmt.Errorf("expected stmt.Else")}
}
elif, ok := els.Stmt.(*stmt.If)
if !ok {
break // exit loop
}
n.AddElseIf(stmt.NewElseIf(elif.Cond, elif.Stmt))
for _, extraElif := range elif.ElseIf {
n.AddElseIf(extraElif)
}
n.Else = elif.Else // Wipe from future processing
}
//
ret := "if " + cond + body
for _, elif := range n.ElseIf {
elif, ok := elif.(*stmt.ElseIf)