Skip to content

Commit 74979c4

Browse files
authored
chore: refactor tree traversal (#3)
Signed-off-by: Abiola Ibrahim <[email protected]>
1 parent 9549377 commit 74979c4

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func parseLayout(root templateSet, t templateFile, funcMap template.FuncMap) (*t
176176
}
177177

178178
// process template tree for layout
179-
refs, err := processTree(layout, t.body, true, true)
179+
refs, err := processTree(layout, t.body)
180180
if err != nil {
181181
return nil, fmt.Errorf("error processing layout: %w", err)
182182
}
@@ -206,7 +206,7 @@ func parseView(root templateSet, layout *template.Template, name, raw string) (*
206206
}
207207

208208
// process template tree for body
209-
refs, err := processTree(body, raw, false, true)
209+
refs, err := processTree(body, raw)
210210
if err != nil {
211211
return nil, fmt.Errorf("error parsing view '%s': %w", name, err)
212212
}

tree.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
// processTree traverses the node tree and swaps render and partial declarations with equivalent template calls.
1111
// It returns all referenced templates encountered during the traversal.
12-
func processTree(t *template.Template, raw string, render, partial bool) ([]templateName, error) {
13-
ts, err := processNode(nil, 0, t.Tree.Root, render, partial)
12+
func processTree(t *template.Template, raw string) ([]templateName, error) {
13+
ts, err := processNode(nil, 0, t.Tree.Root)
1414
if err != nil {
1515
if err, ok := err.(posErr); ok {
1616
line, col := pos(raw, err.pos)
@@ -21,13 +21,7 @@ func processTree(t *template.Template, raw string, render, partial bool) ([]temp
2121
return ts, nil
2222
}
2323

24-
func processNode(
25-
parent *parse.ListNode,
26-
index int,
27-
node parse.Node,
28-
render,
29-
partial bool,
30-
) (ts []templateName, err error) {
24+
func processNode(parent *parse.ListNode, index int, node parse.Node) (ts []templateName, err error) {
3125
// appendResult appends the specified templates to the list of template names when there are no errors
3226
appendResult := func(t []templateName, err1 error) {
3327
if err1 != nil {
@@ -53,21 +47,21 @@ func processNode(
5347
}
5448

5549
if w, ok := node.(*parse.WithNode); ok && w != nil {
56-
appendResult(processNode(parent, index, w.List, render, partial))
57-
appendResult(processNode(parent, index, w.ElseList, render, partial))
50+
appendResult(processNode(parent, index, w.List))
51+
appendResult(processNode(parent, index, w.ElseList))
5852
}
5953
if l, ok := node.(*parse.ListNode); ok && l != nil {
6054
for i, n := range l.Nodes {
61-
appendResult(processNode(l, i, n, render, partial))
55+
appendResult(processNode(l, i, n))
6256
}
6357
}
6458
if i, ok := node.(*parse.IfNode); ok && i != nil {
65-
appendResult(processNode(parent, index, i.List, render, partial))
66-
appendResult(processNode(parent, index, i.ElseList, render, partial))
59+
appendResult(processNode(parent, index, i.List))
60+
appendResult(processNode(parent, index, i.ElseList))
6761
}
6862
if r, ok := node.(*parse.RangeNode); ok && r != nil {
69-
appendResult(processNode(parent, index, r.List, render, partial))
70-
appendResult(processNode(parent, index, r.ElseList, render, partial))
63+
appendResult(processNode(parent, index, r.List))
64+
appendResult(processNode(parent, index, r.ElseList))
7165
}
7266

7367
return ts, err

0 commit comments

Comments
 (0)