[#82] Arrow function and assign coalesce

This commit is contained in:
z7zmey
2019-12-28 22:26:54 +02:00
parent 0e55cb3b25
commit 7b4c72a3af
17 changed files with 8117 additions and 7467 deletions

View File

@@ -135,6 +135,8 @@ func (p *Printer) printNode(n node.Node) {
p.printAssignBitwiseOr(n)
case *assign.BitwiseXor:
p.printAssignBitwiseXor(n)
case *assign.Coalesce:
p.printAssignCoalesce(n)
case *assign.Concat:
p.printAssignConcat(n)
case *assign.Div:
@@ -236,6 +238,8 @@ func (p *Printer) printNode(n node.Node) {
p.printExprArrayItem(n)
case *expr.Array:
p.printExprArray(n)
case *expr.ArrowFunction:
p.printExprArrowFunction(n)
case *expr.BitwiseNot:
p.printExprBitwiseNot(n)
case *expr.BooleanNot:
@@ -712,6 +716,17 @@ func (p *Printer) printAssignBitwiseXor(n node.Node) {
p.printFreeFloating(nn, freefloating.End)
}
func (p *Printer) printAssignCoalesce(n node.Node) {
nn := n.(*assign.Coalesce)
p.printFreeFloating(nn, freefloating.Start)
p.Print(nn.Variable)
p.printFreeFloating(nn, freefloating.Var)
io.WriteString(p.w, "??=")
p.Print(nn.Expression)
p.printFreeFloating(nn, freefloating.End)
}
func (p *Printer) printAssignConcat(n node.Node) {
nn := n.(*assign.Concat)
p.printFreeFloating(nn, freefloating.Start)
@@ -1313,6 +1328,45 @@ func (p *Printer) printExprArray(n node.Node) {
p.printFreeFloating(nn, freefloating.End)
}
func (p *Printer) printExprArrowFunction(n node.Node) {
nn := n.(*expr.ArrowFunction)
p.printFreeFloating(nn, freefloating.Start)
if nn.Static {
io.WriteString(p.w, "static")
}
p.printFreeFloating(nn, freefloating.Static)
if nn.Static && n.GetFreeFloating().IsEmpty() {
io.WriteString(p.w, " ")
}
io.WriteString(p.w, "fn")
p.printFreeFloating(nn, freefloating.Function)
if nn.ReturnsRef {
io.WriteString(p.w, "&")
}
p.printFreeFloating(nn, freefloating.Ampersand)
io.WriteString(p.w, "(")
p.joinPrint(",", nn.Params)
p.printFreeFloating(nn, freefloating.ParameterList)
io.WriteString(p.w, ")")
p.printFreeFloating(nn, freefloating.Params)
if nn.ReturnType != nil {
io.WriteString(p.w, ":")
p.Print(nn.ReturnType)
}
p.printFreeFloating(nn, freefloating.ReturnType)
io.WriteString(p.w, "=>")
p.printNode(nn.Expr)
p.printFreeFloating(nn, freefloating.End)
}
func (p *Printer) printExprBitwiseNot(n node.Node) {
nn := n.(*expr.BitwiseNot)
p.printFreeFloating(nn, freefloating.Start)

View File

@@ -270,6 +270,7 @@ func TestParseAndPrintAssign(t *testing.T) {
$a &= $b ;
$a |= $b ;
$a ^= $b ;
$a ??= $b ;
$a .= $b ;
$a /= $b ;
$a -= $b ;
@@ -482,6 +483,18 @@ func TestParseAndPrintClosure(t *testing.T) {
}
}
func TestParseAndPrintArrowFunction(t *testing.T) {
src := `<?php
$a = static fn & ( $b ) : void => $c ;
`
actual := print(parse(src))
if src != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
}
}
func TestParseAndPrintConstFetch(t *testing.T) {
src := `<?php
null ;

View File

@@ -586,6 +586,27 @@ func TestPrinterPrintAssignBitwiseXor(t *testing.T) {
}
}
func TestPrinterPrintAssignCoalesce(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&assign.Coalesce{
Variable: &expr.Variable{
VarName: &node.Identifier{Value: "a"},
},
Expression: &expr.Variable{
VarName: &node.Identifier{Value: "b"},
},
})
expected := `$a??=$b`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAssignConcat(t *testing.T) {
o := bytes.NewBufferString("")
@@ -1706,6 +1727,40 @@ func TestPrinterPrintExprClosure(t *testing.T) {
}
}
func TestPrinterPrintExprArrowFunction(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&stmt.Expression{
Expr: &expr.ArrowFunction{
Static: true,
ReturnsRef: true,
Params: []node.Node{
&node.Parameter{
ByRef: true,
Variadic: false,
Variable: &expr.Variable{
VarName: &node.Identifier{Value: "var"},
},
},
},
ReturnType: &name.FullyQualified{
Parts: []node.Node{&name.NamePart{Value: "Foo"}},
},
Expr: &expr.Variable{
VarName: &node.Identifier{Value: "a"},
},
},
})
expected := `static fn&(&$var):\Foo=>$a;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprConstFetch(t *testing.T) {
o := bytes.NewBufferString("")