Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove parents field for optimization #527

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
1 change: 1 addition & 0 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type MemberNode struct {
Node Node // Node of the member access. Like "foo" in "foo.bar".
Property Node // Property of the member access. For property access it is a StringNode.
Optional bool // If true then the member access is optional. Like "foo?.bar".
Method bool
}

// SliceNode represents access to a slice of an array.
Expand Down
15 changes: 5 additions & 10 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type checker struct {
config *conf.Config
predicateScopes []predicateScope
varScopes []varScope
parents []ast.Node
err *file.Error
}

Expand Down Expand Up @@ -83,7 +82,6 @@ type info struct {
func (v *checker) visit(node ast.Node) (reflect.Type, info) {
var t reflect.Type
var i info
v.parents = append(v.parents, node)
switch n := node.(type) {
case *ast.NilNode:
t, i = v.NilNode(n)
Expand Down Expand Up @@ -130,7 +128,6 @@ func (v *checker) visit(node ast.Node) (reflect.Type, info) {
default:
panic(fmt.Sprintf("undefined node type (%T)", node))
}
v.parents = v.parents[:len(v.parents)-1]
node.SetType(t)
return t, i
}
Expand Down Expand Up @@ -431,9 +428,6 @@ func (v *checker) ChainNode(node *ast.ChainNode) (reflect.Type, info) {
}

func (v *checker) MemberNode(node *ast.MemberNode) (reflect.Type, info) {
base, _ := v.visit(node.Node)
prop, _ := v.visit(node.Property)

// $env variable
if an, ok := node.Node.(*ast.IdentifierNode); ok && an.Value == "$env" {
if name, ok := node.Property.(*ast.StringNode); ok {
Expand All @@ -450,6 +444,9 @@ func (v *checker) MemberNode(node *ast.MemberNode) (reflect.Type, info) {
return anyType, info{}
}

base, _ := v.visit(node.Node)
prop, _ := v.visit(node.Property)

if name, ok := node.Property.(*ast.StringNode); ok {
if base == nil {
return v.error(node, "type %v has no field %v", base, name.Value)
Expand Down Expand Up @@ -498,10 +495,8 @@ func (v *checker) MemberNode(node *ast.MemberNode) (reflect.Type, info) {
if field, ok := fetchField(base, propertyName); ok {
return field.Type, info{}
}
if len(v.parents) > 1 {
if _, ok := v.parents[len(v.parents)-2].(*ast.CallNode); ok {
return v.error(node, "type %v has no method %v", base, propertyName)
}
if node.Method {
return v.error(node, "type %v has no method %v", base, propertyName)
}
return v.error(node, "type %v has no field %v", base, propertyName)
}
Expand Down
1 change: 1 addition & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ func (p *parser) parsePostfixExpression(node Node) Node {
memberNode.SetLocation(propertyToken.Location)

if p.current.Is(Bracket, "(") {
memberNode.Method = true
node = &CallNode{
Callee: memberNode,
Arguments: p.parseArguments(),
Expand Down
6 changes: 4 additions & 2 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ world`},
{
"foo.bar()",
&CallNode{Callee: &MemberNode{Node: &IdentifierNode{Value: "foo"},
Property: &StringNode{Value: "bar"}},
Property: &StringNode{Value: "bar"}, Method: true},
Arguments: []Node{}},
},
{
`foo.bar("arg1", 2, true)`,
&CallNode{Callee: &MemberNode{Node: &IdentifierNode{Value: "foo"},
Property: &StringNode{Value: "bar"}},
Property: &StringNode{Value: "bar"}, Method: true},
Arguments: []Node{&StringNode{Value: "arg1"},
&IntegerNode{Value: 2},
&BoolNode{Value: true}}},
Expand Down Expand Up @@ -220,12 +220,14 @@ world`},
Property: &StringNode{
Value: "b",
},
Method: true,
},
Arguments: []Node{},
},
Property: &StringNode{
Value: "c",
},
Method: true,
},
Arguments: []Node{},
},
Expand Down
Loading