From 47cf78fac443e4caf94822d1731e608345287975 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sun, 23 Mar 2025 22:17:59 +0100 Subject: [PATCH] chore: refactor tree traversal Signed-off-by: Abiola Ibrahim --- engine.go | 4 ++-- tree.go | 26 ++++++++++---------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/engine.go b/engine.go index bd76ad3..1bd89b0 100644 --- a/engine.go +++ b/engine.go @@ -176,7 +176,7 @@ func parseLayout(root templateSet, t templateFile, funcMap template.FuncMap) (*t } // process template tree for layout - refs, err := processTree(layout, t.body, true, true) + refs, err := processTree(layout, t.body) if err != nil { return nil, fmt.Errorf("error processing layout: %w", err) } @@ -206,7 +206,7 @@ func parseView(root templateSet, layout *template.Template, name, raw string) (* } // process template tree for body - refs, err := processTree(body, raw, false, true) + refs, err := processTree(body, raw) if err != nil { return nil, fmt.Errorf("error parsing view '%s': %w", name, err) } diff --git a/tree.go b/tree.go index 81f0b8b..d58a046 100644 --- a/tree.go +++ b/tree.go @@ -9,8 +9,8 @@ import ( // processTree traverses the node tree and swaps render and partial declarations with equivalent template calls. // It returns all referenced templates encountered during the traversal. -func processTree(t *template.Template, raw string, render, partial bool) ([]templateName, error) { - ts, err := processNode(nil, 0, t.Tree.Root, render, partial) +func processTree(t *template.Template, raw string) ([]templateName, error) { + ts, err := processNode(nil, 0, t.Tree.Root) if err != nil { if err, ok := err.(posErr); ok { line, col := pos(raw, err.pos) @@ -21,13 +21,7 @@ func processTree(t *template.Template, raw string, render, partial bool) ([]temp return ts, nil } -func processNode( - parent *parse.ListNode, - index int, - node parse.Node, - render, - partial bool, -) (ts []templateName, err error) { +func processNode(parent *parse.ListNode, index int, node parse.Node) (ts []templateName, err error) { // appendResult appends the specified templates to the list of template names when there are no errors appendResult := func(t []templateName, err1 error) { if err1 != nil { @@ -53,21 +47,21 @@ func processNode( } if w, ok := node.(*parse.WithNode); ok && w != nil { - appendResult(processNode(parent, index, w.List, render, partial)) - appendResult(processNode(parent, index, w.ElseList, render, partial)) + appendResult(processNode(parent, index, w.List)) + appendResult(processNode(parent, index, w.ElseList)) } if l, ok := node.(*parse.ListNode); ok && l != nil { for i, n := range l.Nodes { - appendResult(processNode(l, i, n, render, partial)) + appendResult(processNode(l, i, n)) } } if i, ok := node.(*parse.IfNode); ok && i != nil { - appendResult(processNode(parent, index, i.List, render, partial)) - appendResult(processNode(parent, index, i.ElseList, render, partial)) + appendResult(processNode(parent, index, i.List)) + appendResult(processNode(parent, index, i.ElseList)) } if r, ok := node.(*parse.RangeNode); ok && r != nil { - appendResult(processNode(parent, index, r.List, render, partial)) - appendResult(processNode(parent, index, r.ElseList, render, partial)) + appendResult(processNode(parent, index, r.List)) + appendResult(processNode(parent, index, r.ElseList)) } return ts, err