[#82] property type

This commit is contained in:
z7zmey
2019-12-29 16:36:56 +02:00
parent 7b4c72a3af
commit dc7aa7302d
10 changed files with 715 additions and 574 deletions

View File

@@ -12,14 +12,16 @@ type PropertyList struct {
FreeFloating freefloating.Collection
Position *position.Position
Modifiers []node.Node
Type node.Node
Properties []node.Node
}
// NewPropertyList node constructor
func NewPropertyList(Modifiers []node.Node, Properties []node.Node) *PropertyList {
func NewPropertyList(Modifiers []node.Node, Type node.Node, Properties []node.Node) *PropertyList {
return &PropertyList{
FreeFloating: nil,
Modifiers: Modifiers,
Type: Type,
Properties: Properties,
}
}
@@ -60,6 +62,12 @@ func (n *PropertyList) Walk(v walker.Visitor) {
v.LeaveChildList("Modifiers", n)
}
if n.Type != nil {
v.EnterChildNode("Type", n)
n.Type.Walk(v)
v.LeaveChildNode("Type", n)
}
if n.Properties != nil {
v.EnterChildList("Properties", n)
for _, nn := range n.Properties {

View File

@@ -6,6 +6,7 @@ import (
"gotest.tools/assert"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/position"
@@ -378,3 +379,109 @@ func TestProperties2(t *testing.T) {
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestPropertyType(t *testing.T) {
src := `<? class foo {var bar $a;}`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 26,
},
Stmts: []node.Node{
&stmt.Class{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 26,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 12,
},
Value: "foo",
},
Stmts: []node.Node{
&stmt.PropertyList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 25,
},
Modifiers: []node.Node{
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 17,
},
Value: "var",
},
},
Type: &name.Name{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 21,
},
Parts: []node.Node{
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 21,
},
Value: "bar",
},
},
},
Properties: []node.Node{
&stmt.Property{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
PhpDocComment: "",
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
Value: "a",
},
},
},
},
},
},
},
},
}
php7parser := php7.NewParser([]byte(src), "7.4")
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}

View File

@@ -323,9 +323,10 @@ var nodesToTest = []struct {
{
&stmt.PropertyList{
Modifiers: []node.Node{&stmt.Expression{}},
Type: &name.Name{},
Properties: []node.Node{&stmt.Expression{}},
},
[]string{"Modifiers", "Properties"},
[]string{"Modifiers", "Type", "Properties"},
nil,
},
{