Skip to content

Commit

Permalink
Implement reference validation
Browse files Browse the repository at this point in the history
Add the ability to use the collected origin and target references in early validation.

Validation funcs will be provided by terraform-ls for now.
  • Loading branch information
jpogran committed Aug 10, 2023
1 parent 36dee47 commit 89142aa
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 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 @@ -91,5 +92,41 @@ func DecoderContext(ctx context.Context) decoder.DecoderContext {
}
}

// TODO: extract out of this file
dCtx.Validations = append(dCtx.Validations, func(ctx context.Context) lang.DiagnosticsMap {
// get files from pathContext
//iterate over files and validate
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
})

return dCtx
}

0 comments on commit 89142aa

Please sign in to comment.