Skip to content

Commit

Permalink
extract and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jpogran committed Aug 15, 2023
1 parent ede6647 commit 85d39dd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 48 deletions.
5 changes: 4 additions & 1 deletion internal/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/codelens"
Expand Down Expand Up @@ -92,7 +93,9 @@ func DecoderContext(ctx context.Context) decoder.DecoderContext {
}
}

validations := validations.ValidationFuncs()
validations := []lang.ValidationFunc{
validations.UnReferencedOrigin,
}
dCtx.Validations = append(dCtx.Validations, validations...)

return dCtx
Expand Down
43 changes: 43 additions & 0 deletions internal/decoder/validations/unreferenced_origin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package validations

import (
"context"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2"
)

func UnReferencedOrigin(ctx context.Context) lang.DiagnosticsMap {
pathCtx, err := decoder.PathCtx(ctx)
if err != nil {
// TODO
}

diagsMap := make(lang.DiagnosticsMap)

for _, origin := range pathCtx.ReferenceOrigins {
matchableOrigin, ok := origin.(reference.MatchableOrigin)
if !ok {
// TODO: add a diag here
continue
}

_, ok = pathCtx.ReferenceTargets.Match(matchableOrigin)
if !ok {
// target not found
diagsMap[origin.OriginRange().Filename] = hcl.Diagnostics{
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "No reference found", // TODO: Is there more we can state here?
Subject: origin.OriginRange().Ptr(),
},
}
continue
}

}

return diagsMap
}
44 changes: 44 additions & 0 deletions internal/decoder/validations/unreferenced_origin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package validations

import (
"context"
"reflect"
"testing"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl/v2"
)

func TestUnReferencedOrigin(t *testing.T) {
ctx := context.Background()
// build pathdecoder
// set pathctx
// ctx = withPathContext(ctx, d.pathCtx)

tests := []struct {
name string
ctx context.Context
want lang.DiagnosticsMap
}{
{
name: "unreferenced variable",
ctx: ctx,
want: lang.DiagnosticsMap{
"test.tf": hcl.Diagnostics{
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "No reference found",
Subject: &hcl.Range{},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := UnReferencedOrigin(tt.ctx); !reflect.DeepEqual(got, tt.want) {
t.Errorf("UnReferencedOrigin() = %v, want %v", got, tt.want)
}
})
}
}
47 changes: 0 additions & 47 deletions internal/decoder/validations/validations.go

This file was deleted.

0 comments on commit 85d39dd

Please sign in to comment.