Skip to content

Commit f1c399c

Browse files
authored
Merge pull request #28 from inteon/add_extra_linter_cases
Add support for missing `with (or ...)` variable detection.
2 parents 65cefcd + 30ec1d9 commit f1c399c

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

linter/parsetemplates/templates.go

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

linter/parsetemplates/templates_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ func TestListTemplatePathsFromTemplates(t *testing.T) {
157157
"test2",
158158
},
159159
},
160+
{
161+
templates: []string{
162+
"{{ if .Values.test1 }}{{ end }}",
163+
},
164+
expectedPaths: []string{
165+
"test1",
166+
},
167+
},
160168
{
161169
templates: []string{
162170
"{{define \"T1\" }}{{ .test2 }}{{end}} {{ .Values.foo }}",
@@ -229,6 +237,24 @@ func TestListTemplatePathsFromTemplates(t *testing.T) {
229237
"app.name",
230238
},
231239
},
240+
{
241+
templates: []string{
242+
"{{- with (or .Values.test1 .Values.test2) }}{{- toYaml . | nindent 8 }}{{- end }}",
243+
},
244+
expectedPaths: []string{
245+
"test1",
246+
"test2",
247+
},
248+
},
249+
{
250+
templates: []string{
251+
"{{- with (or .Values.test1 .Values.test2) }}aaa{{- end }}",
252+
},
253+
expectedPaths: []string{
254+
"test1",
255+
"test2",
256+
},
257+
},
232258
}
233259

234260
for _, tc := range testcases {

0 commit comments

Comments
 (0)