From e0c6a9ed6f0c709a17d5016e9346bbb752cf8863 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 10 Jun 2024 22:28:17 +0200 Subject: [PATCH] Fix linter: using a local in an assertion. (#723) --- linter/internal/variables/find_variables.go | 3 +++ linter/testdata/local_used_in_assertion.jsonnet | 6 ++++++ linter/testdata/local_used_in_assertion.linter.golden | 0 testdata/local_in_object_assertion.linter.golden | 5 ----- 4 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 linter/testdata/local_used_in_assertion.jsonnet create mode 100644 linter/testdata/local_used_in_assertion.linter.golden diff --git a/linter/internal/variables/find_variables.go b/linter/internal/variables/find_variables.go index cfdef8ede..64b1862d7 100644 --- a/linter/internal/variables/find_variables.go +++ b/linter/internal/variables/find_variables.go @@ -67,6 +67,9 @@ func findVariablesInObject(node *ast.DesugaredObject, info *common.VariableInfo, for _, local := range node.Locals { findVariables(local.Body, info, scopeInside) } + for _, assert := range node.Asserts { + findVariables(assert, info, scopeInside) + } for _, field := range node.Fields { findVariables(field.Body, info, scopeInside) findVariables(field.Name, info, scopeOutside) diff --git a/linter/testdata/local_used_in_assertion.jsonnet b/linter/testdata/local_used_in_assertion.jsonnet new file mode 100644 index 000000000..f57990457 --- /dev/null +++ b/linter/testdata/local_used_in_assertion.jsonnet @@ -0,0 +1,6 @@ +{ + local input = [1,2,3], + local knownItems = [1,2], + local unknownItems = std.filter(function(i) !(i in knownItems), input), + assert unknownItems == [] : "unexpected items: %s" % std.join(",",unknownItems) +} \ No newline at end of file diff --git a/linter/testdata/local_used_in_assertion.linter.golden b/linter/testdata/local_used_in_assertion.linter.golden new file mode 100644 index 000000000..e69de29bb diff --git a/testdata/local_in_object_assertion.linter.golden b/testdata/local_in_object_assertion.linter.golden index 8e110cfd1..e69de29bb 100644 --- a/testdata/local_in_object_assertion.linter.golden +++ b/testdata/local_in_object_assertion.linter.golden @@ -1,5 +0,0 @@ -../testdata/local_in_object_assertion:1:9-15 Unused variable: x - -{ local x = 42, assert x == 42 } - -