Skip to content

Commit

Permalink
validations: Enable reference validation of locals (#1381)
Browse files Browse the repository at this point in the history
* validations: Enable reference validation of locals

* Update internal/decoder/validations/unreferenced_origin.go

Co-authored-by: Daniel Banck <dbanck@users.noreply.github.com>

* fix imports

---------

Co-authored-by: Daniel Banck <dbanck@users.noreply.github.com>
  • Loading branch information
radeksimko and dbanck committed Sep 7, 2023
1 parent 703ba06 commit 468764c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
8 changes: 5 additions & 3 deletions internal/decoder/validations/unreferenced_origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package validations
import (
"context"
"fmt"
"slices"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
Expand Down Expand Up @@ -44,11 +45,12 @@ func UnreferencedOrigins(ctx context.Context, pathCtx *decoder.PathContext) lang
continue
}

// we only initially validate variables
// we only initially validate variables & local values
// resources and data sources can have unknown schema
// and will be researched at a later point
firstStep := address[0]
if firstStep.String() != "var" {
supported := []string{"var", "local"}
firstStep := address[0].String()
if !slices.Contains(supported, firstStep) {
continue
}

Expand Down
29 changes: 29 additions & 0 deletions internal/decoder/validations/unreferenced_origin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ func TestUnreferencedOrigins(t *testing.T) {
},
},
},
{
name: "undeclared local value",
origins: reference.Origins{
reference.LocalOrigin{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{},
End: hcl.Pos{},
},
Addr: lang.Address{
lang.RootStep{Name: "local"},
lang.AttrStep{Name: "foo"},
},
},
},
want: lang.DiagnosticsMap{
"test.tf": hcl.Diagnostics{
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "No declaration found for \"local.foo\"",
Subject: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{},
End: hcl.Pos{},
},
},
},
},
},
{
name: "unsupported variable of complex type",
origins: reference.Origins{
Expand Down

0 comments on commit 468764c

Please sign in to comment.