@@ -230,10 +230,12 @@ func walk(
230230 case * parse.CommandNode :
231231 // handle 'include "test.labels" .' separately
232232 if len (tn .Args ) >= 3 && tn .Args [0 ].String () == "include" && tn .Args [1 ].Type () == parse .NodeString {
233- foundTemplateFn (
234- tn .Args [1 ].(* parse.StringNode ).Text ,
235- getPath (tn .Args [2 ], parentNode , parentPath ),
236- )
233+ foreachPath (tn .Args [2 ], parentNode , parentPath , func (path string ) {
234+ foundTemplateFn (
235+ tn .Args [1 ].(* parse.StringNode ).Text ,
236+ path ,
237+ )
238+ })
237239 }
238240 for _ , snode := range tn .Args {
239241 walk (snode , parentNode , parentPath , foundPathFn , foundTemplateFn , foundVarUsageFn , foundVarDefFn )
@@ -296,31 +298,45 @@ func walk(
296298 foundVarDefFn (varname , node , path + "[*]" )
297299 },
298300 )
299- path := getPath (tn .Pipe , parentNode , parentPath ) + "[*]"
300- walk (tn .List , parentNode , path , foundPathFn , foundTemplateFn , foundVarUsageFn , foundVarDefFn )
301- walk (tn .ElseList , parentNode , path , foundPathFn , foundTemplateFn , foundVarUsageFn , foundVarDefFn )
301+ foreachPath (tn .Pipe , parentNode , parentPath , func (path string ) {
302+ walk (tn .List , parentNode , path + "[*]" , foundPathFn , foundTemplateFn , foundVarUsageFn , foundVarDefFn )
303+ walk (tn .ElseList , parentNode , path + "[*]" , foundPathFn , foundTemplateFn , foundVarUsageFn , foundVarDefFn )
304+ })
302305 case * parse.WithNode :
303- path := getPath (tn .Pipe , parentNode , parentPath )
304- walk (tn .List , parentNode , path , foundPathFn , foundTemplateFn , foundVarUsageFn , foundVarDefFn )
305- walk (tn .ElseList , parentNode , path , foundPathFn , foundTemplateFn , foundVarUsageFn , foundVarDefFn )
306+ walk (tn .Pipe , parentNode , parentPath ,
307+ func (path string ) {
308+ foundPathFn (path )
309+ },
310+ func (templateName , context string ) {
311+ foundTemplateFn (templateName , context )
312+ },
313+ func (varname string , path string ) {
314+ foundVarUsageFn (varname , path )
315+ },
316+ func (varname string , node , path string ) {
317+ foundVarDefFn (varname , node , path )
318+ },
319+ )
320+ foreachPath (tn .Pipe , parentNode , parentPath , func (path string ) {
321+ walk (tn .List , parentNode , path , foundPathFn , foundTemplateFn , foundVarUsageFn , foundVarDefFn )
322+ walk (tn .ElseList , parentNode , path , foundPathFn , foundTemplateFn , foundVarUsageFn , foundVarDefFn )
323+ })
306324 }
307325}
308326
309- func getPath (node parse.Node , parentNode string , parentPath string ) string {
310- longestPath := parentPath
327+ func foreachPath (node parse.Node , parentNode string , parentPath string , found func (string )) {
311328 walk (node , parentNode , parentPath ,
312329 func (path string ) {
313- if len (path ) > len (longestPath ) {
314- longestPath = path
315- }
330+ found (path )
316331 },
317332 func (_ , context string ) {
318- if len (context ) > len (longestPath ) {
319- longestPath = context
320- }
333+ found (context )
334+ },
335+ func (varname string , path string ) {
336+ found (path )
337+ },
338+ func (varname string , node , path string ) {
339+ found (path )
321340 },
322- func (varname string , path string ) {},
323- func (varname string , node , path string ) {},
324341 )
325- return longestPath
326342}
0 commit comments