From bb7ee2d0b86d73d2c670fd4e34f07ede225ae98b Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Sun, 8 Sep 2024 18:49:13 -0400 Subject: [PATCH 1/2] Extract `findSelfObject` This is used twice and it's complex enough that it can be extracted --- pkg/ast/processing/find_field.go | 43 +++++++++++++------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/pkg/ast/processing/find_field.go b/pkg/ast/processing/find_field.go index a35a38b..7ba55d7 100644 --- a/pkg/ast/processing/find_field.go +++ b/pkg/ast/processing/find_field.go @@ -159,19 +159,7 @@ func (p *Processor) extractObjectRangesFromDesugaredObjs(desugaredObjs []*ast.De case *ast.Binary: fieldNodes = append(fieldNodes, flattenBinary(fieldNode)...) case *ast.Self: - filename := fieldNode.LocRange.FileName - rootNode, _, _ := p.vm.ImportAST("", filename) - tmpStack, err := FindNodeByPosition(rootNode, fieldNode.LocRange.Begin) - if err != nil { - return nil, err - } - for !tmpStack.IsEmpty() { - node := tmpStack.Pop() - if castNode, ok := node.(*ast.DesugaredObject); ok { - desugaredObjs = append(desugaredObjs, castNode) - break - } - } + desugaredObjs = append(desugaredObjs, p.findSelfObject(fieldNode)) } i++ } @@ -195,18 +183,7 @@ func (p *Processor) unpackFieldNodes(fields []*ast.DesugaredObjectField) ([]ast. for _, foundField := range fields { switch fieldNode := foundField.Body.(type) { case *ast.Self: - filename := fieldNode.LocRange.FileName - rootNode, _, _ := p.vm.ImportAST("", filename) - tmpStack, err := FindNodeByPosition(rootNode, fieldNode.LocRange.Begin) - if err != nil { - return nil, err - } - for !tmpStack.IsEmpty() { - node := tmpStack.Pop() - if _, ok := node.(*ast.DesugaredObject); ok { - fieldNodes = append(fieldNodes, node) - } - } + fieldNodes = append(fieldNodes, p.findSelfObject(fieldNode)) case *ast.Binary: fieldNodes = append(fieldNodes, flattenBinary(fieldNode)...) default: @@ -304,3 +281,19 @@ func (p *Processor) findLHSDesugaredObject(stack *nodestack.NodeStack) (*ast.Des } return nil, fmt.Errorf("could not find a lhs object") } + +func (p *Processor) findSelfObject(self *ast.Self) *ast.DesugaredObject { + filename := self.LocRange.FileName + rootNode, _, _ := p.vm.ImportAST("", filename) + tmpStack, err := FindNodeByPosition(rootNode, self.LocRange.Begin) + if err != nil { + return nil + } + for !tmpStack.IsEmpty() { + node := tmpStack.Pop() + if castNode, ok := node.(*ast.DesugaredObject); ok { + return castNode + } + } + return nil +} From ef30a9afae5bd74cebd7855b7241d8bd9e2dc0ec Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Sun, 8 Sep 2024 19:18:08 -0400 Subject: [PATCH 2/2] Simpler `unpackFieldNodes` --- pkg/ast/processing/find_field.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pkg/ast/processing/find_field.go b/pkg/ast/processing/find_field.go index 7ba55d7..5d5d5b7 100644 --- a/pkg/ast/processing/find_field.go +++ b/pkg/ast/processing/find_field.go @@ -107,11 +107,7 @@ func (p *Processor) extractObjectRangesFromDesugaredObjs(desugaredObjs []*ast.De return ranges, nil } - fieldNodes, err := p.unpackFieldNodes(foundFields) - if err != nil { - return nil, err - } - + fieldNodes := p.unpackFieldNodes(foundFields) i := 0 for i < len(fieldNodes) { fieldNode := fieldNodes[i] @@ -145,7 +141,7 @@ func (p *Processor) extractObjectRangesFromDesugaredObjs(desugaredObjs []*ast.De additionalIndexList := append(nodestack.NewNodeStack(fieldNode).BuildIndexList(), indexList...) result, _ := p.FindRangesFromIndexList(stack, additionalIndexList, partialMatchFields) if len(result) > 0 { - return result, err + return result, nil } } @@ -178,7 +174,7 @@ func flattenBinary(node ast.Node) []ast.Node { // unpackFieldNodes extracts nodes from fields // - Binary nodes. A field could be either in the left or right side of the binary // - Self nodes. We want the object self refers to, not the self node itself -func (p *Processor) unpackFieldNodes(fields []*ast.DesugaredObjectField) ([]ast.Node, error) { +func (p *Processor) unpackFieldNodes(fields []*ast.DesugaredObjectField) []ast.Node { var fieldNodes []ast.Node for _, foundField := range fields { switch fieldNode := foundField.Body.(type) { @@ -191,7 +187,7 @@ func (p *Processor) unpackFieldNodes(fields []*ast.DesugaredObjectField) ([]ast. } } - return fieldNodes, nil + return fieldNodes } func findObjectFieldsInObjects(objectNodes []*ast.DesugaredObject, index string, partialMatchFields bool) []*ast.DesugaredObjectField {