Skip to content

Commit

Permalink
Fix errors in the visitor, add an test/example in how to extract exte…
Browse files Browse the repository at this point in the history
…rnal variables.
  • Loading branch information
jhsx committed May 10, 2017
1 parent 112ffd0 commit 93b0e83
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
17 changes: 14 additions & 3 deletions utils/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand All @@ -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) {
Expand All @@ -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)
Expand Down
58 changes: 58 additions & 0 deletions utils/visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,61 @@ func TestVisitor(t *testing.T) {
t.Errorf("%q", collectedIdentifiers)
}
}

func TestSimpleTemplate(t *testing.T) {
var mTemplate, err = Set.LoadTemplate("_testing2", "<html><head><title>Thank you!</title></head>\n\n<body>\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</body>\n</html>")
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)
}
}

0 comments on commit 93b0e83

Please sign in to comment.