From 93b0e830ecf4c3f6102e1a69c6ea3bafa37aee7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Santos?= Date: Wed, 10 May 2017 22:49:29 +0100 Subject: [PATCH] Fix errors in the visitor, add an test/example in how to extract external variables. --- utils/visitor.go | 17 ++++++++++--- utils/visitor_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/utils/visitor.go b/utils/visitor.go index 27185f8..8890f95 100644 --- a/utils/visitor.go +++ b/utils/visitor.go @@ -88,7 +88,7 @@ func (vc VisitorContext) Visit(node jet.Node) { case *jet.FieldNode: default: - panic(fmt.Errorf("unexpected node %t", node)) + panic(fmt.Errorf("unexpected node %v", node)) } } @@ -97,15 +97,22 @@ func (vc VisitorContext) visitIncludeNode(includeNode *jet.IncludeNode) { } func (vc VisitorContext) visitBlockNode(blockNode *jet.BlockNode) { + for _, node := range blockNode.Parameters.List { if node.Expression != nil { vc.visitNode(node.Expression) } } + if blockNode.Expression != nil { vc.visitNode(blockNode.Expression) } - vc.visitNode(blockNode.Content) + + vc.visitListNode(blockNode.List) + + if blockNode.Content != nil { + vc.visitNode(blockNode.Content) + } } func (vc VisitorContext) visitRangeNode(rangeNode *jet.RangeNode) { @@ -125,7 +132,11 @@ func (vc VisitorContext) visitBranchNode(branchNode *jet.BranchNode) { if branchNode.Set != nil { vc.visitNode(branchNode.Set) } - vc.visitNode(branchNode.Expression) + + if branchNode.Expression != nil { + vc.visitNode(branchNode.Expression) + } + vc.visitNode(branchNode.List) if branchNode.ElseList != nil { vc.visitNode(branchNode.ElseList) diff --git a/utils/visitor_test.go b/utils/visitor_test.go index 2e1250c..3b9533f 100644 --- a/utils/visitor_test.go +++ b/utils/visitor_test.go @@ -22,3 +22,61 @@ func TestVisitor(t *testing.T) { t.Errorf("%q", collectedIdentifiers) } } + +func TestSimpleTemplate(t *testing.T) { + var mTemplate, err = Set.LoadTemplate("_testing2", "Thank you!\n\n\n\tHello {{userName}},\n\n\tThanks for the order!\n\n\t{{range product := products}}\n\t\t{{product.name}}\n\n\t {{block productPrice(price=product.Price) product}}\n {{if price > ExpensiveProduct}}\n Expensive!!\n {{end}}\n {{end}}\n\n\t\t${{product.price / 100}}\n\t{{end}}\n\n") + if err != nil { + t.Error(err) + } + + var ( + localVariables []string + externalVariables []string + ) + + Walk(mTemplate, VisitorFunc(func(context VisitorContext, node jet.Node) { + var stackState = len(localVariables) + + switch node := node.(type) { + case *jet.ActionNode: + context.Visit(node) + case *jet.SetNode: + if node.Let { + for _, ident := range node.Left { + localVariables = append(localVariables, ident.String()) + } + } + context.Visit(node) + case *jet.IdentifierNode: + + // skip local identifiers + for _, varName := range localVariables { + if varName == node.Ident { + return + } + } + + // skip already inserted identifiers + for _, varName := range externalVariables { + if varName == node.Ident { + return + } + } + + externalVariables = append(externalVariables, node.Ident) + case *jet.BlockNode: + for _, param := range node.Parameters.List { + localVariables = append(localVariables, param.Identifier) + } + context.Visit(node) + default: + context.Visit(node) + localVariables = localVariables[0:stackState] + } + + })) + + if !reflect.DeepEqual(externalVariables, []string{"userName", "products", "ExpensiveProduct"}) { + t.Errorf("%q", externalVariables) + } +}